A Guide to Python asyncio


Concurrency is like having multiple threads running on a single CPU core. There is nothing extraordinary about that. A modern workstation has four or eight CPU cores, but, at the same time, is running more than 100 processes. Even though the CPU itself can’t handle more than four or eight jobs at once, the computer is seamlessly dealing with +100 processes.

Coroutines and Tasks

Coroutines are a key element of the library. Just like generators, coroutines produce data, but can also consume data. A coroutine can suspend its execution if no further progress can be made (because, for instance, it is waiting for a network request to be completed) and transfer the control to another coroutine, which can utilize CPU time better. The point where the coroutine suspends its execution is saved. Once the network response comes, the execution could be resumed from that point. Historically, before Python 3.5, coroutines shared syntax with generators. This has changed with PEP 492, which introduced new async/await syntax to Python. Now coroutines are declared with the async defstatement.

10:53:24.500115 "Hello!" scheduled for execution
10:53:25.504662 Hello!
10:53:25.504785 "Hi!" scheduled for execution
10:53:27.506079 Hi!
10:53:27.506187 Done.
13:19:52.941399 "Hello!" scheduled for execution
13:19:52.941477 "Hi!" scheduled for execution
13:19:53.941723 Hello!
13:19:54.946535 Hi!
13:19:54.946714 Done.
11:36:30.754006 Starting worker...
11:36:31.755152 Message from worker
11:36:32.756569 Message from worker
11:36:33.757679 Message from worker
11:36:34.255729 The task has been canceled

Asynchronous Downloader

Network I/O is a good example of how an asynchronous operation can handle things more efficiently. Instead of wasting CPU cycles waiting, it is better to do something else until a response comes back from the network. The following example downloads zip archives containing documentation for three versions of Python 3.8: 3.8.4, 3.8.5 and 3.8.6.

pip install aiohttp


asyncio introduces a whole new way of writing concurrent code in Python. Many third-party libraries are introducing support for asyncio, and its popularity is growing fast. Meanwhile, because it is relatively new, asyncio still lacks coverage in books and online tutorials. This is especially true for the new async/await syntax. Let’s hope that despite all the obstacles, the spread of asyncio will continue and result in better and more efficient Python code.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store