18.5.asyncio — Asynchronous I/O, event loop, coroutines and tasks

New in version 3.4.

Source code:Lib/asyncio/


This module provides infrastructure for writing single-threaded concurrentcode using coroutines, multiplexing I/O access over sockets and otherresources, running network clients and servers, and other related primitives.Here is a more detailed list of the package contents:

  • a pluggableevent loop with various system-specificimplementations;

  • transport andprotocol abstractions(similar to those inTwisted);

  • concrete support for TCP, UDP, SSL, subprocess pipes, delayed calls, andothers (some may be system-dependent);

  • aFuture class that mimics the one in theconcurrent.futuresmodule, but adapted for use with the event loop;

  • coroutines and tasks based onyieldfrom (PEP 380), to help writeconcurrent code in a sequential fashion;

  • cancellation support forFutures and coroutines;

  • synchronization primitives for use between coroutines ina single thread, mimicking those in thethreading module;

  • an interface for passing work off to a threadpool, for times whenyou absolutely, positively have to use a library that makes blockingI/O calls.

Asynchronous programming is more complex than classical “sequential”programming: see theDevelop with asyncio page which listscommon traps and explains how to avoid them.Enable the debug mode during development to detect common issues.

Table of contents:

See also

Theasyncio module was designed inPEP 3156. For amotivational primer on transports and protocols, seePEP 3153.