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 concurrent code using coroutines, multiplexing I/O access over sockets and other resources, running network clients and servers, and other related primitives.

Here is a more detailed list of the package contents:

  • a pluggable event loop with various system-specific implementations;
  • transport and protocol abstractions (similar to those in Twisted);
  • concrete support for TCP, UDP, SSL, subprocess pipes, delayed calls, and others (some may be system-dependent);
  • a Future class that mimicks the one in the concurrent.futures module, but adapted for use with the event loop;
  • coroutines and tasks based on yield from (PEP 380), to help write concurrent code in a sequential fashion;
  • cancellation support for Futures and coroutines;
  • synchronization primitives for use between coroutines in a single thread, mimicking those in the threading module;
  • an interface for passing work off to a threadpool, for times when you absolutely, positively have to use a library that makes blocking I/O calls.

18.5.1. Disclaimer

Full documentation is not yet ready; we hope to have it written before Python 3.4 leaves beta. Until then, the best reference is PEP 3156. For a motivational primer on transports and protocols, see PEP 3153.

18.5.2. Event loops

The event loop is the central execution device provided by asyncio. It provides multiple facilities, amongst which:

  • Registering, executing and cancelling delayed calls (timeouts)
  • Creating client and server transports for various kinds of communication
  • Launching subprocesses and the associated transports for communication with an external program
  • Delegating costly function calls to a pool of threads

18.5.2.1. Getting an event loop

The easiest way to get an event loop is to call the get_event_loop() function.

18.5.2.2. Delayed calls

The event loop has its own internal clock for computing timeouts. Which clock is used depends on the (platform-specific) event loop implementation; ideally it is a monotonic clock. This will generally be a different clock than time.time().

asyncio.time()

Return the current time, as a float value, according to the event loop’s internal clock.

asyncio.call_later(delay, callback, *args)

Arrange for the callback to be called after the given delay seconds (either an int or float).

A “handle” is returned: an opaque object with a cancel() method that can be used to cancel the call.

callback will be called exactly once per call to call_later(). If two callbacks are scheduled for exactly the same time, it is undefined which will be called first.

The optional positional args will be passed to the callback when it is called. If you want the callback to be called with some named arguments, use a closure or functools.partial().

asyncio.call_at(when, callback, *args)

Arrange for the callback to be called at the given absolute timestamp when (an int or float), using the same time reference as time().

This method’s behavior is the same as call_later().

18.5.2.3. Creating connections

asyncio.create_connection(protocol_factory, host=None, port=None, **options)

Create a streaming transport connection to a given Internet host and port. protocol_factory must be a callable returning a protocol instance.

This method returns a coroutine which will try to establish the connection in the background. When successful, the coroutine returns a (transport, protocol) pair.

The chronological synopsis of the underlying operation is as follows:

  1. The connection is established, and a transport is created to represent it.
  2. protocol_factory is called without arguments and must return a protocol instance.
  3. The protocol instance is tied to the transport, and its connection_made() method is called.
  4. The coroutine returns successfully with the (transport, protocol) pair.

The created transport is an implementation-dependent bidirectional stream.

Note

protocol_factory can be any kind of callable, not necessarily a class. For example, if you want to use a pre-created protocol instance, you can pass lambda: my_protocol.

options are optional named arguments allowing to change how the connection is created:

  • ssl: if given and not false, a SSL/TLS transport is created (by default a plain TCP transport is created). If ssl is a ssl.SSLContext object, this context is used to create the transport; if ssl is True, a context with some unspecified default settings is used.
  • server_hostname, is only for use together with ssl, and sets or overrides the hostname that the target server’s certificate will be matched against. By default the value of the host argument is used. If host is empty, there is no default and you must pass a value for server_hostname. If server_hostname is an empty string, hostname matching is disabled (which is a serious security risk, allowing for man-in-the-middle-attacks).
  • family, proto, flags are the optional address family, protocol and flags to be passed through to getaddrinfo() for host resolution. If given, these should all be integers from the corresponding socket module constants.
  • sock, if given, should be an existing, already connected socket.socket object to be used by the transport. If sock is given, none of host, port, family, proto, flags and local_addr should be specified.
  • local_addr, if given, is a (local_host, local_port) tuple used to bind the socket to locally. The local_host and local_port are looked up using getaddrinfo(), similarly to host and port.

18.5.3. Protocols

asyncio provides base classes that you can subclass to implement your network protocols. Those classes are used in conjunction with transports (see below): the protocol parses incoming data and asks for the writing of outgoing data, while the transport is responsible for the actual I/O and buffering.

When subclassing a protocol class, it is recommended you override certain methods. Those methods are callbacks: they will be called by the transport on certain events (for example when some data is received); you shouldn’t call them yourself, unless you are implementing a transport.

Note

All callbacks have default implementations, which are empty. Therefore, you only need to implement the callbacks for the events in which you are interested.

18.5.3.1. Protocol classes

class asyncio.Protocol

The base class for implementing streaming protocols (for use with e.g. TCP and SSL transports).

class asyncio.DatagramProtocol

The base class for implementing datagram protocols (for use with e.g. UDP transports).

class asyncio.SubprocessProtocol

The base class for implementing protocols communicating with child processes (through a set of unidirectional pipes).

18.5.3.2. Connection callbacks

These callbacks may be called on Protocol and SubprocessProtocol instances:

asyncio.connection_made(transport)

Called when a connection is made.

The transport argument is the transport representing the connection. You are responsible for storing it somewhere (e.g. as an attribute) if you need to.

asyncio.connection_lost(exc)

Called when the connection is lost or closed.

The argument is either an exception object or None. The latter means a regular EOF is received, or the connection was aborted or closed by this side of the connection.

connection_made() and connection_lost() are called exactly once per successful connection. All other callbacks will be called between those two methods, which allows for easier resource management in your protocol implementation.

The following callbacks may be called only on SubprocessProtocol instances:

asyncio.pipe_data_received(fd, data)

Called when the child process writes data into its stdout or stderr pipe. fd is the integer file descriptor of the pipe. data is a non-empty bytes object containing the data.

asyncio.pipe_connection_lost(fd, exc)

Called when one of the pipes communicating with the child process is closed. fd is the integer file descriptor that was closed.

asyncio.process_exited()

Called when the child process has exited.

18.5.3.3. Data reception callbacks

18.5.3.3.1. Streaming protocols

The following callbacks are called on Protocol instances:

asyncio.data_received(data)

Called when some data is received. data is a non-empty bytes object containing the incoming data.

Note

Whether the data is buffered, chunked or reassembled depends on the transport. In general, you shouldn’t rely on specific semantics and instead make your parsing generic and flexible enough. However, data is always received in the correct order.

asyncio.eof_received()

Calls when the other end signals it won’t send any more data (for example by calling write_eof(), if the other end also uses asyncio).

This method may return a false value (including None), in which case the transport will close itself. Conversely, if this method returns a true value, closing the transport is up to the protocol. Since the default implementation returns None, it implicitly closes the connection.

Note

Some transports such as SSL don’t support half-closed connections, in which case returning true from this method will not prevent closing the connection.

data_received() can be called an arbitrary number of times during a connection. However, eof_received() is called at most once and, if called, data_received() won’t be called after it.

18.5.3.3.2. Datagram protocols

The following callbacks are called on DatagramProtocol instances.

asyncio.datagram_received(data, addr)

Called when a datagram is received. data is a bytes object containing the incoming data. addr is the address of the peer sending the data; the exact format depends on the transport.

asyncio.error_received(exc)

Called when a previous send or receive operation raises an OSError. exc is the OSError instance.

This method is called in rare conditions, when the transport (e.g. UDP) detects that a datagram couldn’t be delivered to its recipient. In many conditions though, undeliverable datagrams will be silently dropped.

18.5.3.4. Flow control callbacks

These callbacks may be called on Protocol and SubprocessProtocol instances:

asyncio.pause_writing()

Called when the transport’s buffer goes over the high-water mark.

asyncio.resume_writing()

Called when the transport’s buffer drains below the low-water mark.

pause_writing() and resume_writing() calls are paired – pause_writing() is called once when the buffer goes strictly over the high-water mark (even if subsequent writes increases the buffer size even more), and eventually resume_writing() is called once when the buffer size reaches the low-water mark.

Note

If the buffer size equals the high-water mark, pause_writing() is not called – it must go strictly over. Conversely, resume_writing() is called when the buffer size is equal or lower than the low-water mark. These end conditions are important to ensure that things go as expected when either mark is zero.

18.5.4. Transports

Transports are classed provided by asyncio in order to abstract various kinds of communication channels. You generally won’t instantiate a transport yourself; instead, you will call a EventLoop method which will create the transport and try to initiate the underlying communication channel, calling you back when it succeeds.

Once the communication channel is established, a transport is always paired with a protocol instance. The protocol can then call the transport’s methods for various purposes.

asyncio currently implements transports for TCP, UDP, SSL, and subprocess pipes. The methods available on a transport depend on the transport’s kind.

18.5.4.1. Methods common to all transports

asyncio.close(self)

Close the transport. If the transport has a buffer for outgoing data, buffered data will be flushed asynchronously. No more data will be received. After all buffered data is flushed, the protocol’s connection_lost() method will be called with None as its argument.

asyncio.get_extra_info(name, default=None)

Return optional transport information. name is a string representing the piece of transport-specific information to get, default is the value to return if the information doesn’t exist.

This method allows transport implementations to easily expose channel-specific information.

18.5.4.2. Methods of readable streaming transports

asyncio.pause_reading()

Pause the receiving end of the transport. No data will be passed to the protocol’s data_received() method until meth:resume_reading is called.

asyncio.resume_reading()

Resume the receiving end. The protocol’s data_received() method will be called once again if some data is available for reading.

18.5.4.3. Methods of writable streaming transports

asyncio.write(data)

Write some data bytes to the transport.

This method does not block; it buffers the data and arranges for it to be sent out asynchronously.

asyncio.writelines(list_of_data)

Write a list (or any iterable) of data bytes to the transport. This is functionally equivalent to calling write() on each element yielded by the iterable, but may be implemented more efficiently.

asyncio.write_eof()

Close the write end of the transport after flushing buffered data. Data may still be received.

This method can raise NotImplementedError if the transport (e.g. SSL) doesn’t support half-closes.

asyncio.can_write_eof()

Return True if the transport supports write_eof(), False if not.

asyncio.abort()

Close the transport immediately, without waiting for pending operations to complete. Buffered data will be lost. No more data will be received. The protocol’s connection_lost() method will eventually be called with None as its argument.

asyncio.set_write_buffer_limits(high=None, low=None)

Set the high- and low-water limits for write flow control.

These two values control when call the protocol’s pause_writing() and resume_writing() methods are called. If specified, the low-water limit must be less than or equal to the high-water limit. Neither high nor low can be negative.

The defaults are implementation-specific. If only the high-water limit is given, the low-water limit defaults to a implementation-specific value less than or equal to the high-water limit. Setting high to zero forces low to zero as well, and causes pause_writing() to be called whenever the buffer becomes non-empty. Setting low to zero causes resume_writing() to be called only once the buffer is empty. Use of zero for either limit is generally sub-optimal as it reduces opportunities for doing I/O and computation concurrently.

asyncio.get_write_buffer_size()

Return the current size of the output buffer used by the transport.

18.5.4.4. Methods of datagram transports

asyncio.sendto(data, addr=None)

Send the data bytes to the remote peer given by addr (a transport-dependent target address). If addr is None, the data is sent to the target address given on transport creation.

This method does not block; it buffers the data and arranges for it to be sent out asynchronously.

asyncio.abort()

Close the transport immediately, without waiting for pending operations to complete. Buffered data will be lost. No more data will be received. The protocol’s connection_lost() method will eventually be called with None as its argument.

18.5.4.5. Methods of subprocess transports

asyncio.get_pid()

Return the subprocess process id as an integer.

asyncio.get_returncode()

Return the subprocess returncode as an integer or None if it hasn’t returned, similarly to the subprocess.Popen.returncode attribute.

asyncio.get_pipe_transport(fd)

Return the transport for the communication pipe correspondong to the integer file descriptor fd. The return value can be a readable or writable streaming transport, depending on the fd. If fd doesn’t correspond to a pipe belonging to this transport, None is returned.

asyncio.send_signal(signal)

Send the signal number to the subprocess, as in subprocess.Popen.send_signal().

asyncio.terminate()

Ask the subprocess to stop, as in subprocess.Popen.terminate(). This method is an alias for the close() method.

On POSIX systems, this method sends SIGTERM to the subprocess. On Windows, the Windows API function TerminateProcess() is called to stop the subprocess.

asyncio.kill(self)

Kill the subprocess, as in subprocess.Popen.kill()

On POSIX systems, the function sends SIGKILL to the subprocess. On Windows, this method is an alias for terminate().

18.5.5. Coroutines

18.5.6. Synchronization primitives

18.5.7. Examples

A Protocol implementing an echo server:

class EchoServer(asyncio.Protocol):

    TIMEOUT = 5.0

    def timeout(self):
        print('connection timeout, closing.')
        self.transport.close()

    def connection_made(self, transport):
        print('connection made')
        self.transport = transport

        # start 5 seconds timeout timer
        self.h_timeout = asyncio.get_event_loop().call_later(
            self.TIMEOUT, self.timeout)

    def data_received(self, data):
        print('data received: ', data.decode())
        self.transport.write(b'Re: ' + data)

        # restart timeout timer
        self.h_timeout.cancel()
        self.h_timeout = asyncio.get_event_loop().call_later(
            self.TIMEOUT, self.timeout)

    def eof_received(self):
        pass

    def connection_lost(self, exc):
        print('connection lost:', exc)
        self.h_timeout.cancel()