AsyncIO for Networking

Learn about the architecture of an AsyncIO-driven server for inter-process logging, highlighting effective coroutine management and responsive concurrency strategies.

Overview

AsyncIO was specifically designed for use with network sockets, so let’s implement a server using the asyncio module. We’ve created a fairly complex server to catch log entries being sent from one process to another process using sockets. At the time, we used it as an example of a complex resource we didn’t want to set up and tear down for each test.

Example

We’ll rewrite that example, creating an asyncio-based server that can handle requests from a (large) number of clients. It can do this by having lots of coroutines, all waiting for log records to arrive. When a record arrives, one coroutine can save the record, doing some computation, while the remaining coroutines wait.

We’ve already discussed the integration of a log catcher process with separate log-writing client application processes. Here’s an illustration of the relationships involved:

Get hands-on with 1200+ tech skills courses.