... continued

This lesson discusses the general concept of a coroutine.

We'll cover the following...

We'll define an uber coroutine that will create the server and instantiate the users.

async def main():
    server_port = random.randint(10000, 65000)
    server_host = "127.0.0.1"
    chat_server = ChatServer(server_port)
    jane = User("Jane", server_host, server_port)
    zak = User("Zak", server_host, server_port)

    server = await asyncio.start_server(chat_server.run_server, server_host, server_port)
    asyncio.create_task(jane.run_client())
    asyncio.create_task(zak.run_client())

    await server.serve_forever()

Note that we are using the asyncio.create_task() API to schedule the execution of the run_client() API for each user object. Finally, we'll use the asyncio.run() method to run the main() coroutine. The complete code appears below:

...