Glossary

asyncio

asyncio is Python's standard library module for writing asynchronous, concurrent code using coroutines, an event loop, and non-blocking I/O. It was introduced in Python 3.4 (PEP 3156) and has been significantly refined in every subsequent release. The entry point is typically asyncio.run(main()), which creates an event loop, runs the coroutine, and shuts down cleanly.

The event loop is the core of asyncio. It runs coroutines, handles I/O callbacks, schedules delayed calls, and manages subprocesses. asyncio.gather() runs multiple coroutines concurrently and returns their results; asyncio.create_task() schedules a coroutine to run soon; asyncio.wait_for() adds a timeout; and asyncio.Queue provides an async-safe channel between producers and consumers.

asyncio is not the only async framework in Python — Trio and AnyIO offer alternative designs with structured concurrency — but it is the standard and most widely supported. Major libraries built on asyncio include aiohttp (HTTP client/server), FastAPI (web framework), asyncpg (PostgreSQL driver), and aiofiles (file I/O). Understanding asyncio requires understanding that it is cooperative: a coroutine that blocks without await will freeze the entire event loop.

Related terms: Coroutine, Concurrent Futures, FastAPI

Discussed in:

This site is currently in Beta. Please email Chris Paton (cpaton@gmail.com) with any suggestions, questions or comments.