Python 3.4+Async programmingterm
asyncio
module
Asynchronous I/O with async/await: many tasks in a single thread driven by an event loop.
Syntax
import asyncio
It suits work that mostly waits on the network or disk: thousands of connections served by one thread. It does not speed up computation — use processes (concurrent.futures, multiprocessing) for that, and threads for simple waiting with blocking code. A common mistake is calling a blocking function such as time.sleep inside a coroutine: it stalls the whole event loop.
Examples
import asyncio
async def square(n):
await asyncio.sleep(0)
return n * n
async def main():
return await asyncio.gather(square(2), square(3))
print(asyncio.run(main())) # → [4, 9]
See also
Open in the full glossarydocs.python.orgReport an inaccuracy