<< plib_concurrent

Python Library asyncio

asyncio Introduction

asyncio is a library to write concurrent code using async/await syntax.

asyncio provides a set of high-level APIs to:

Additionally, there are low-level APIs for library and framework developers to:

coroutine

We create coroutine by adding async before def of function:

import asyncio

async def main():
    print('hello')
    await asyncio.sleep(1)
    print('world')

asyncio.run(main())

Note that the coroutine will not be executed if you simply call main(), same as asyncio.sleep(1), which is also a coroutine.

Three main mechanism are introduced by asyncio to run a coroutine (we’ve demonstrated two of them in the example above):

Note that the two methods above both run the coroutine sequentially.

Consider two examples that run the coroutine sequentially(by await) and concurrently(by create_task) respectively:

import asyncio
import time

async def say_after(delay, what):
    await asyncio.sleep(delay)
    print(what)

async def main():
    print(f"started at {time.strftime('%X')}")
    await say_after(1, 'hello')
    await say_after(2, 'world')
    print(f"finished at {time.strftime('%X')}")

asyncio.run(main())
import asyncio
import time


async def say_after(delay, what):
    await asyncio.sleep(delay)
    print(what)

async def main():
    task1 = asyncio.create_task(say_after(1, 'hello'))
    task2 = asyncio.create_task(say_after(2, 'world'))

    print(f"started at {time.strftime('%X')}")
    await task1
    await task2
    print(f"finished at {time.strftime('%X')}")

asyncio.run(main())

Source codes are presented in async_await_demo.py and async_task_demo.py

The time elapsed are 3 seconds for the sequential program while 2 seconds for the concurrent program.