2012-12-12 20:35:17 -05:00
|
|
|
|
PEP: 3156
|
2013-09-30 19:15:24 -04:00
|
|
|
|
Title: Asynchronous IO Support Rebooted: the "asyncio" Module
|
2012-12-12 20:35:17 -05:00
|
|
|
|
Version: $Revision$
|
|
|
|
|
Last-Modified: $Date$
|
|
|
|
|
Author: Guido van Rossum <guido@python.org>
|
2013-11-20 13:03:47 -05:00
|
|
|
|
BDFL-Delegate: Antoine Pitrou <antoine@python.org>
|
2022-02-27 17:46:36 -05:00
|
|
|
|
Discussions-To: python-tulip@googlegroups.com
|
2013-11-22 12:35:10 -05:00
|
|
|
|
Status: Final
|
2012-12-12 20:35:17 -05:00
|
|
|
|
Type: Standards Track
|
|
|
|
|
Content-Type: text/x-rst
|
|
|
|
|
Created: 12-Dec-2012
|
2022-08-24 18:40:18 -04:00
|
|
|
|
Python-Version: 3.3
|
2012-12-21 14:34:12 -05:00
|
|
|
|
Post-History: 21-Dec-2012
|
2018-07-10 14:04:15 -04:00
|
|
|
|
Replaces: 3153
|
2013-11-20 13:11:28 -05:00
|
|
|
|
Resolution: https://mail.python.org/pipermail/python-dev/2013-November/130419.html
|
2012-12-12 20:35:17 -05:00
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
|
========
|
|
|
|
|
|
2013-10-23 11:17:05 -04:00
|
|
|
|
This is a proposal for asynchronous I/O in Python 3, starting at
|
2012-12-12 20:35:17 -05:00
|
|
|
|
Python 3.3. Consider this the concrete proposal that is missing from
|
2022-01-21 06:03:51 -05:00
|
|
|
|
:pep:`3153`. The proposal includes a pluggable event loop, transport and
|
2013-10-23 11:17:05 -04:00
|
|
|
|
protocol abstractions similar to those in Twisted, and a higher-level
|
2022-01-21 06:03:51 -05:00
|
|
|
|
scheduler based on ``yield from`` (:pep:`380`). The proposed package
|
2013-10-23 11:17:05 -04:00
|
|
|
|
name is ``asyncio``.
|
2013-09-30 19:15:24 -04:00
|
|
|
|
|
2012-12-12 20:35:17 -05:00
|
|
|
|
|
|
|
|
|
Introduction
|
|
|
|
|
============
|
|
|
|
|
|
2013-10-23 11:17:05 -04:00
|
|
|
|
Status
|
|
|
|
|
------
|
|
|
|
|
|
|
|
|
|
A reference implementation exists under the code name Tulip. The
|
|
|
|
|
Tulip repo is linked from the References section at the end. Packages
|
|
|
|
|
based on this repo will be provided on PyPI (see References) to enable
|
|
|
|
|
using the ``asyncio`` package with Python 3.3 installations.
|
|
|
|
|
|
|
|
|
|
As of October 20th 2013, the ``asyncio`` package has been checked into
|
|
|
|
|
the Python 3.4 repository and released with Python 3.4-alpha-4, with
|
|
|
|
|
"provisional" API status. This is an expression of confidence and
|
|
|
|
|
intended to increase early feedback on the API, and not intended to
|
|
|
|
|
force acceptance of the PEP. The expectation is that the package will
|
|
|
|
|
keep provisional status in Python 3.4 and progress to final status in
|
|
|
|
|
Python 3.5. Development continues to occur primarily in the Tulip
|
2013-10-31 18:48:48 -04:00
|
|
|
|
repo, with changes occasionally merged into the CPython repo.
|
2013-10-23 11:17:05 -04:00
|
|
|
|
|
|
|
|
|
Dependencies
|
|
|
|
|
------------
|
|
|
|
|
|
|
|
|
|
Python 3.3 is required for many of the proposed features. The
|
|
|
|
|
reference implementation (Tulip) requires no new language or standard
|
|
|
|
|
library features beyond Python 3.3, no third-party modules or
|
2013-10-31 18:48:48 -04:00
|
|
|
|
packages, and no C code, except for the (optional) IOCP support on
|
2013-10-23 11:17:05 -04:00
|
|
|
|
Windows.
|
|
|
|
|
|
|
|
|
|
Module Namespace
|
|
|
|
|
----------------
|
|
|
|
|
|
|
|
|
|
The specification here lives in a new top-level package, ``asyncio``.
|
|
|
|
|
Different components live in separate submodules of the package. The
|
|
|
|
|
package will import common APIs from their respective submodules and
|
|
|
|
|
make them available as package attributes (similar to the way the
|
|
|
|
|
email package works). For such common APIs, the name of the submodule
|
|
|
|
|
that actually defines them is not part of the specification. Less
|
|
|
|
|
common APIs may have to explicitly be imported from their respective
|
|
|
|
|
submodule, and in this case the submodule name is part of the
|
|
|
|
|
specification.
|
|
|
|
|
|
|
|
|
|
Classes and functions defined without a submodule name are assumed to
|
|
|
|
|
live in the namespace of the top-level package. (But do not confuse
|
|
|
|
|
these with methods of various classes, which for brevity are also used
|
|
|
|
|
without a namespace prefix in certain contexts.)
|
|
|
|
|
|
|
|
|
|
Interoperability
|
|
|
|
|
----------------
|
|
|
|
|
|
2012-12-12 21:30:32 -05:00
|
|
|
|
The event loop is the place where most interoperability occurs. It
|
|
|
|
|
should be easy for (Python 3.3 ports of) frameworks like Twisted,
|
2013-03-24 16:34:39 -04:00
|
|
|
|
Tornado, or even gevents to either adapt the default event loop
|
2013-10-23 11:17:05 -04:00
|
|
|
|
implementation to their needs using a lightweight adapter or proxy, or
|
2012-12-12 21:30:32 -05:00
|
|
|
|
to replace the default event loop implementation with an adaptation of
|
|
|
|
|
their own event loop implementation. (Some frameworks, like Twisted,
|
|
|
|
|
have multiple event loop implementations. This should not be a
|
|
|
|
|
problem since these all have the same interface.)
|
|
|
|
|
|
2013-03-24 16:34:39 -04:00
|
|
|
|
In most cases it should be possible for two different third-party
|
|
|
|
|
frameworks to interoperate, either by sharing the default event loop
|
|
|
|
|
implementation (each using its own adapter), or by sharing the event
|
|
|
|
|
loop implementation of either framework. In the latter case two
|
|
|
|
|
levels of adaptation would occur (from framework A's event loop to the
|
|
|
|
|
standard event loop interface, and from there to framework B's event
|
|
|
|
|
loop). Which event loop implementation is used should be under
|
|
|
|
|
control of the main program (though a default policy for event loop
|
|
|
|
|
selection is provided).
|
|
|
|
|
|
|
|
|
|
For this interoperability to be effective, the preferred direction of
|
|
|
|
|
adaptation in third party frameworks is to keep the default event loop
|
|
|
|
|
and adapt it to the framework's API. Ideally all third party
|
|
|
|
|
frameworks would give up their own event loop implementation in favor
|
|
|
|
|
of the standard implementation. But not all frameworks may be
|
|
|
|
|
satisfied with the functionality provided by the standard
|
|
|
|
|
implementation.
|
|
|
|
|
|
|
|
|
|
In order to support both directions of adaptation, two separate APIs
|
2013-10-31 18:48:48 -04:00
|
|
|
|
are specified:
|
2012-12-12 21:30:32 -05:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- An interface for managing the current event loop
|
|
|
|
|
- The interface of a conforming event loop
|
2012-12-12 21:30:32 -05:00
|
|
|
|
|
|
|
|
|
An event loop implementation may provide additional methods and
|
2013-10-23 11:17:05 -04:00
|
|
|
|
guarantees, as long as these are called out in the documentation as
|
|
|
|
|
non-standard. An event loop implementation may also leave certain
|
|
|
|
|
methods unimplemented if they cannot be implemented in the given
|
|
|
|
|
environment; however, such deviations from the standard API should be
|
|
|
|
|
considered only as a last resort, and only if the platform or
|
2013-10-31 18:48:48 -04:00
|
|
|
|
environment forces the issue. (An example would be a platform where
|
|
|
|
|
there is a system event loop that cannot be started or stopped; see
|
|
|
|
|
"Embedded Event Loops" below.)
|
2013-10-23 11:17:05 -04:00
|
|
|
|
|
2016-11-03 18:21:23 -04:00
|
|
|
|
The event loop API does not depend on ``await/yield from``. Rather, it uses
|
2013-10-23 11:17:05 -04:00
|
|
|
|
a combination of callbacks, additional interfaces (transports and
|
2012-12-12 21:30:32 -05:00
|
|
|
|
protocols), and Futures. The latter are similar to those defined in
|
2022-01-21 06:03:51 -05:00
|
|
|
|
:pep:`3148`, but have a different implementation and are not tied to
|
2013-10-31 18:48:48 -04:00
|
|
|
|
threads. In particular, the ``result()`` method raises an exception
|
|
|
|
|
instead of blocking when a result is not yet ready; the user is
|
2016-11-03 18:21:23 -04:00
|
|
|
|
expected to use callbacks (or ``await/yield from``) to wait for the result.
|
2012-12-12 21:30:32 -05:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
All event loop methods specified as returning a coroutine are allowed
|
2013-10-23 11:17:05 -04:00
|
|
|
|
to return either a Future or a coroutine, at the implementation's
|
|
|
|
|
choice (the standard implementation always returns coroutines). All
|
|
|
|
|
event loop methods documented as accepting coroutine arguments *must*
|
|
|
|
|
accept both Futures and coroutines for such arguments. (A convenience
|
2016-11-03 18:21:23 -04:00
|
|
|
|
function, ``ensure_future()``, exists to convert an argument that is either a
|
2013-11-11 09:01:17 -05:00
|
|
|
|
coroutine or a Future into a Future.)
|
2013-10-23 11:17:05 -04:00
|
|
|
|
|
2012-12-12 21:30:32 -05:00
|
|
|
|
For users (like myself) who don't like using callbacks, a scheduler is
|
|
|
|
|
provided for writing asynchronous I/O code as coroutines using the PEP
|
2022-01-21 06:03:51 -05:00
|
|
|
|
380 ``yield from`` or :pep:`492` ``await`` expressions.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
The scheduler is not pluggable;
|
2013-10-23 11:17:05 -04:00
|
|
|
|
pluggability occurs at the event loop level, and the standard
|
|
|
|
|
scheduler implementation should work with any conforming event loop
|
|
|
|
|
implementation. (In fact this is an important litmus test for
|
|
|
|
|
conforming implementations.)
|
2012-12-12 21:30:32 -05:00
|
|
|
|
|
|
|
|
|
For interoperability between code written using coroutines and other
|
2013-10-23 11:17:05 -04:00
|
|
|
|
async frameworks, the scheduler defines a Task class that behaves like a
|
2012-12-12 21:30:32 -05:00
|
|
|
|
Future. A framework that interoperates at the event loop level can
|
|
|
|
|
wait for a Future to complete by adding a callback to the Future.
|
|
|
|
|
Likewise, the scheduler offers an operation to suspend a coroutine
|
|
|
|
|
until a callback is called.
|
|
|
|
|
|
2016-11-03 18:19:44 -04:00
|
|
|
|
If such a framework cannot use the Future and Task classes as-is, it
|
|
|
|
|
may reimplement the ``loop.create_future()`` and
|
|
|
|
|
``loop.create_task()`` methods. These should return objects
|
|
|
|
|
implementing (a superset of) the Future/Task interfaces.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
2016-11-03 18:19:44 -04:00
|
|
|
|
A less ambitious framework may just call the
|
|
|
|
|
``loop.set_task_factory()`` to replace the Task class without
|
|
|
|
|
implementing its own event loop.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
2013-10-23 11:17:05 -04:00
|
|
|
|
The event loop API provides limited interoperability with threads:
|
2022-01-21 06:03:51 -05:00
|
|
|
|
there is an API to submit a function to an executor (see :pep:`3148`)
|
2013-10-23 11:17:05 -04:00
|
|
|
|
which returns a Future that is compatible with the event loop, and
|
|
|
|
|
there is a method to schedule a callback with an event loop from
|
|
|
|
|
another thread in a thread-safe manner.
|
2012-12-12 21:30:32 -05:00
|
|
|
|
|
2013-10-23 11:17:05 -04:00
|
|
|
|
Transports and Protocols
|
|
|
|
|
------------------------
|
2013-03-18 18:43:00 -04:00
|
|
|
|
|
|
|
|
|
For those not familiar with Twisted, a quick explanation of the
|
2013-10-23 11:17:05 -04:00
|
|
|
|
relationship between transports and protocols is in order. At the
|
2013-03-18 18:43:00 -04:00
|
|
|
|
highest level, the transport is concerned with *how* bytes are
|
|
|
|
|
transmitted, while the protocol determines *which* bytes to transmit
|
2013-04-26 17:16:59 -04:00
|
|
|
|
(and to some extent when).
|
|
|
|
|
|
2013-10-23 11:17:05 -04:00
|
|
|
|
A different way of saying the same thing: a transport is an
|
|
|
|
|
abstraction for a socket (or similar I/O endpoint) while a protocol is
|
|
|
|
|
an abstraction for an application, from the transport's point of view.
|
|
|
|
|
|
|
|
|
|
Yet another view is simply that the transport and protocol interfaces
|
|
|
|
|
*together* define an abstract interface for using network I/O and
|
|
|
|
|
interprocess I/O.
|
|
|
|
|
|
|
|
|
|
There is almost always a 1:1 relationship between transport and
|
|
|
|
|
protocol objects: the protocol calls transport methods to send data,
|
|
|
|
|
while the transport calls protocol methods to pass it data that has
|
2017-02-08 21:44:15 -05:00
|
|
|
|
been received. Neither transport nor protocol methods "block" -- they
|
2013-10-23 11:17:05 -04:00
|
|
|
|
set events into motion and then return.
|
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
The most common type of transport is a bidirectional stream transport.
|
2013-10-23 11:17:05 -04:00
|
|
|
|
It represents a pair of buffered streams (one in each direction) that
|
|
|
|
|
each transmit a sequence of bytes. The most common example of a
|
2013-04-26 17:16:59 -04:00
|
|
|
|
bidirectional stream transport is probably a TCP connection. Another
|
2013-10-24 11:37:55 -04:00
|
|
|
|
common example is an SSL/TLS connection. But there are some other things
|
2013-04-26 17:16:59 -04:00
|
|
|
|
that can be viewed this way, for example an SSH session or a pair of
|
|
|
|
|
UNIX pipes. Typically there aren't many different transport
|
|
|
|
|
implementations, and most of them come with the event loop
|
2013-10-31 18:48:48 -04:00
|
|
|
|
implementation. However, there is no requirement that all transports
|
|
|
|
|
must be created by calling an event loop method: a third party module
|
|
|
|
|
may well implement a new transport and provide a constructor or
|
|
|
|
|
factory function for it that simply takes an event loop as an argument
|
|
|
|
|
or calls ``get_event_loop()``.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
2013-10-23 11:17:05 -04:00
|
|
|
|
Note that transports don't need to use sockets, not even if they use
|
|
|
|
|
TCP -- sockets are a platform-specific implementation detail.
|
|
|
|
|
|
|
|
|
|
A bidirectional stream transport has two "ends": one end talks to
|
2013-04-26 17:16:59 -04:00
|
|
|
|
the network (or another process, or whatever low-level interface it
|
2013-10-23 11:17:05 -04:00
|
|
|
|
wraps), and the other end talks to the protocol. The former uses
|
2013-04-26 17:16:59 -04:00
|
|
|
|
whatever API is necessary to implement the transport; but the
|
|
|
|
|
interface between transport and protocol is standardized by this PEP.
|
2013-03-18 18:43:00 -04:00
|
|
|
|
|
2013-10-23 11:17:05 -04:00
|
|
|
|
A protocol can represent some kind of "application-level" protocol
|
|
|
|
|
such as HTTP or SMTP; it can also implement an abstraction shared by
|
|
|
|
|
multiple protocols, or a whole application. A protocol's primary
|
|
|
|
|
interface is with the transport. While some popular protocols (and
|
|
|
|
|
other abstractions) may have standard implementations, often
|
|
|
|
|
applications implement custom protocols. It also makes sense to have
|
|
|
|
|
libraries of useful third party protocol implementations that can be
|
|
|
|
|
downloaded and installed from PyPI.
|
2013-03-18 18:43:00 -04:00
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
There general notion of transport and protocol includes other
|
|
|
|
|
interfaces, where the transport wraps some other communication
|
|
|
|
|
abstraction. Examples include interfaces for sending and receiving
|
|
|
|
|
datagrams (e.g. UDP), or a subprocess manager. The separation of
|
|
|
|
|
concerns is the same as for bidirectional stream transports and
|
|
|
|
|
protocols, but the specific interface between transport and protocol
|
|
|
|
|
is different in each case.
|
2013-03-18 18:43:00 -04:00
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
Details of the interfaces defined by the various standard types of
|
|
|
|
|
transports and protocols are given later.
|
2013-03-18 18:43:00 -04:00
|
|
|
|
|
2013-04-30 00:24:46 -04:00
|
|
|
|
|
|
|
|
|
Event Loop Interface Specification
|
|
|
|
|
==================================
|
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
Event Loop Policy: Getting and Setting the Current Event Loop
|
|
|
|
|
-------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
Event loop management is controlled by an event loop policy, which is
|
2013-08-09 20:27:42 -04:00
|
|
|
|
a global (per-process) object. There is a default policy, and an API
|
|
|
|
|
to change the policy. A policy defines the notion of context; a
|
|
|
|
|
policy manages a separate event loop per context. The default
|
|
|
|
|
policy's notion of context is defined as the current thread.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
Certain platforms or programming frameworks may change the default
|
|
|
|
|
policy to something more suitable to the expectations of the users of
|
|
|
|
|
that platform or framework. Such platforms or frameworks must
|
|
|
|
|
document their policy and at what point during their initialization
|
2013-08-09 20:27:42 -04:00
|
|
|
|
sequence the policy is set, in order to avoid undefined behavior when
|
2013-04-26 17:16:59 -04:00
|
|
|
|
multiple active frameworks want to override the default policy.
|
2013-10-31 18:48:48 -04:00
|
|
|
|
(See also "Embedded Event Loops" below.)
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
2013-08-09 20:27:42 -04:00
|
|
|
|
To get the event loop for current context, use ``get_event_loop()``.
|
|
|
|
|
This returns an event loop object implementing the interface specified
|
|
|
|
|
below, or raises an exception in case no event loop has been set for
|
|
|
|
|
the current context and the current policy does not specify to create
|
|
|
|
|
one. It should never return ``None``.
|
|
|
|
|
|
|
|
|
|
To set the event loop for the current context, use
|
|
|
|
|
``set_event_loop(event_loop)``, where ``event_loop`` is an event loop
|
2013-10-23 11:17:05 -04:00
|
|
|
|
object, i.e. an instance of ``AbstractEventLoop``, or ``None``.
|
|
|
|
|
It is okay to set the current event loop to ``None``, in
|
2013-08-09 20:27:42 -04:00
|
|
|
|
which case subsequent calls to ``get_event_loop()`` will raise an
|
|
|
|
|
exception. This is useful for testing code that should not depend on
|
|
|
|
|
the existence of a default event loop.
|
|
|
|
|
|
|
|
|
|
It is expected that ``get_event_loop()`` returns a different event
|
|
|
|
|
loop object depending on the context (in fact, this is the definition
|
|
|
|
|
of context). It may create a new event loop object if none is set and
|
|
|
|
|
creation is allowed by the policy. The default policy will create a
|
2013-10-18 12:35:37 -04:00
|
|
|
|
new event loop only in the main thread (as defined by threading.py,
|
|
|
|
|
which uses a special subclass for the main thread), and only if
|
2013-08-09 20:27:42 -04:00
|
|
|
|
``get_event_loop()`` is called before ``set_event_loop()`` is ever
|
|
|
|
|
called. (To reset this state, reset the policy.) In other threads an
|
|
|
|
|
event loop must be explicitly set. Other policies may behave
|
|
|
|
|
differently. Event loop by the default policy creation is lazy;
|
|
|
|
|
i.e. the first call to ``get_event_loop()`` creates an event loop
|
|
|
|
|
instance if necessary and specified by the current policy.
|
2012-12-12 23:26:20 -05:00
|
|
|
|
|
2012-12-16 17:26:48 -05:00
|
|
|
|
For the benefit of unit tests and other special cases there's a third
|
2013-01-08 18:45:25 -05:00
|
|
|
|
policy function: ``new_event_loop()``, which creates and returns a new
|
2013-04-26 17:16:59 -04:00
|
|
|
|
event loop object according to the policy's default rules. To make
|
2013-08-09 20:27:42 -04:00
|
|
|
|
this the current event loop, you must call ``set_event_loop()`` with
|
|
|
|
|
it.
|
2012-12-16 17:26:48 -05:00
|
|
|
|
|
2013-10-23 11:17:05 -04:00
|
|
|
|
To change the event loop policy, call
|
|
|
|
|
``set_event_loop_policy(policy)``, where ``policy`` is an event loop
|
|
|
|
|
policy object or ``None``. If not ``None``, the policy object must be
|
|
|
|
|
an instance of ``AbstractEventLoopPolicy`` that defines methods
|
|
|
|
|
``get_event_loop()``, ``set_event_loop(loop)`` and
|
2013-04-26 17:16:59 -04:00
|
|
|
|
``new_event_loop()``, all behaving like the functions described above.
|
2013-10-23 11:17:05 -04:00
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
Passing a policy value of ``None`` restores the default event loop
|
|
|
|
|
policy (overriding the alternate default set by the platform or
|
|
|
|
|
framework). The default event loop policy is an instance of the class
|
|
|
|
|
``DefaultEventLoopPolicy``. The current event loop policy object can
|
2013-10-23 11:17:05 -04:00
|
|
|
|
be retrieved by calling ``get_event_loop_policy()``.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
2019-06-25 00:58:50 -04:00
|
|
|
|
TBD: describe child watchers and UNIX quirks for subprocess processing.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
2013-11-25 01:43:36 -05:00
|
|
|
|
Passing an Event Loop Around Explicitly
|
|
|
|
|
'''''''''''''''''''''''''''''''''''''''
|
|
|
|
|
|
|
|
|
|
It is possible to write code that uses an event loop without relying
|
|
|
|
|
on a global or per-thread default event loop. For this purpose, all
|
|
|
|
|
APIs that need access to the current event loop (and aren't methods on
|
|
|
|
|
an event class) take an optional keyword argument named ``loop``. If
|
|
|
|
|
this argument is ``None`` or unspecified, such APIs will call
|
|
|
|
|
``get_event_loop()`` to get the default event loop, but if the
|
|
|
|
|
``loop`` keyword argument is set to an event loop object, they will
|
|
|
|
|
use that event loop, and pass it along to any other such APIs they
|
|
|
|
|
call. For example, ``Future(loop=my_loop)`` will create a Future tied
|
|
|
|
|
to the event loop ``my_loop``. When the default current event is
|
|
|
|
|
``None``, the ``loop`` keyword argument is effectively mandatory.
|
|
|
|
|
|
|
|
|
|
Note that an explicitly passed event loop must still belong to the
|
|
|
|
|
current thread; the ``loop`` keyword argument does not magically
|
|
|
|
|
change the constraints on how an event loop can be used.
|
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
Specifying Times
|
|
|
|
|
----------------
|
|
|
|
|
|
|
|
|
|
As usual in Python, all timeouts, intervals and delays are measured in
|
|
|
|
|
seconds, and may be ints or floats. However, absolute times are not
|
|
|
|
|
specified as POSIX timestamps. The accuracy, precision and epoch of
|
|
|
|
|
the clock are up to the implementation.
|
|
|
|
|
|
|
|
|
|
The default implementation uses ``time.monotonic()``. Books could be
|
|
|
|
|
written about the implications of this choice. Better read the docs
|
2014-02-23 00:42:54 -05:00
|
|
|
|
for the standard library ``time`` module.
|
2013-10-31 18:48:48 -04:00
|
|
|
|
|
|
|
|
|
Embedded Event Loops
|
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
|
|
On some platforms an event loop is provided by the system. Such a
|
|
|
|
|
loop may already be running when the user code starts, and there may
|
|
|
|
|
be no way to stop or close it without exiting from the program. In
|
|
|
|
|
this case, the methods for starting, stopping and closing the event
|
|
|
|
|
loop may not be implementable, and ``is_running()`` may always return
|
|
|
|
|
``True``.
|
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
Event Loop Classes
|
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
There is no actual class named ``EventLoop``. There is an
|
|
|
|
|
``AbstractEventLoop`` class which defines all the methods without
|
|
|
|
|
implementations, and serves primarily as documentation. The following
|
|
|
|
|
concrete classes are defined:
|
|
|
|
|
|
|
|
|
|
- ``SelectorEventLoop`` is a concrete implementation of the full API
|
2013-10-18 12:35:37 -04:00
|
|
|
|
based on the ``selectors`` module (new in Python 3.4). The
|
|
|
|
|
constructor takes one optional argument, a ``selectors.Selector``
|
|
|
|
|
object. By default an instance of ``selectors.DefaultSelector`` is
|
|
|
|
|
created and used.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
- ``ProactorEventLoop`` is a concrete implementation of the API except
|
|
|
|
|
for the I/O event handling and signal handling methods. It is only
|
|
|
|
|
defined on Windows (or on other platforms which support a similar
|
|
|
|
|
API for "overlapped I/O"). The constructor takes one optional
|
|
|
|
|
argument, a ``Proactor`` object. By default an instance of
|
|
|
|
|
``IocpProactor`` is created and used. (The ``IocpProactor`` class
|
2013-10-31 18:48:48 -04:00
|
|
|
|
is not specified by this PEP; it is just an implementation
|
2013-10-24 11:37:55 -04:00
|
|
|
|
detail of the ``ProactorEventLoop`` class.)
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
Event Loop Methods Overview
|
|
|
|
|
---------------------------
|
|
|
|
|
|
|
|
|
|
The methods of a conforming event loop are grouped into several
|
2013-10-31 18:48:48 -04:00
|
|
|
|
categories. The first set of categories must be supported by all
|
|
|
|
|
conforming event loop implementations, with the exception that
|
|
|
|
|
embedded event loops may not implement the methods for starting,
|
|
|
|
|
stopping and closing. (However, a partially-conforming event loop is
|
|
|
|
|
still better than nothing. :-)
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- Starting, stopping and closing: ``run_forever()``,
|
2016-11-03 18:21:23 -04:00
|
|
|
|
``run_until_complete()``, ``stop()``, ``is_running()``, ``close()``,
|
|
|
|
|
``is_closed()``.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- Basic and timed callbacks: ``call_soon()``, ``call_later()``,
|
|
|
|
|
``call_at()``, ``time()``.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
- Thread interaction: ``call_soon_threadsafe()``,
|
2013-05-21 11:34:55 -04:00
|
|
|
|
``run_in_executor()``, ``set_default_executor()``.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
- Internet name lookups: ``getaddrinfo()``, ``getnameinfo()``.
|
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- Internet connections: ``create_connection()``, ``create_server()``,
|
|
|
|
|
``create_datagram_endpoint()``.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
- Wrapped socket methods: ``sock_recv()``, ``sock_sendall()``,
|
|
|
|
|
``sock_connect()``, ``sock_accept()``.
|
|
|
|
|
|
2016-11-03 18:19:44 -04:00
|
|
|
|
- Tasks and futures support: ``create_future()``, ``create_task()``,
|
2016-11-03 18:21:23 -04:00
|
|
|
|
``set_task_factory()``, ``get_task_factory()``.
|
|
|
|
|
|
|
|
|
|
- Error handling: ``get_exception_handler()``, ``set_exception_handler()``,
|
|
|
|
|
``default_exception_handler()``, ``call_exception_handler()``.
|
|
|
|
|
|
|
|
|
|
- Debug mode: ``get_debug()``, ``set_debug()``.
|
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
The second set of categories *may* be supported by conforming event
|
|
|
|
|
loop implementations. If not supported, they will raise
|
2013-10-31 18:48:48 -04:00
|
|
|
|
``NotImplementedError``. (In the default implementation,
|
2013-04-26 17:16:59 -04:00
|
|
|
|
``SelectorEventLoop`` on UNIX systems supports all of these;
|
|
|
|
|
``SelectorEventLoop`` on Windows supports the I/O event handling
|
2013-10-31 18:48:48 -04:00
|
|
|
|
category; ``ProactorEventLoop`` on Windows supports the pipes and
|
|
|
|
|
subprocess category.)
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
- I/O callbacks: ``add_reader()``, ``remove_reader()``,
|
|
|
|
|
``add_writer()``, ``remove_writer()``.
|
|
|
|
|
|
|
|
|
|
- Pipes and subprocesses: ``connect_read_pipe()``,
|
2013-10-31 18:48:48 -04:00
|
|
|
|
``connect_write_pipe()``, ``subprocess_shell()``,
|
|
|
|
|
``subprocess_exec()``.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
- Signal callbacks: ``add_signal_handler()``,
|
|
|
|
|
``remove_signal_handler()``.
|
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
Event Loop Methods
|
|
|
|
|
------------------
|
2013-05-02 17:11:08 -04:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
Starting, Stopping and Closing
|
|
|
|
|
''''''''''''''''''''''''''''''
|
2012-12-13 01:47:17 -05:00
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
An (unclosed) event loop can be in one of two states: running or
|
|
|
|
|
stopped. These methods deal with starting and stopping an event loop:
|
2012-12-13 01:47:17 -05:00
|
|
|
|
|
2013-04-24 19:59:39 -04:00
|
|
|
|
- ``run_forever()``. Runs the event loop until ``stop()`` is called.
|
|
|
|
|
This cannot be called when the event loop is already running. (This
|
|
|
|
|
has a long name in part to avoid confusion with earlier versions of
|
|
|
|
|
this PEP, where ``run()`` had different behavior, in part because
|
|
|
|
|
there are already too many APIs that have a method named ``run()``,
|
|
|
|
|
and in part because there shouldn't be many places where this is
|
2013-04-26 06:31:02 -04:00
|
|
|
|
called anyway.)
|
2012-12-20 16:48:24 -05:00
|
|
|
|
|
2013-09-08 22:11:55 -04:00
|
|
|
|
- ``run_until_complete(future)``. Runs the event loop until the
|
|
|
|
|
Future is done. If the Future is done, its result is returned, or
|
|
|
|
|
its exception is raised. This cannot be called when the event loop
|
|
|
|
|
is already running.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
The method creates a new ``Task`` object if the
|
|
|
|
|
parameter is a coroutine.
|
2012-12-13 13:30:42 -05:00
|
|
|
|
|
2013-01-08 22:39:28 -05:00
|
|
|
|
- ``stop()``. Stops the event loop as soon as it is convenient. It
|
2013-04-26 17:16:59 -04:00
|
|
|
|
is fine to restart the loop with ``run_forever()`` or
|
|
|
|
|
``run_until_complete()`` subsequently; no scheduled callbacks will
|
|
|
|
|
be lost if this is done. Note: ``stop()`` returns normally and the
|
|
|
|
|
current callback is allowed to continue. How soon after this point
|
|
|
|
|
the event loop stops is up to the implementation, but the intention
|
|
|
|
|
is to stop short of polling for I/O, and not to run any callbacks
|
|
|
|
|
scheduled in the future; the major freedom an implementation has is
|
|
|
|
|
how much of the "ready queue" (callbacks already scheduled with
|
|
|
|
|
``call_soon()``) it processes before stopping.
|
|
|
|
|
|
|
|
|
|
- ``is_running()``. Returns ``True`` if the event loop is currently
|
2013-10-31 18:48:48 -04:00
|
|
|
|
running, ``False`` if it is stopped.
|
|
|
|
|
|
|
|
|
|
- ``close()``. Closes the event loop, releasing any resources it may
|
|
|
|
|
hold, such as the file descriptor used by ``epoll()`` or
|
|
|
|
|
``kqueue()``, and the default executor. This should not be called
|
|
|
|
|
while the event loop is running. After it has been called the event
|
|
|
|
|
loop should not be used again. It may be called multiple times;
|
|
|
|
|
subsequent calls are no-ops.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
2016-11-03 18:21:23 -04:00
|
|
|
|
- ``is_closed()``. Returns ``True`` if the event loop is closed,
|
2016-11-03 18:19:44 -04:00
|
|
|
|
``False`` otherwise. (Primarily intended for error reporting;
|
|
|
|
|
please don't implement functionality based on this method.)
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
Basic Callbacks
|
|
|
|
|
'''''''''''''''
|
2013-01-08 22:39:28 -05:00
|
|
|
|
|
2013-10-01 11:37:33 -04:00
|
|
|
|
Callbacks associated with the same event loop are strictly serialized:
|
|
|
|
|
one callback must finish before the next one will be called. This is
|
|
|
|
|
an important guarantee: when two or more callbacks use or modify
|
2013-12-26 18:12:56 -05:00
|
|
|
|
shared state, each callback is guaranteed that while it is running, the
|
2013-10-01 11:37:33 -04:00
|
|
|
|
shared state isn't changed by another callback.
|
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
- ``call_soon(callback, *args)``. This schedules a callback to be
|
2013-10-31 18:48:48 -04:00
|
|
|
|
called as soon as possible. Returns a ``Handle`` (see below)
|
|
|
|
|
representing the callback, whose ``cancel()`` method can be used to
|
|
|
|
|
cancel the callback. It guarantees that callbacks are called in the
|
|
|
|
|
order in which they were scheduled.
|
2013-01-08 22:39:28 -05:00
|
|
|
|
|
2012-12-17 12:55:32 -05:00
|
|
|
|
- ``call_later(delay, callback, *args)``. Arrange for
|
|
|
|
|
``callback(*args)`` to be called approximately ``delay`` seconds in
|
2013-10-31 18:48:48 -04:00
|
|
|
|
the future, once, unless cancelled. Returns a ``Handle`` representing
|
2013-04-29 14:49:58 -04:00
|
|
|
|
the callback, whose ``cancel()`` method can be used to cancel the
|
2013-05-02 17:11:08 -04:00
|
|
|
|
callback. Callbacks scheduled in the past or at exactly the same
|
|
|
|
|
time will be called in an undefined order.
|
|
|
|
|
|
|
|
|
|
- ``call_at(when, callback, *args)``. This is like ``call_later()``,
|
2013-05-02 19:35:23 -04:00
|
|
|
|
but the time is expressed as an absolute time. Returns a similar
|
2013-10-31 18:48:48 -04:00
|
|
|
|
``Handle``. There is a simple equivalency: ``loop.call_later(delay,
|
2013-05-02 19:35:23 -04:00
|
|
|
|
callback, *args)`` is the same as ``loop.call_at(loop.time() +
|
|
|
|
|
delay, callback, *args)``.
|
2013-05-02 17:11:08 -04:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- ``time()``. Returns the current time according to the event loop's
|
|
|
|
|
clock. This may be ``time.time()`` or ``time.monotonic()`` or some
|
|
|
|
|
other system-specific clock, but it must return a float expressing
|
|
|
|
|
the time in units of approximately one second since some epoch.
|
2022-01-21 06:03:51 -05:00
|
|
|
|
(No clock is perfect -- see :pep:`418`.)
|
2013-10-31 18:48:48 -04:00
|
|
|
|
|
2013-05-02 17:11:08 -04:00
|
|
|
|
Note: A previous version of this PEP defined a method named
|
|
|
|
|
``call_repeatedly()``, which promised to call a callback at regular
|
|
|
|
|
intervals. This has been withdrawn because the design of such a
|
|
|
|
|
function is overspecified. On the one hand, a simple timer loop can
|
|
|
|
|
easily be emulated using a callback that reschedules itself using
|
|
|
|
|
``call_later()``; it is also easy to write coroutine containing a loop
|
|
|
|
|
and a ``sleep()`` call (a toplevel function in the module, see below).
|
|
|
|
|
On the other hand, due to the complexities of accurate timekeeping
|
2022-01-21 06:03:51 -05:00
|
|
|
|
there are many traps and pitfalls here for the unaware (see :pep:`418`),
|
2013-05-02 17:11:08 -04:00
|
|
|
|
and different use cases require different behavior in edge cases. It
|
|
|
|
|
is impossible to offer an API for this purpose that is bullet-proof in
|
|
|
|
|
all cases, so it is deemed better to let application designers decide
|
|
|
|
|
for themselves what kind of timer loop to implement.
|
2013-03-24 16:34:39 -04:00
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
Thread interaction
|
|
|
|
|
''''''''''''''''''
|
2012-12-13 01:47:17 -05:00
|
|
|
|
|
|
|
|
|
- ``call_soon_threadsafe(callback, *args)``. Like
|
|
|
|
|
``call_soon(callback, *args)``, but when called from another thread
|
|
|
|
|
while the event loop is blocked waiting for I/O, unblocks the event
|
2013-10-31 18:48:48 -04:00
|
|
|
|
loop. Returns a ``Handle``. This is the *only* method that is safe
|
|
|
|
|
to call from another thread. (To schedule a callback for a later
|
|
|
|
|
time in a threadsafe manner, you can use
|
|
|
|
|
``loop.call_soon_threadsafe(loop.call_later, when, callback,
|
|
|
|
|
*args)``.) Note: this is not safe to call from a signal handler
|
|
|
|
|
(since it may use locks). In fact, no API is signal-safe; if you
|
|
|
|
|
want to handle signals, use ``add_signal_handler()`` described
|
|
|
|
|
below.
|
2012-12-13 14:05:52 -05:00
|
|
|
|
|
2013-01-08 22:39:28 -05:00
|
|
|
|
- ``run_in_executor(executor, callback, *args)``. Arrange to call
|
2022-01-21 06:03:51 -05:00
|
|
|
|
``callback(*args)`` in an executor (see :pep:`3148`). Returns an
|
2013-10-31 18:48:48 -04:00
|
|
|
|
``asyncio.Future`` instance whose result on success is the return
|
|
|
|
|
value of that call. This is equivalent to
|
|
|
|
|
``wrap_future(executor.submit(callback, *args))``. If ``executor``
|
|
|
|
|
is ``None``, the default executor set by ``set_default_executor()``
|
|
|
|
|
is used. If no default executor has been set yet, a
|
|
|
|
|
``ThreadPoolExecutor`` with a default number of threads is created
|
|
|
|
|
and set as the default executor. (The default implementation uses
|
|
|
|
|
5 threads in this case.)
|
2012-12-13 14:05:52 -05:00
|
|
|
|
|
2012-12-16 23:01:54 -05:00
|
|
|
|
- ``set_default_executor(executor)``. Set the default executor used
|
2022-01-21 06:03:51 -05:00
|
|
|
|
by ``run_in_executor()``. The argument must be a :pep:`3148`
|
2013-04-26 17:16:59 -04:00
|
|
|
|
``Executor`` instance or ``None``, in order to reset the default
|
|
|
|
|
executor.
|
|
|
|
|
|
2013-05-10 19:09:48 -04:00
|
|
|
|
See also the ``wrap_future()`` function described in the section about
|
|
|
|
|
Futures.
|
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
Internet name lookups
|
|
|
|
|
'''''''''''''''''''''
|
|
|
|
|
|
|
|
|
|
These methods are useful if you want to connect or bind a socket to an
|
|
|
|
|
address without the risk of blocking for the name lookup. They are
|
|
|
|
|
usually called implicitly by ``create_connection()``,
|
2013-10-31 18:48:48 -04:00
|
|
|
|
``create_server()`` or ``create_datagram_endpoint()``.
|
2012-12-16 23:01:54 -05:00
|
|
|
|
|
2012-12-13 14:05:52 -05:00
|
|
|
|
- ``getaddrinfo(host, port, family=0, type=0, proto=0, flags=0)``.
|
|
|
|
|
Similar to the ``socket.getaddrinfo()`` function but returns a
|
|
|
|
|
Future. The Future's result on success will be a list of the same
|
2013-04-26 17:16:59 -04:00
|
|
|
|
format as returned by ``socket.getaddrinfo()``, i.e. a list of
|
|
|
|
|
``(address_family, socket_type, socket_protocol, canonical_name,
|
|
|
|
|
address)`` where ``address`` is a 2-tuple ``(ipv4_address, port)``
|
|
|
|
|
for IPv4 addresses and a 4-tuple ``(ipv4_address, port, flow_info,
|
|
|
|
|
scope_id)`` for IPv6 addresses. If the ``family`` argument is zero
|
|
|
|
|
or unspecified, the list returned may contain a mixture of IPv4 and
|
|
|
|
|
IPv6 addresses; otherwise the addresses returned are constrained by
|
|
|
|
|
the ``family`` value (similar for ``proto`` and ``flags``). The
|
|
|
|
|
default implementation calls ``socket.getaddrinfo()`` using
|
2012-12-13 14:05:52 -05:00
|
|
|
|
``run_in_executor()``, but other implementations may choose to
|
2012-12-21 14:34:12 -05:00
|
|
|
|
implement their own DNS lookup. The optional arguments *must* be
|
|
|
|
|
specified as keyword arguments.
|
2012-12-13 14:05:52 -05:00
|
|
|
|
|
2013-04-30 13:38:12 -04:00
|
|
|
|
Note: implementations are allowed to implement a subset of the full
|
2013-04-29 14:49:58 -04:00
|
|
|
|
socket.getaddrinfo() interface; e.g. they may not support symbolic
|
2013-04-30 13:38:12 -04:00
|
|
|
|
port names, or they may ignore or incompletely implement the
|
|
|
|
|
``type``, ``proto`` and ``flags`` arguments. However, if ``type``
|
|
|
|
|
and ``proto`` are ignored, the argument values passed in should be
|
|
|
|
|
copied unchanged into the return tuples' ``socket_type`` and
|
|
|
|
|
``socket_protocol`` elements. (You can't ignore ``family``, since
|
|
|
|
|
IPv4 and IPv6 addresses must be looked up differently. The only
|
|
|
|
|
permissible values for ``family`` are ``socket.AF_UNSPEC`` (``0``),
|
|
|
|
|
``socket.AF_INET`` and ``socket.AF_INET6``, and the latter only if
|
|
|
|
|
it is defined by the platform.)
|
2013-04-29 14:49:58 -04:00
|
|
|
|
|
2012-12-16 17:26:48 -05:00
|
|
|
|
- ``getnameinfo(sockaddr, flags=0)``. Similar to
|
2012-12-13 14:05:52 -05:00
|
|
|
|
``socket.getnameinfo()`` but returns a Future. The Future's result
|
|
|
|
|
on success will be a tuple ``(host, port)``. Same implementation
|
|
|
|
|
remarks as for ``getaddrinfo()``.
|
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
Internet connections
|
|
|
|
|
''''''''''''''''''''
|
|
|
|
|
|
|
|
|
|
These are the high-level interfaces for managing internet connections.
|
|
|
|
|
Their use is recommended over the corresponding lower-level interfaces
|
|
|
|
|
because they abstract away the differences between selector-based
|
|
|
|
|
and proactor-based event loops.
|
|
|
|
|
|
|
|
|
|
Note that the client and server side of stream connections use the
|
|
|
|
|
same transport and protocol interface. However, datagram endpoints
|
|
|
|
|
use a different transport and protocol interface.
|
|
|
|
|
|
2013-10-18 12:35:37 -04:00
|
|
|
|
- ``create_connection(protocol_factory, host, port, <options>)``.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
Creates a stream connection to a given internet host and port. This
|
|
|
|
|
is a task that is typically called from the client side of the
|
2013-10-31 18:48:48 -04:00
|
|
|
|
connection. It creates an implementation-dependent bidirectional
|
|
|
|
|
stream Transport to represent the connection, then calls
|
2013-04-26 17:16:59 -04:00
|
|
|
|
``protocol_factory()`` to instantiate (or retrieve) the user's
|
|
|
|
|
Protocol implementation, and finally ties the two together. (See
|
|
|
|
|
below for the definitions of Transport and Protocol.) The user's
|
|
|
|
|
Protocol implementation is created or retrieved by calling
|
|
|
|
|
``protocol_factory()`` without arguments(*). The coroutine's result
|
|
|
|
|
on success is the ``(transport, protocol)`` pair; if a failure
|
|
|
|
|
prevents the creation of a successful connection, an appropriate
|
|
|
|
|
exception will be raised. Note that when the coroutine completes,
|
|
|
|
|
the protocol's ``connection_made()`` method has not yet been called;
|
|
|
|
|
that will happen when the connection handshake is complete.
|
2012-12-20 17:41:31 -05:00
|
|
|
|
|
2013-01-05 18:32:00 -05:00
|
|
|
|
(*) There is no requirement that ``protocol_factory`` is a class.
|
|
|
|
|
If your protocol class needs to have specific arguments passed to
|
2013-10-31 18:48:48 -04:00
|
|
|
|
its constructor, you can use ``lambda``.
|
2013-01-17 14:57:00 -05:00
|
|
|
|
You can also pass a trivial ``lambda`` that returns a previously
|
|
|
|
|
constructed Protocol instance.
|
2013-01-05 18:32:00 -05:00
|
|
|
|
|
2013-10-18 12:35:37 -04:00
|
|
|
|
The <options> are all specified using optional keyword arguments:
|
2012-12-20 17:41:31 -05:00
|
|
|
|
|
2013-10-24 11:37:55 -04:00
|
|
|
|
- ``ssl``: Pass ``True`` to create an SSL/TLS transport (by default
|
|
|
|
|
a plain TCP transport is created). Or pass an ``ssl.SSLContext``
|
2013-10-31 18:48:48 -04:00
|
|
|
|
object to override the default SSL context object to be used. If
|
|
|
|
|
a default context is created it is up to the implementation to
|
|
|
|
|
configure reasonable defaults. The reference implementation
|
|
|
|
|
currently uses ``PROTOCOL_SSLv23`` and sets the ``OP_NO_SSLv2``
|
2014-02-24 17:22:46 -05:00
|
|
|
|
option, calls ``set_default_verify_paths()`` and sets ``verify_mode``
|
2013-10-31 18:48:48 -04:00
|
|
|
|
to ``CERT_REQUIRED``. In addition, whenever the context (default
|
2014-02-24 17:22:46 -05:00
|
|
|
|
or otherwise) specifies a ``verify_mode`` of ``CERT_REQUIRED`` or
|
2013-10-24 11:37:55 -04:00
|
|
|
|
``CERT_OPTIONAL``, if a hostname is given, immediately after a
|
|
|
|
|
successful handshake ``ssl.match_hostname(peercert, hostname)`` is
|
2016-07-11 11:14:08 -04:00
|
|
|
|
called, and if this raises an exception the connection is closed.
|
2014-02-24 17:22:46 -05:00
|
|
|
|
(To avoid this behavior, pass in an SSL context that has
|
|
|
|
|
``verify_mode`` set to ``CERT_NONE``. But this means you are not
|
2013-10-24 11:37:55 -04:00
|
|
|
|
secure, and vulnerable to for example man-in-the-middle attacks.)
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
- ``family``, ``proto``, ``flags``: Address family, protocol and
|
|
|
|
|
flags to be passed through to ``getaddrinfo()``. These all
|
|
|
|
|
default to ``0``, which means "not specified". (The socket type
|
|
|
|
|
is always ``SOCK_STREAM``.) If any of these values are not
|
|
|
|
|
specified, the ``getaddrinfo()`` method will choose appropriate
|
|
|
|
|
values. Note: ``proto`` has nothing to do with the high-level
|
|
|
|
|
Protocol concept or the ``protocol_factory`` argument.
|
|
|
|
|
|
|
|
|
|
- ``sock``: An optional socket to be used instead of using the
|
2013-10-31 18:48:48 -04:00
|
|
|
|
``host``, ``port``, ``family``, ``proto`` and ``flags``
|
|
|
|
|
arguments. If this is given, ``host`` and ``port`` must be
|
|
|
|
|
explicitly set to ``None``.
|
2012-12-20 17:41:31 -05:00
|
|
|
|
|
2013-05-10 19:09:48 -04:00
|
|
|
|
- ``local_addr``: If given, a ``(host, port)`` tuple used to bind
|
|
|
|
|
the socket to locally. This is rarely needed but on multi-homed
|
|
|
|
|
servers you occasionally need to force a connection to come from a
|
|
|
|
|
specific address. This is how you would do that. The host and
|
|
|
|
|
port are looked up using ``getaddrinfo()``.
|
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- ``server_hostname``: This is only relevant when using SSL/TLS; it
|
|
|
|
|
should not be used when ``ssl`` is not set. When ``ssl`` is set,
|
|
|
|
|
this sets or overrides the hostname that will be verified. 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``. To disable hostname verification (which is a
|
|
|
|
|
serious security risk) you must pass an empty string here and pass
|
|
|
|
|
an ``ssl.SSLContext`` object whose ``verify_mode`` is set to
|
|
|
|
|
``ssl.CERT_NONE`` as the ``ssl`` argument.
|
|
|
|
|
|
2013-10-23 11:17:05 -04:00
|
|
|
|
- ``create_server(protocol_factory, host, port, <options>)``.
|
|
|
|
|
Enters a serving loop that accepts connections.
|
2013-10-18 12:35:37 -04:00
|
|
|
|
This is a coroutine that completes once the serving loop is set up
|
|
|
|
|
to serve. The return value is a ``Server`` object which can be used
|
2013-10-31 18:48:48 -04:00
|
|
|
|
to stop the serving loop in a controlled fashion (see below).
|
2013-10-18 12:35:37 -04:00
|
|
|
|
Multiple sockets may be bound if the specified address allows
|
2013-10-31 18:48:48 -04:00
|
|
|
|
both IPv4 and IPv6 connections.
|
|
|
|
|
|
|
|
|
|
Each time a connection is accepted,
|
|
|
|
|
``protocol_factory`` is called without arguments(**) to create a
|
|
|
|
|
Protocol, a bidirectional stream Transport is created to represent
|
2012-12-21 14:34:12 -05:00
|
|
|
|
the network side of the connection, and the two are tied together by
|
|
|
|
|
calling ``protocol.connection_made(transport)``.
|
2012-12-14 13:13:20 -05:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
(**) See previous footnote for ``create_connection()``. However, since
|
2013-01-17 14:57:00 -05:00
|
|
|
|
``protocol_factory()`` is called once for each new incoming
|
2013-04-26 17:16:59 -04:00
|
|
|
|
connection, it should return a new Protocol object each time it is
|
|
|
|
|
called.
|
2013-01-05 18:32:00 -05:00
|
|
|
|
|
2013-10-18 12:35:37 -04:00
|
|
|
|
The <options> are all specified using optional keyword arguments:
|
2012-12-14 13:13:20 -05:00
|
|
|
|
|
2013-10-24 11:37:55 -04:00
|
|
|
|
- ``ssl``: Pass an ``ssl.SSLContext`` object (or an object with the
|
|
|
|
|
same interface) to override the default SSL context object to be
|
2013-10-31 18:48:48 -04:00
|
|
|
|
used. (Unlike for ``create_connection()``, passing ``True`` does
|
|
|
|
|
not make sense here -- the ``SSLContext`` object is needed to
|
|
|
|
|
specify the certificate and key.)
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
- ``backlog``: Backlog value to be passed to the ``listen()`` call.
|
2013-10-31 18:48:48 -04:00
|
|
|
|
The default is implementation-dependent; in the default
|
|
|
|
|
implementation the default value is ``100``.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
- ``reuse_address``: Whether to set the ``SO_REUSEADDR`` option on
|
|
|
|
|
the socket. The default is ``True`` on UNIX, ``False`` on
|
|
|
|
|
Windows.
|
|
|
|
|
|
|
|
|
|
- ``family``, ``flags``: Address family and flags to be passed
|
|
|
|
|
through to ``getaddrinfo()``. The family defaults to
|
|
|
|
|
``AF_UNSPEC``; the flags default to ``AI_PASSIVE``. (The socket
|
|
|
|
|
type is always ``SOCK_STREAM``; the socket protocol always set to
|
|
|
|
|
``0``, to let ``getaddrinfo()`` choose.)
|
|
|
|
|
|
|
|
|
|
- ``sock``: An optional socket to be used instead of using the
|
2013-10-31 18:48:48 -04:00
|
|
|
|
``host``, ``port``, ``family`` and ``flags`` arguments. If this
|
|
|
|
|
is given, ``host`` and ``port`` must be explicitly set to ``None``.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
2013-10-18 12:35:37 -04:00
|
|
|
|
- ``create_datagram_endpoint(protocol_factory, local_addr=None,
|
2013-10-31 18:48:48 -04:00
|
|
|
|
remote_addr=None, <options>)``. Creates an endpoint for sending and
|
2013-04-26 17:16:59 -04:00
|
|
|
|
receiving datagrams (typically UDP packets). Because of the nature
|
|
|
|
|
of datagram traffic, there are no separate calls to set up client
|
|
|
|
|
and server side, since usually a single endpoint acts as both client
|
|
|
|
|
and server. This is a coroutine that returns a ``(transport,
|
|
|
|
|
protocol)`` pair on success, or raises an exception on failure. If
|
|
|
|
|
the coroutine returns successfully, the transport will call
|
|
|
|
|
callbacks on the protocol whenever a datagram is received or the
|
|
|
|
|
socket is closed; it is up to the protocol to call methods on the
|
2013-10-31 18:48:48 -04:00
|
|
|
|
protocol to send datagrams. The transport returned is a
|
|
|
|
|
``DatagramTransport``. The protocol returned is a
|
|
|
|
|
``DatagramProtocol``. These are described later.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
Mandatory positional argument:
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
- ``protocol_factory``: A class or factory function that will be
|
2013-10-31 18:48:48 -04:00
|
|
|
|
called exactly once, without arguments, to construct the protocol
|
|
|
|
|
object to be returned. The interface between datagram transport
|
|
|
|
|
and protocol is described below.
|
|
|
|
|
|
|
|
|
|
Optional arguments that may be specified positionally or as keyword
|
|
|
|
|
arguments:
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
- ``local_addr``: An optional tuple indicating the address to which
|
|
|
|
|
the socket will be bound. If given this must be a ``(host,
|
|
|
|
|
port)`` pair. It will be passed to ``getaddrinfo()`` to be
|
|
|
|
|
resolved and the result will be passed to the ``bind()`` method of
|
|
|
|
|
the socket created. If ``getaddrinfo()`` returns more than one
|
|
|
|
|
address, they will be tried in turn. If omitted, no ``bind()``
|
|
|
|
|
call will be made.
|
|
|
|
|
|
|
|
|
|
- ``remote_addr``: An optional tuple indicating the address to which
|
|
|
|
|
the socket will be "connected". (Since there is no such thing as
|
|
|
|
|
a datagram connection, this just specifies a default value for the
|
|
|
|
|
destination address of outgoing datagrams.) If given this must be
|
|
|
|
|
a ``(host, port)`` pair. It will be passed to ``getaddrinfo()``
|
|
|
|
|
to be resolved and the result will be passed to ``sock_connect()``
|
|
|
|
|
together with the socket created. If ``getaddrinfo()`` returns
|
|
|
|
|
more than one address, they will be tried in turn. If omitted,
|
2013-10-31 18:48:48 -04:00
|
|
|
|
no ``sock_connect()`` call will be made.
|
|
|
|
|
|
|
|
|
|
The <options> are all specified using optional keyword arguments:
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
- ``family``, ``proto``, ``flags``: Address family, protocol and
|
|
|
|
|
flags to be passed through to ``getaddrinfo()``. These all
|
|
|
|
|
default to ``0``, which means "not specified". (The socket type
|
|
|
|
|
is always ``SOCK_DGRAM``.) If any of these values are not
|
|
|
|
|
specified, the ``getaddrinfo()`` method will choose appropriate
|
|
|
|
|
values.
|
|
|
|
|
|
|
|
|
|
Note that if both ``local_addr`` and ``remote_addr`` are present,
|
|
|
|
|
all combinations of local and remote addresses with matching address
|
|
|
|
|
family will be tried.
|
|
|
|
|
|
|
|
|
|
Wrapped Socket Methods
|
|
|
|
|
''''''''''''''''''''''
|
|
|
|
|
|
|
|
|
|
The following methods for doing async I/O on sockets are not for
|
|
|
|
|
general use. They are primarily meant for transport implementations
|
|
|
|
|
working with IOCP through the ``ProactorEventLoop`` class. However,
|
|
|
|
|
they are easily implementable for other event loop types, so there is
|
|
|
|
|
no reason not to require them. The socket argument has to be a
|
|
|
|
|
non-blocking socket.
|
|
|
|
|
|
|
|
|
|
- ``sock_recv(sock, n)``. Receive up to ``n`` bytes from socket
|
|
|
|
|
``sock``. Returns a Future whose result on success will be a
|
|
|
|
|
bytes object.
|
|
|
|
|
|
|
|
|
|
- ``sock_sendall(sock, data)``. Send bytes ``data`` to socket
|
|
|
|
|
``sock``. Returns a Future whose result on success will be
|
|
|
|
|
``None``. Note: the name uses ``sendall`` instead of ``send``, to
|
|
|
|
|
reflect that the semantics and signature of this method echo those
|
|
|
|
|
of the standard library socket method ``sendall()`` rather than
|
2013-11-01 18:14:39 -04:00
|
|
|
|
``send()``.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
- ``sock_connect(sock, address)``. Connect to the given address.
|
|
|
|
|
Returns a Future whose result on success will be ``None``.
|
|
|
|
|
|
|
|
|
|
- ``sock_accept(sock)``. Accept a connection from a socket. The
|
|
|
|
|
socket must be in listening mode and bound to an address. Returns a
|
|
|
|
|
Future whose result on success will be a tuple ``(conn, peer)``
|
|
|
|
|
where ``conn`` is a connected non-blocking socket and ``peer`` is
|
|
|
|
|
the peer address.
|
|
|
|
|
|
|
|
|
|
I/O Callbacks
|
|
|
|
|
'''''''''''''
|
|
|
|
|
|
|
|
|
|
These methods are primarily meant for transport implementations
|
|
|
|
|
working with a selector. They are implemented by
|
|
|
|
|
``SelectorEventLoop`` but not by ``ProactorEventLoop``. Custom event
|
|
|
|
|
loop implementations may or may not implement them.
|
|
|
|
|
|
|
|
|
|
The ``fd`` arguments below may be integer file descriptors, or
|
|
|
|
|
"file-like" objects with a ``fileno()`` method that wrap integer file
|
|
|
|
|
descriptors. Not all file-like objects or file descriptors are
|
|
|
|
|
acceptable. Sockets (and socket file descriptors) are always
|
|
|
|
|
accepted. On Windows no other types are supported. On UNIX, pipes
|
|
|
|
|
and possibly tty devices are also supported, but disk files are not.
|
|
|
|
|
Exactly which special file types are supported may vary by platform
|
|
|
|
|
and per selector implementation. (Experimentally, there is at least
|
2013-10-31 18:48:48 -04:00
|
|
|
|
one kind of pseudo-tty on OS X that is supported by ``select`` and
|
2013-04-26 17:16:59 -04:00
|
|
|
|
``poll`` but not by ``kqueue``: it is used by Emacs shell windows.)
|
2012-12-13 10:54:08 -05:00
|
|
|
|
|
2012-12-13 01:47:17 -05:00
|
|
|
|
- ``add_reader(fd, callback, *args)``. Arrange for
|
|
|
|
|
``callback(*args)`` to be called whenever file descriptor ``fd`` is
|
2013-05-02 17:11:08 -04:00
|
|
|
|
deemed ready for reading. Calling ``add_reader()`` again for the
|
|
|
|
|
same file descriptor implies a call to ``remove_reader()`` for the
|
|
|
|
|
same file descriptor.
|
2012-12-13 01:47:17 -05:00
|
|
|
|
|
|
|
|
|
- ``add_writer(fd, callback, *args)``. Like ``add_reader()``,
|
|
|
|
|
but registers the callback for writing instead of for reading.
|
|
|
|
|
|
|
|
|
|
- ``remove_reader(fd)``. Cancels the current read callback for file
|
2013-04-26 17:16:59 -04:00
|
|
|
|
descriptor ``fd``, if one is set. If no callback is currently set
|
|
|
|
|
for the file descriptor, this is a no-op and returns ``False``.
|
2013-05-02 17:11:08 -04:00
|
|
|
|
Otherwise, it removes the callback arrangement and returns ``True``.
|
2012-12-13 01:47:17 -05:00
|
|
|
|
|
|
|
|
|
- ``remove_writer(fd)``. This is to ``add_writer()`` as
|
|
|
|
|
``remove_reader()`` is to ``add_reader()``.
|
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
Pipes and Subprocesses
|
|
|
|
|
''''''''''''''''''''''
|
2012-12-17 19:46:54 -05:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
These methods are supported by ``SelectorEventLoop`` on UNIX and
|
|
|
|
|
``ProactorEventLoop`` on Windows.
|
|
|
|
|
|
|
|
|
|
The transports and protocols used with pipes and subprocesses differ
|
|
|
|
|
from those used with regular stream connections. These are described
|
|
|
|
|
later.
|
|
|
|
|
|
|
|
|
|
Each of the methods below has a ``protocol_factory`` argument, similar
|
|
|
|
|
to ``create_connection()``; this will be called exactly once, without
|
|
|
|
|
arguments, to construct the protocol object to be returned.
|
|
|
|
|
|
|
|
|
|
Each method is a coroutine that returns a ``(transport, protocol)``
|
|
|
|
|
pair on success, or raises an exception on failure.
|
2013-04-29 14:49:58 -04:00
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
- ``connect_read_pipe(protocol_factory, pipe)``: Create a
|
|
|
|
|
unidrectional stream connection from a file-like object wrapping the
|
2013-10-31 18:48:48 -04:00
|
|
|
|
read end of a UNIX pipe, which must be in non-blocking mode. The
|
|
|
|
|
transport returned is a ``ReadTransport``.
|
2012-12-13 13:30:42 -05:00
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
- ``connect_write_pipe(protocol_factory, pipe)``: Create a
|
|
|
|
|
unidrectional stream connection from a file-like object wrapping the
|
2013-10-31 18:48:48 -04:00
|
|
|
|
write end of a UNIX pipe, which must be in non-blocking mode. The
|
|
|
|
|
transport returned is a ``WriteTransport``; it does not have any
|
|
|
|
|
read-related methods. The protocol returned is a ``BaseProtocol``.
|
|
|
|
|
|
|
|
|
|
- ``subprocess_shell(protocol_factory, cmd, <options>)``: Create a
|
|
|
|
|
subprocess from ``cmd``, which is a string using the platform's
|
|
|
|
|
"shell" syntax. This is similar to the standard library
|
|
|
|
|
``subprocess.Popen()`` class called with ``shell=True``. The
|
|
|
|
|
remaining arguments and return value are described below.
|
|
|
|
|
|
|
|
|
|
- ``subprocess_exec(protocol_factory, *args, <options>)``: Create a
|
|
|
|
|
subprocess from one or more string arguments, where the first string
|
|
|
|
|
specifies the program to execute, and the remaining strings specify
|
|
|
|
|
the program's arguments. (Thus, together the string arguments form
|
|
|
|
|
the ``sys.argv`` value of the program, assuming it is a Python
|
|
|
|
|
script.) This is similar to the standard library
|
|
|
|
|
``subprocess.Popen()`` class called with ``shell=False`` and the
|
|
|
|
|
list of strings passed as the first argument; however, where
|
|
|
|
|
``Popen()`` takes a single argument which is list of strings,
|
|
|
|
|
``subprocess_exec()`` takes multiple string arguments. The
|
|
|
|
|
remaining arguments and return value are described below.
|
|
|
|
|
|
|
|
|
|
Apart from the way the program to execute is specified, the two
|
|
|
|
|
``subprocess_*()`` methods behave the same. The transport returned is
|
|
|
|
|
a ``SubprocessTransport`` which has a different interface than the
|
|
|
|
|
common bidirectional stream transport. The protocol returned is a
|
|
|
|
|
``SubprocessProtocol`` which also has a custom interface.
|
|
|
|
|
|
|
|
|
|
The <options> are all specified using optional keyword arguments:
|
|
|
|
|
|
|
|
|
|
- ``stdin``: Either a file-like object representing the pipe to be
|
|
|
|
|
connected to the subprocess's standard input stream using
|
2013-12-26 18:12:56 -05:00
|
|
|
|
``connect_write_pipe()``, or the constant ``subprocess.PIPE`` (the
|
2013-10-31 18:48:48 -04:00
|
|
|
|
default). By default a new pipe will be created and connected.
|
|
|
|
|
|
|
|
|
|
- ``stdout``: Either a file-like object representing the pipe to be
|
|
|
|
|
connected to the subprocess's standard output stream using
|
2013-12-26 18:12:56 -05:00
|
|
|
|
``connect_read_pipe()``, or the constant ``subprocess.PIPE`` (the
|
2013-10-31 18:48:48 -04:00
|
|
|
|
default). By default a new pipe will be created and connected.
|
|
|
|
|
|
|
|
|
|
- ``stderr``: Either a file-like object representing the pipe to be
|
|
|
|
|
connected to the subprocess's standard error stream using
|
2013-12-26 18:12:56 -05:00
|
|
|
|
``connect_read_pipe()``, or one of the constants ``subprocess.PIPE``
|
2013-10-31 18:48:48 -04:00
|
|
|
|
(the default) or ``subprocess.STDOUT``. By default a new pipe will
|
|
|
|
|
be created and connected. When ``subprocess.STDOUT`` is specified,
|
|
|
|
|
the subprocess's standard error stream will be connected to the same
|
2014-02-23 00:42:54 -05:00
|
|
|
|
pipe as the standard output stream.
|
2013-10-31 18:48:48 -04:00
|
|
|
|
|
|
|
|
|
- ``bufsize``: The buffer size to be used when creating a pipe; this
|
|
|
|
|
is passed to ``subprocess.Popen()``. In the default implementation
|
|
|
|
|
this defaults to zero, and on Windows it must be zero; these
|
|
|
|
|
defaults deviate from ``subprocess.Popen()``.
|
|
|
|
|
|
|
|
|
|
- ``executable``, ``preexec_fn``, ``close_fds``, ``cwd``, ``env``,
|
|
|
|
|
``startupinfo``, ``creationflags``, ``restore_signals``,
|
|
|
|
|
``start_new_session``, ``pass_fds``: These optional arguments are
|
|
|
|
|
passed to ``subprocess.Popen()`` without interpretation.
|
2012-12-13 13:30:42 -05:00
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
Signal callbacks
|
|
|
|
|
''''''''''''''''
|
2012-12-13 13:30:42 -05:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
These methods are only supported on UNIX.
|
|
|
|
|
|
2013-08-09 19:35:07 -04:00
|
|
|
|
- ``add_signal_handler(sig, callback, *args)``. Whenever signal
|
|
|
|
|
``sig`` is received, arrange for ``callback(*args)`` to be called.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
Specifying another callback for the same signal replaces the
|
|
|
|
|
previous handler (only one handler can be active per signal). The
|
2014-02-24 17:22:46 -05:00
|
|
|
|
``sig`` must be a valid signal number defined in the ``signal``
|
2013-04-26 17:16:59 -04:00
|
|
|
|
module. If the signal cannot be handled this raises an exception:
|
2013-08-09 19:35:07 -04:00
|
|
|
|
``ValueError`` if it is not a valid signal or if it is an
|
2013-08-09 20:26:53 -04:00
|
|
|
|
uncatchable signal (e.g. ``SIGKILL``), ``RuntimeError`` if this
|
2013-08-09 19:35:07 -04:00
|
|
|
|
particular event loop instance cannot handle signals (since signals
|
|
|
|
|
are global per process, only an event loop associated with the main
|
|
|
|
|
thread can handle signals).
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
- ``remove_signal_handler(sig)``. Removes the handler for signal
|
|
|
|
|
``sig``, if one is set. Raises the same exceptions as
|
|
|
|
|
``add_signal_handler()`` (except that it may return ``False``
|
|
|
|
|
instead raising ``RuntimeError`` for uncatchable signals). Returns
|
|
|
|
|
``True`` if a handler was removed successfully, ``False`` if no
|
|
|
|
|
handler was set.
|
|
|
|
|
|
|
|
|
|
Note: If these methods are statically known to be unsupported, they
|
2013-10-31 18:48:48 -04:00
|
|
|
|
may raise ``NotImplementedError`` instead of ``RuntimeError``.
|
2012-12-13 01:47:17 -05:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
Mutual Exclusion of Callbacks
|
|
|
|
|
-----------------------------
|
2012-12-13 01:47:17 -05:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
An event loop should enforce mutual exclusion of callbacks, i.e. it
|
|
|
|
|
should never start a callback while a previously callback is still
|
|
|
|
|
running. This should apply across all types of callbacks, regardless
|
|
|
|
|
of whether they are scheduled using ``call_soon()``, ``call_later()``,
|
|
|
|
|
``call_at()``, ``call_soon_threadsafe()``, ``add_reader()``,
|
|
|
|
|
``add_writer()``, or ``add_signal_handler()``.
|
2012-12-13 01:47:17 -05:00
|
|
|
|
|
2012-12-13 17:40:23 -05:00
|
|
|
|
Exceptions
|
|
|
|
|
----------
|
|
|
|
|
|
|
|
|
|
There are two categories of exceptions in Python: those that derive
|
|
|
|
|
from the ``Exception`` class and those that derive from
|
|
|
|
|
``BaseException``. Exceptions deriving from ``Exception`` will
|
|
|
|
|
generally be caught and handled appropriately; for example, they will
|
|
|
|
|
be passed through by Futures, and they will be logged and ignored when
|
|
|
|
|
they occur in a callback.
|
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
However, exceptions deriving only from ``BaseException`` are typically
|
|
|
|
|
not caught, and will usually cause the program to terminate with a
|
|
|
|
|
traceback. In some cases they are caught and re-raised. (Examples of
|
|
|
|
|
this category include ``KeyboardInterrupt`` and ``SystemExit``; it is
|
|
|
|
|
usually unwise to treat these the same as most other exceptions.)
|
2012-12-13 17:40:23 -05:00
|
|
|
|
|
2016-11-03 18:19:44 -04:00
|
|
|
|
The event loop passes the latter category into its *exception
|
|
|
|
|
handler*. This is a callback which accepts a *context* dict as a
|
|
|
|
|
parameter::
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
|
|
|
|
def exception_handler(context):
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
*context* may have many different keys but several of them are very
|
2016-11-03 18:19:44 -04:00
|
|
|
|
widely used:
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
|
|
|
|
- ``'message'``: error message.
|
2016-11-03 18:19:44 -04:00
|
|
|
|
- ``'exception'``: exception instance; ``None`` if there is no
|
|
|
|
|
exception.
|
|
|
|
|
- ``'source_traceback'``: a list of strings representing stack at the
|
|
|
|
|
point the object involved in the error was created.
|
|
|
|
|
- ``'handle_traceback'``: a list of strings representing the stack at
|
|
|
|
|
the moment the handle involved in the error was created.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
|
|
|
|
The loop has the following methods related to exception handling:
|
|
|
|
|
|
2016-11-03 18:19:44 -04:00
|
|
|
|
- ``get_exception_handler()`` returns the current exception handler
|
|
|
|
|
registered for the loop.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
2016-11-03 18:19:44 -04:00
|
|
|
|
- ``set_exception_handler(handler)`` sets the exception handler.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
2016-11-03 18:19:44 -04:00
|
|
|
|
- ``default_exception_handler(context)`` the *default* exception
|
|
|
|
|
handler for this loop implementation.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
|
|
|
|
- ``call_exception_handler(context)`` passes *context* into the
|
2016-11-03 18:19:44 -04:00
|
|
|
|
registered exception handler. This allows handling uncaught
|
2016-11-03 18:21:23 -04:00
|
|
|
|
exceptions uniformly by third-party libraries.
|
|
|
|
|
|
|
|
|
|
The loop uses ``default_exception_handler()`` if the default was not
|
|
|
|
|
overridden by explicit ``set_exception_handler()`` call.
|
|
|
|
|
|
|
|
|
|
Debug Mode
|
|
|
|
|
----------
|
|
|
|
|
|
2016-11-03 18:19:44 -04:00
|
|
|
|
By default the loop operates in *release* mode. Applications may
|
|
|
|
|
enable *debug* mode better error reporting at the cost of some
|
|
|
|
|
performance.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
2016-11-03 18:19:44 -04:00
|
|
|
|
In debug mode many additional checks are enabled, for example:
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
2016-11-03 18:19:44 -04:00
|
|
|
|
- Source tracebacks are available for unhandled exceptions in futures/tasks.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
2016-11-03 18:19:44 -04:00
|
|
|
|
- The loop checks for slow callbacks to detect accidental blocking for I/O.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
2016-11-03 18:19:44 -04:00
|
|
|
|
The ``loop.slow_callback_duration`` attribute controls the maximum
|
|
|
|
|
execution time allowed between two *yield points* before a slow
|
|
|
|
|
callback is reported. The default value is 0.1 seconds; it may be
|
|
|
|
|
changed by assigning to it.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
2016-11-03 18:19:44 -04:00
|
|
|
|
There are two methods related to debug mode:
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
|
|
|
|
- ``get_debug()`` returns ``True`` if *debug* mode is enabled,
|
|
|
|
|
``False`` otherwise.
|
|
|
|
|
|
2016-11-03 18:19:44 -04:00
|
|
|
|
- ``set_debug(enabled)`` enables *debug* mode if the argument is ``True``.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
2016-11-03 18:19:44 -04:00
|
|
|
|
Debug mode is automatically enabled if the ``PYTHONASYNCIODEBUG``
|
|
|
|
|
*environment variable* is defined and not empty.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
|
|
|
|
|
2013-04-29 14:49:58 -04:00
|
|
|
|
Handles
|
|
|
|
|
-------
|
2013-04-26 23:33:04 -04:00
|
|
|
|
|
2013-05-02 17:11:08 -04:00
|
|
|
|
The various methods for registering one-off callbacks
|
2013-05-02 19:35:23 -04:00
|
|
|
|
(``call_soon()``, ``call_later()``, ``call_at()`` and
|
|
|
|
|
``call_soon_threadsafe()``) all return an object representing the
|
|
|
|
|
registration that can be used to cancel the callback. This object is
|
2013-10-31 18:48:48 -04:00
|
|
|
|
called a ``Handle``. Handles are opaque and have only one public
|
|
|
|
|
method:
|
|
|
|
|
|
|
|
|
|
- ``cancel()``: Cancel the callback.
|
|
|
|
|
|
|
|
|
|
Note that ``add_reader()``, ``add_writer()`` and
|
|
|
|
|
``add_signal_handler()`` do not return Handles.
|
|
|
|
|
|
|
|
|
|
Servers
|
|
|
|
|
-------
|
2013-04-26 23:33:04 -04:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
The ``create_server()`` method returns a ``Server`` instance, which
|
|
|
|
|
wraps the sockets (or other network objects) used to accept requests.
|
|
|
|
|
This class has two public methods:
|
2012-12-13 14:58:47 -05:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- ``close()``: Close the service. This stops accepting new requests
|
|
|
|
|
but does not cancel requests that have already been accepted and are
|
|
|
|
|
currently being handled.
|
|
|
|
|
|
|
|
|
|
- ``wait_closed()``: A coroutine that blocks until the service is
|
|
|
|
|
closed and all accepted requests have been handled.
|
2012-12-13 14:58:47 -05:00
|
|
|
|
|
2012-12-13 01:47:17 -05:00
|
|
|
|
Futures
|
|
|
|
|
-------
|
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
The ``asyncio.Future`` class here is intentionally similar to the
|
2022-01-21 06:03:51 -05:00
|
|
|
|
``concurrent.futures.Future`` class specified by :pep:`3148`, but there
|
2013-01-09 14:58:51 -05:00
|
|
|
|
are slight differences. Whenever this PEP talks about Futures or
|
2013-10-31 18:48:48 -04:00
|
|
|
|
futures this should be understood to refer to ``asyncio.Future`` unless
|
2013-01-09 14:58:51 -05:00
|
|
|
|
``concurrent.futures.Future`` is explicitly mentioned. The supported
|
2022-01-21 06:03:51 -05:00
|
|
|
|
public API is as follows, indicating the differences with :pep:`3148`:
|
2013-01-09 14:58:51 -05:00
|
|
|
|
|
2013-11-01 18:14:39 -04:00
|
|
|
|
- ``cancel()``. If the Future is already done (or cancelled), do
|
|
|
|
|
nothing and return ``False``. Otherwise, this attempts to cancel
|
2016-05-03 05:03:16 -04:00
|
|
|
|
the Future and returns ``True``. If the cancellation attempt is
|
2013-11-01 18:14:39 -04:00
|
|
|
|
successful, eventually the Future's state will change to cancelled
|
2013-11-25 01:43:36 -05:00
|
|
|
|
(so that ``cancelled()`` will return ``True``)
|
2013-11-01 18:14:39 -04:00
|
|
|
|
and the callbacks will be scheduled. For regular Futures,
|
|
|
|
|
cancellation will always succeed immediately; but for Tasks (see
|
|
|
|
|
below) the task may ignore or delay the cancellation attempt.
|
2013-01-09 14:58:51 -05:00
|
|
|
|
|
2013-11-01 18:14:39 -04:00
|
|
|
|
- ``cancelled()``. Returns ``True`` if the Future was successfully
|
|
|
|
|
cancelled.
|
2013-01-09 14:58:51 -05:00
|
|
|
|
|
|
|
|
|
- ``done()``. Returns ``True`` if the Future is done. Note that a
|
|
|
|
|
cancelled Future is considered done too (here and everywhere).
|
|
|
|
|
|
|
|
|
|
- ``result()``. Returns the result set with ``set_result()``, or
|
|
|
|
|
raises the exception set with ``set_exception()``. Raises
|
2022-01-21 06:03:51 -05:00
|
|
|
|
``CancelledError`` if cancelled. Difference with :pep:`3148`: This has
|
2013-01-09 14:58:51 -05:00
|
|
|
|
no timeout argument and does *not* wait; if the future is not yet
|
|
|
|
|
done, it raises an exception.
|
|
|
|
|
|
|
|
|
|
- ``exception()``. Returns the exception if set with
|
|
|
|
|
``set_exception()``, or ``None`` if a result was set with
|
|
|
|
|
``set_result()``. Raises ``CancelledError`` if cancelled.
|
2022-01-21 06:03:51 -05:00
|
|
|
|
Difference with :pep:`3148`: This has no timeout argument and does
|
2013-01-09 14:58:51 -05:00
|
|
|
|
*not* wait; if the future is not yet done, it raises an exception.
|
|
|
|
|
|
|
|
|
|
- ``add_done_callback(fn)``. Add a callback to be run when the Future
|
|
|
|
|
becomes done (or is cancelled). If the Future is already done (or
|
|
|
|
|
cancelled), schedules the callback to using ``call_soon()``.
|
2022-01-21 06:03:51 -05:00
|
|
|
|
Difference with :pep:`3148`: The callback is never called immediately,
|
2013-10-01 11:37:33 -04:00
|
|
|
|
and always in the context of the caller -- typically this is a
|
|
|
|
|
thread. You can think of this as calling the callback through
|
2022-01-21 06:03:51 -05:00
|
|
|
|
``call_soon()``. Note that in order to match :pep:`3148`, the callback
|
2013-05-02 19:35:23 -04:00
|
|
|
|
(unlike all other callbacks defined in this PEP, and ignoring the
|
|
|
|
|
convention from the section "Callback Style" below) is always called
|
2013-10-01 11:37:33 -04:00
|
|
|
|
with a single argument, the Future object. (The motivation for
|
|
|
|
|
strictly serializing callbacks scheduled with ``call_soon()``
|
|
|
|
|
applies here too.)
|
2013-01-09 14:58:51 -05:00
|
|
|
|
|
2013-05-29 18:33:55 -04:00
|
|
|
|
- ``remove_done_callback(fn)``. Remove the argument from the list of
|
2022-01-21 06:03:51 -05:00
|
|
|
|
callbacks. This method is not defined by :pep:`3148`. The argument
|
2013-05-29 18:33:55 -04:00
|
|
|
|
must be equal (using ``==``) to the argument passed to
|
|
|
|
|
``add_done_callback()``. Returns the number of times the callback
|
|
|
|
|
was removed.
|
|
|
|
|
|
2013-01-09 14:58:51 -05:00
|
|
|
|
- ``set_result(result)``. The Future must not be done (nor cancelled)
|
|
|
|
|
already. This makes the Future done and schedules the callbacks.
|
2022-01-21 06:03:51 -05:00
|
|
|
|
Difference with :pep:`3148`: This is a public API.
|
2013-01-09 14:58:51 -05:00
|
|
|
|
|
|
|
|
|
- ``set_exception(exception)``. The Future must not be done (nor
|
|
|
|
|
cancelled) already. This makes the Future done and schedules the
|
2022-01-21 06:03:51 -05:00
|
|
|
|
callbacks. Difference with :pep:`3148`: This is a public API.
|
2013-01-09 14:58:51 -05:00
|
|
|
|
|
|
|
|
|
The internal method ``set_running_or_notify_cancel()`` is not
|
2013-08-09 20:26:53 -04:00
|
|
|
|
supported; there is no way to set the running state. Likewise,
|
|
|
|
|
the method ``running()`` is not supported.
|
2013-01-09 14:58:51 -05:00
|
|
|
|
|
|
|
|
|
The following exceptions are defined:
|
|
|
|
|
|
|
|
|
|
- ``InvalidStateError``. Raised whenever the Future is not in a state
|
|
|
|
|
acceptable to the method being called (e.g. calling ``set_result()``
|
|
|
|
|
on a Future that is already done, or calling ``result()`` on a Future
|
|
|
|
|
that is not yet done).
|
|
|
|
|
|
|
|
|
|
- ``InvalidTimeoutError``. Raised by ``result()`` and ``exception()``
|
|
|
|
|
when a nonzero ``timeout`` argument is given.
|
|
|
|
|
|
|
|
|
|
- ``CancelledError``. An alias for
|
|
|
|
|
``concurrent.futures.CancelledError``. Raised when ``result()`` or
|
|
|
|
|
``exception()`` is called on a Future that is cancelled.
|
|
|
|
|
|
|
|
|
|
- ``TimeoutError``. An alias for ``concurrent.futures.TimeoutError``.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
May be raised by ``run_until_complete()``.
|
2013-01-09 14:58:51 -05:00
|
|
|
|
|
2013-11-01 18:14:39 -04:00
|
|
|
|
A Future is associated with an event loop when it is created.
|
2012-12-13 14:58:47 -05:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
A ``asyncio.Future`` object is not acceptable to the ``wait()`` and
|
2012-12-13 14:58:47 -05:00
|
|
|
|
``as_completed()`` functions in the ``concurrent.futures`` package.
|
2013-10-31 18:48:48 -04:00
|
|
|
|
However, there are similar APIs ``asyncio.wait()`` and
|
|
|
|
|
``asyncio.as_completed()``, described below.
|
2012-12-13 14:58:47 -05:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
A ``asyncio.Future`` object is acceptable to a ``yield from`` expression
|
2012-12-13 20:47:40 -05:00
|
|
|
|
when used in a coroutine. This is implemented through the
|
|
|
|
|
``__iter__()`` interface on the Future. See the section "Coroutines
|
|
|
|
|
and the Scheduler" below.
|
2012-12-13 01:47:17 -05:00
|
|
|
|
|
2012-12-16 17:26:48 -05:00
|
|
|
|
When a Future is garbage-collected, if it has an associated exception
|
2013-11-01 18:14:39 -04:00
|
|
|
|
but neither ``result()`` nor ``exception()`` has ever been called, the
|
|
|
|
|
exception is logged. (When a coroutine uses ``yield from`` to wait
|
|
|
|
|
for a Future, that Future's ``result()`` method is called once the
|
|
|
|
|
coroutine is resumed.)
|
2012-12-16 17:26:48 -05:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
In the future (pun intended) we may unify ``asyncio.Future`` and
|
2012-12-14 14:39:26 -05:00
|
|
|
|
``concurrent.futures.Future``, e.g. by adding an ``__iter__()`` method
|
|
|
|
|
to the latter that works with ``yield from``. To prevent accidentally
|
|
|
|
|
blocking the event loop by calling e.g. ``result()`` on a Future
|
2013-05-10 19:09:48 -04:00
|
|
|
|
that's not done yet, the blocking operation may detect that an event
|
2012-12-14 14:39:26 -05:00
|
|
|
|
loop is active in the current thread and raise an exception instead.
|
|
|
|
|
However the current PEP strives to have no dependencies beyond Python
|
|
|
|
|
3.3, so changes to ``concurrent.futures.Future`` are off the table for
|
|
|
|
|
now.
|
|
|
|
|
|
2013-05-21 11:34:55 -04:00
|
|
|
|
There are some public functions related to Futures:
|
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- ``asyncio.async(arg)``. This takes an argument that is either a
|
|
|
|
|
coroutine object or a Future (i.e., anything you can use with
|
|
|
|
|
``yield from``) and returns a Future. If the argument is a Future,
|
|
|
|
|
it is returned unchanged; if it is a coroutine object, it wraps it
|
|
|
|
|
in a Task (remember that ``Task`` is a subclass of ``Future``).
|
2013-05-10 19:09:48 -04:00
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
|
- ``asyncio.wrap_future(future)``. This takes a :pep:`3148` Future
|
2013-10-31 18:48:48 -04:00
|
|
|
|
(i.e., an instance of ``concurrent.futures.Future``) and returns a
|
|
|
|
|
Future compatible with the event loop (i.e., a ``asyncio.Future``
|
|
|
|
|
instance).
|
2013-05-10 19:09:48 -04:00
|
|
|
|
|
2012-12-13 01:47:17 -05:00
|
|
|
|
Transports
|
|
|
|
|
----------
|
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
Transports and protocols are strongly influenced by Twisted and PEP
|
|
|
|
|
3153. Users rarely implement or instantiate transports -- rather,
|
|
|
|
|
event loops offer utility methods to set up transports.
|
2012-12-13 17:40:23 -05:00
|
|
|
|
|
|
|
|
|
Transports work in conjunction with protocols. Protocols are
|
|
|
|
|
typically written without knowing or caring about the exact type of
|
|
|
|
|
transport used, and transports can be used with a wide variety of
|
|
|
|
|
protocols. For example, an HTTP client protocol implementation may be
|
2013-10-24 11:37:55 -04:00
|
|
|
|
used with either a plain socket transport or an SSL/TLS transport.
|
|
|
|
|
The plain socket transport can be used with many different protocols
|
2012-12-13 17:40:23 -05:00
|
|
|
|
besides HTTP (e.g. SMTP, IMAP, POP, FTP, IRC, SPDY).
|
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
The most common type of transport is a bidirectional stream transport.
|
|
|
|
|
There are also unidirectional stream transports (used for pipes) and
|
|
|
|
|
datagram transports (used by the ``create_datagram_endpoint()``
|
|
|
|
|
method).
|
|
|
|
|
|
|
|
|
|
Methods For All Transports
|
|
|
|
|
''''''''''''''''''''''''''
|
|
|
|
|
|
|
|
|
|
- ``get_extra_info(name, default=None)``. This is a catch-all method
|
|
|
|
|
that returns implementation-specific information about a transport.
|
|
|
|
|
The first argument is the name of the extra field to be retrieved.
|
|
|
|
|
The optional second argument is a default value to be returned.
|
|
|
|
|
Consult the implementation documentation to find out the supported
|
|
|
|
|
extra field names. For an unsupported name, the default is always
|
|
|
|
|
returned.
|
|
|
|
|
|
|
|
|
|
Bidirectional Stream Transports
|
|
|
|
|
'''''''''''''''''''''''''''''''
|
|
|
|
|
|
2017-02-08 21:44:15 -05:00
|
|
|
|
A bidirectional stream transport is an abstraction on top of a socket
|
2013-10-24 11:37:55 -04:00
|
|
|
|
or something similar (for example, a pair of UNIX pipes or an SSL/TLS
|
2013-04-26 17:16:59 -04:00
|
|
|
|
connection).
|
|
|
|
|
|
2012-12-13 17:40:23 -05:00
|
|
|
|
Most connections have an asymmetric nature: the client and server
|
|
|
|
|
usually have very different roles and behaviors. Hence, the interface
|
|
|
|
|
between transport and protocol is also asymmetric. From the
|
|
|
|
|
protocol's point of view, *writing* data is done by calling the
|
|
|
|
|
``write()`` method on the transport object; this buffers the data and
|
|
|
|
|
returns immediately. However, the transport takes a more active role
|
|
|
|
|
in *reading* data: whenever some data is read from the socket (or
|
|
|
|
|
other data source), the transport calls the protocol's
|
|
|
|
|
``data_received()`` method.
|
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
Nevertheless, the interface between transport and protocol used by
|
|
|
|
|
bidirectional streams is the same for clients as it is for servers,
|
|
|
|
|
since the connection between a client and a server is essentially a
|
|
|
|
|
pair of streams, one in each direction.
|
|
|
|
|
|
|
|
|
|
Bidirectional stream transports have the following public methods:
|
2012-12-13 17:40:23 -05:00
|
|
|
|
|
|
|
|
|
- ``write(data)``. Write some bytes. The argument must be a bytes
|
|
|
|
|
object. Returns ``None``. The transport is free to buffer the
|
|
|
|
|
bytes, but it must eventually cause the bytes to be transferred to
|
|
|
|
|
the entity at the other end, and it must maintain stream behavior.
|
|
|
|
|
That is, ``t.write(b'abc'); t.write(b'def')`` is equivalent to
|
|
|
|
|
``t.write(b'abcdef')``, as well as to::
|
|
|
|
|
|
|
|
|
|
t.write(b'a')
|
|
|
|
|
t.write(b'b')
|
|
|
|
|
t.write(b'c')
|
|
|
|
|
t.write(b'd')
|
|
|
|
|
t.write(b'e')
|
|
|
|
|
t.write(b'f')
|
|
|
|
|
|
|
|
|
|
- ``writelines(iterable)``. Equivalent to::
|
|
|
|
|
|
|
|
|
|
for data in iterable:
|
2012-12-19 00:40:17 -05:00
|
|
|
|
self.write(data)
|
2012-12-13 17:40:23 -05:00
|
|
|
|
|
|
|
|
|
- ``write_eof()``. Close the writing end of the connection.
|
|
|
|
|
Subsequent calls to ``write()`` are not allowed. Once all buffered
|
|
|
|
|
data is transferred, the transport signals to the other end that no
|
|
|
|
|
more data will be received. Some protocols don't support this
|
|
|
|
|
operation; in that case, calling ``write_eof()`` will raise an
|
|
|
|
|
exception. (Note: This used to be called ``half_close()``, but
|
|
|
|
|
unless you already know what it is for, that name doesn't indicate
|
|
|
|
|
*which* end is closed.)
|
|
|
|
|
|
|
|
|
|
- ``can_write_eof()``. Return ``True`` if the protocol supports
|
2013-04-29 14:49:58 -04:00
|
|
|
|
``write_eof()``, ``False`` if it does not. (This method typically
|
|
|
|
|
returns a fixed value that depends only on the specific Transport
|
|
|
|
|
class, not on the state of the Transport object. It is needed
|
2012-12-13 17:40:23 -05:00
|
|
|
|
because some protocols need to change their behavior when
|
|
|
|
|
``write_eof()`` is unavailable. For example, in HTTP, to send data
|
|
|
|
|
whose size is not known ahead of time, the end of the data is
|
2013-10-24 11:37:55 -04:00
|
|
|
|
typically indicated using ``write_eof()``; however, SSL/TLS does not
|
2012-12-13 17:40:23 -05:00
|
|
|
|
support this, and an HTTP protocol implementation would have to use
|
|
|
|
|
the "chunked" transfer encoding in this case. But if the data size
|
|
|
|
|
is known ahead of time, the best approach in both cases is to use
|
|
|
|
|
the Content-Length header.)
|
|
|
|
|
|
2013-11-14 16:25:58 -05:00
|
|
|
|
- ``get_write_buffer_size()``. Return the current size of the
|
|
|
|
|
transport's write buffer in bytes. This only knows about the write
|
|
|
|
|
buffer managed explicitly by the transport; buffering in other
|
|
|
|
|
layers of the network stack or elsewhere of the network is not
|
|
|
|
|
reported.
|
|
|
|
|
|
|
|
|
|
- ``set_write_buffer_limits(high=None, low=None)``. Set the high- and
|
|
|
|
|
low-water limits for flow control.
|
|
|
|
|
|
|
|
|
|
These two values control when to call the protocol's
|
|
|
|
|
``pause_writing()`` and ``resume_writing()`` methods. If specified,
|
|
|
|
|
the low-water limit must be less than or equal to the high-water
|
|
|
|
|
limit. Neither value can be negative.
|
|
|
|
|
|
|
|
|
|
The defaults are implementation-specific. If only the high-water
|
2021-02-03 09:06:23 -05:00
|
|
|
|
limit is given, the low-water limit defaults to an
|
2013-11-14 16:25:58 -05:00
|
|
|
|
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.
|
|
|
|
|
|
2013-10-18 12:35:37 -04:00
|
|
|
|
- ``pause_reading()``. Suspend delivery of data to the protocol until
|
|
|
|
|
a subsequent ``resume_reading()`` call. Between ``pause_reading()``
|
|
|
|
|
and ``resume_reading()``, the protocol's ``data_received()`` method
|
2013-11-01 18:14:39 -04:00
|
|
|
|
will not be called.
|
2012-12-13 17:40:23 -05:00
|
|
|
|
|
2013-10-18 12:35:37 -04:00
|
|
|
|
- ``resume_reading()``. Restart delivery of data to the protocol via
|
2013-11-01 18:14:39 -04:00
|
|
|
|
``data_received()``. Note that "paused" is a binary state --
|
|
|
|
|
``pause_reading()`` should only be called when the transport is not
|
|
|
|
|
paused, while ``resume_reading()`` should only be called when the
|
|
|
|
|
transport is paused.
|
2012-12-13 17:40:23 -05:00
|
|
|
|
|
|
|
|
|
- ``close()``. Sever the connection with the entity at the other end.
|
|
|
|
|
Any data buffered by ``write()`` will (eventually) be transferred
|
|
|
|
|
before the connection is actually closed. The protocol's
|
|
|
|
|
``data_received()`` method will not be called again. Once all
|
|
|
|
|
buffered data has been flushed, the protocol's ``connection_lost()``
|
|
|
|
|
method will be called with ``None`` as the argument. Note that
|
|
|
|
|
this method does not wait for all that to happen.
|
|
|
|
|
|
|
|
|
|
- ``abort()``. Immediately sever the connection. Any data still
|
|
|
|
|
buffered by the transport is thrown away. Soon, the protocol's
|
|
|
|
|
``connection_lost()`` method will be called with ``None`` as
|
2013-11-01 18:14:39 -04:00
|
|
|
|
argument.
|
2012-12-12 20:35:17 -05:00
|
|
|
|
|
2013-04-26 17:16:59 -04:00
|
|
|
|
Unidirectional Stream Transports
|
|
|
|
|
''''''''''''''''''''''''''''''''
|
|
|
|
|
|
|
|
|
|
A writing stream transport supports the ``write()``, ``writelines()``,
|
2017-02-08 21:44:15 -05:00
|
|
|
|
``write_eof()``, ``can_write_eof()``, ``close()`` and ``abort()`` methods
|
|
|
|
|
described for bidirectional stream transports.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
2013-10-18 12:35:37 -04:00
|
|
|
|
A reading stream transport supports the ``pause_reading()``,
|
|
|
|
|
``resume_reading()`` and ``close()`` methods described for
|
2017-02-08 21:44:15 -05:00
|
|
|
|
bidirectional stream transports.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
A writing stream transport calls only ``connection_made()`` and
|
|
|
|
|
``connection_lost()`` on its associated protocol.
|
|
|
|
|
|
|
|
|
|
A reading stream transport can call all protocol methods specified in
|
|
|
|
|
the Protocols section below (i.e., the previous two plus
|
|
|
|
|
``data_received()`` and ``eof_received()``).
|
|
|
|
|
|
|
|
|
|
Datagram Transports
|
|
|
|
|
'''''''''''''''''''
|
|
|
|
|
|
|
|
|
|
Datagram transports have these methods:
|
|
|
|
|
|
|
|
|
|
- ``sendto(data, addr=None)``. Sends a datagram (a bytes object).
|
|
|
|
|
The optional second argument is the destination address. If
|
|
|
|
|
omitted, ``remote_addr`` must have been specified in the
|
|
|
|
|
``create_datagram_endpoint()`` call that created this transport. If
|
2013-04-30 00:24:46 -04:00
|
|
|
|
present, and ``remote_addr`` was specified, they must match. The
|
|
|
|
|
(data, addr) pair may be sent immediately or buffered. The return
|
2013-11-14 16:25:58 -05:00
|
|
|
|
value is ``None``.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
- ``abort()``. Immediately close the transport. Buffered data will
|
|
|
|
|
be discarded.
|
|
|
|
|
|
|
|
|
|
- ``close()``. Close the transport. Buffered data will be
|
|
|
|
|
transmitted asynchronously.
|
|
|
|
|
|
|
|
|
|
Datagram transports call the following methods on the associated
|
|
|
|
|
protocol object: ``connection_made()``, ``connection_lost()``,
|
2013-11-15 19:47:42 -05:00
|
|
|
|
``error_received()`` and ``datagram_received()``. ("Connection"
|
2013-04-26 17:16:59 -04:00
|
|
|
|
in these method names is a slight misnomer, but the concepts still
|
|
|
|
|
exist: ``connection_made()`` means the transport representing the
|
|
|
|
|
endpoint has been created, and ``connection_lost()`` means the
|
2013-11-15 19:47:42 -05:00
|
|
|
|
transport is closed.)
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
Subprocess Transports
|
|
|
|
|
'''''''''''''''''''''
|
|
|
|
|
|
2013-11-14 16:25:58 -05:00
|
|
|
|
Subprocess transports have the following methods:
|
|
|
|
|
|
|
|
|
|
- ``get_pid()``. Return the process ID of the subprocess.
|
|
|
|
|
|
|
|
|
|
- ``get_returncode()``. Return the process return code, if the
|
|
|
|
|
process has exited; otherwise ``None``.
|
|
|
|
|
|
|
|
|
|
- ``get_pipe_transport(fd)``. Return the pipe transport (a
|
|
|
|
|
unidirectional stream transport) corresponding to the argument,
|
|
|
|
|
which should be 0, 1 or 2 representing stdin, stdout or stderr (of
|
|
|
|
|
the subprocess). If there is no such pipe transport, return
|
|
|
|
|
``None``. For stdin, this is a writing transport; for stdout and
|
|
|
|
|
stderr this is a reading transport. You must use this method to get
|
|
|
|
|
a transport you can use to write to the subprocess's stdin.
|
|
|
|
|
|
|
|
|
|
- ``send_signal(signal)``. Send a signal to the subprocess.
|
|
|
|
|
|
|
|
|
|
- ``terminate()``. Terminate the subprocess.
|
|
|
|
|
|
|
|
|
|
- ``kill()``. Kill the subprocess. On Windows this is an alias for
|
|
|
|
|
``terminate()``.
|
|
|
|
|
|
|
|
|
|
- ``close()``. This is an alias for ``terminate()``.
|
|
|
|
|
|
|
|
|
|
Note that ``send_signal()``, ``terminate()`` and ``kill()`` wrap the
|
|
|
|
|
corresponding methods in the standard library ``subprocess`` module.
|
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
|
2012-12-13 01:47:17 -05:00
|
|
|
|
Protocols
|
|
|
|
|
---------
|
|
|
|
|
|
2012-12-13 17:40:23 -05:00
|
|
|
|
Protocols are always used in conjunction with transports. While a few
|
2012-12-14 13:13:20 -05:00
|
|
|
|
common protocols are provided (e.g. decent though not necessarily
|
2012-12-13 17:40:23 -05:00
|
|
|
|
excellent HTTP client and server implementations), most protocols will
|
|
|
|
|
be implemented by user code or third-party libraries.
|
|
|
|
|
|
2013-04-30 00:24:46 -04:00
|
|
|
|
|
|
|
|
|
Like for transports, we distinguish between stream protocols, datagram
|
|
|
|
|
protocols, and perhaps other custom protocols. The most common type
|
|
|
|
|
of protocol is a bidirectional stream protocol. (There are no
|
|
|
|
|
unidirectional protocols.)
|
|
|
|
|
|
|
|
|
|
Stream Protocols
|
|
|
|
|
''''''''''''''''
|
|
|
|
|
|
|
|
|
|
A (bidirectional) stream protocol must implement the following
|
|
|
|
|
methods, which will be called by the transport. Think of these as
|
|
|
|
|
callbacks that are always called by the event loop in the right
|
|
|
|
|
context. (See the "Context" section way above.)
|
2012-12-13 17:40:23 -05:00
|
|
|
|
|
|
|
|
|
- ``connection_made(transport)``. Indicates that the transport is
|
|
|
|
|
ready and connected to the entity at the other end. The protocol
|
|
|
|
|
should probably save the transport reference as an instance variable
|
|
|
|
|
(so it can call its ``write()`` and other methods later), and may
|
|
|
|
|
write an initial greeting or request at this point.
|
|
|
|
|
|
|
|
|
|
- ``data_received(data)``. The transport has read some bytes from the
|
|
|
|
|
connection. The argument is always a non-empty bytes object. There
|
|
|
|
|
are no guarantees about the minimum or maximum size of the data
|
|
|
|
|
passed along this way. ``p.data_received(b'abcdef')`` should be
|
|
|
|
|
treated exactly equivalent to::
|
|
|
|
|
|
|
|
|
|
p.data_received(b'abc')
|
|
|
|
|
p.data_received(b'def')
|
|
|
|
|
|
2012-12-14 18:36:20 -05:00
|
|
|
|
- ``eof_received()``. This is called when the other end called
|
2013-10-18 12:35:37 -04:00
|
|
|
|
``write_eof()`` (or something equivalent). If this returns a false
|
2013-11-14 16:25:58 -05:00
|
|
|
|
value (including ``None``), the transport will close itself. If it
|
2013-10-18 12:35:37 -04:00
|
|
|
|
returns a true value, closing the transport is up to the protocol.
|
2013-11-01 18:14:39 -04:00
|
|
|
|
However, for SSL/TLS connections this is ignored, because the TLS
|
|
|
|
|
standard requires that no more data is sent and the connection is
|
|
|
|
|
closed as soon as a "closure alert" is received.
|
2013-10-18 12:35:37 -04:00
|
|
|
|
|
2013-11-14 16:25:58 -05:00
|
|
|
|
The default implementation returns ``None``.
|
|
|
|
|
|
|
|
|
|
- ``pause_writing()``. Asks that the protocol temporarily stop
|
|
|
|
|
writing data to the transport. Heeding the request is optional, but
|
|
|
|
|
the transport's buffer may grow without bounds if you keep writing.
|
|
|
|
|
The buffer size at which this is called can be controlled through
|
|
|
|
|
the transport's ``set_write_buffer_limits()`` method.
|
|
|
|
|
|
|
|
|
|
- ``resume_writing()``. Tells the protocol that it is safe to start
|
2014-02-24 17:22:46 -05:00
|
|
|
|
writing data to the transport again. Note that this may be called
|
|
|
|
|
directly by the transport's ``write()`` method (as opposed to being
|
|
|
|
|
called indirectly using ``call_soon()``), so that the protocol may
|
|
|
|
|
be aware of its paused state immediately after ``write()`` returns.
|
2012-12-14 18:36:20 -05:00
|
|
|
|
|
2012-12-13 17:40:23 -05:00
|
|
|
|
- ``connection_lost(exc)``. The transport has been closed or aborted,
|
|
|
|
|
has detected that the other end has closed the connection cleanly,
|
|
|
|
|
or has encountered an unexpected error. In the first three cases
|
|
|
|
|
the argument is ``None``; for an unexpected error, the argument is
|
2013-11-01 18:14:39 -04:00
|
|
|
|
the exception that caused the transport to give up.
|
2012-12-13 17:40:23 -05:00
|
|
|
|
|
2013-11-14 16:25:58 -05:00
|
|
|
|
Here is a table indicating the order and multiplicity of the basic
|
|
|
|
|
calls:
|
2012-12-14 18:36:20 -05:00
|
|
|
|
|
2016-05-03 04:35:10 -04:00
|
|
|
|
1. ``connection_made()`` -- exactly once
|
|
|
|
|
2. ``data_received()`` -- zero or more times
|
|
|
|
|
3. ``eof_received()`` -- at most once
|
|
|
|
|
4. ``connection_lost()`` -- exactly once
|
2012-12-14 18:36:20 -05:00
|
|
|
|
|
2013-11-14 16:25:58 -05:00
|
|
|
|
Calls to ``pause_writing()`` and ``resume_writing()`` occur in pairs
|
|
|
|
|
and only between #1 and #4. These pairs will not be nested. The
|
|
|
|
|
final ``resume_writing()`` call may be omitted; i.e. a paused
|
|
|
|
|
connection may be lost and never be resumed.
|
2012-12-13 01:47:17 -05:00
|
|
|
|
|
2013-04-30 00:24:46 -04:00
|
|
|
|
Datagram Protocols
|
|
|
|
|
''''''''''''''''''
|
|
|
|
|
|
|
|
|
|
Datagram protocols have ``connection_made()`` and
|
|
|
|
|
``connection_lost()`` methods with the same signatures as stream
|
|
|
|
|
protocols. (As explained in the section about datagram transports, we
|
|
|
|
|
prefer the slightly odd nomenclature over defining different method
|
|
|
|
|
names to indicating the opening and closing of the socket.)
|
|
|
|
|
|
|
|
|
|
In addition, they have the following methods:
|
|
|
|
|
|
|
|
|
|
- ``datagram_received(data, addr)``. Indicates that a datagram
|
|
|
|
|
``data`` (a bytes objects) was received from remote address ``addr``
|
|
|
|
|
(an IPv4 2-tuple or an IPv6 4-tuple).
|
|
|
|
|
|
2013-11-15 19:47:42 -05:00
|
|
|
|
- ``error_received(exc)``. Indicates that a send or receive operation
|
|
|
|
|
raised an ``OSError`` exception. Since datagram errors may be
|
|
|
|
|
transient, it is up to the protocol to call the transport's
|
|
|
|
|
``close()`` method if it wants to close the endpoint.
|
2013-04-30 00:24:46 -04:00
|
|
|
|
|
|
|
|
|
Here is a chart indicating the order and multiplicity of calls:
|
|
|
|
|
|
2016-05-03 04:35:10 -04:00
|
|
|
|
1. ``connection_made()`` -- exactly once
|
|
|
|
|
2. ``datagram_received()``, ``error_received()`` -- zero or more times
|
|
|
|
|
3. ``connection_lost()`` -- exactly once
|
2013-11-15 19:47:42 -05:00
|
|
|
|
|
2013-04-30 00:24:46 -04:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
Subprocess Protocol
|
|
|
|
|
'''''''''''''''''''
|
|
|
|
|
|
2014-02-24 17:22:46 -05:00
|
|
|
|
Subprocess protocols have ``connection_made()``, ``connection_lost()``,
|
2013-11-14 16:25:58 -05:00
|
|
|
|
``pause_writing()`` and ``resume_writing()`` methods with the same
|
|
|
|
|
signatures as stream protocols. In addition, they have the following
|
|
|
|
|
methods:
|
|
|
|
|
|
|
|
|
|
- ``pipe_data_received(fd, data)``. Called when the subprocess writes
|
|
|
|
|
data to its stdout or stderr. ``fd`` is the file descriptor (1 for
|
2016-11-03 18:21:23 -04:00
|
|
|
|
stdout, 2 for stderr). ``data`` is a ``bytes`` object.
|
2013-11-14 16:25:58 -05:00
|
|
|
|
|
|
|
|
|
- ``pipe_connection_lost(fd, exc)``. Called when the subprocess
|
|
|
|
|
closes its stdin, stdout or stderr. ``fd`` is the file descriptor.
|
|
|
|
|
``exc`` is an exception or ``None``.
|
|
|
|
|
|
|
|
|
|
- ``process_exited()``. Called when the subprocess has exited. To
|
|
|
|
|
retrieve the exit status, use the transport's ``get_returncode()``
|
|
|
|
|
method.
|
|
|
|
|
|
|
|
|
|
Note that depending on the behavior of the subprocess it is possible
|
|
|
|
|
that ``process_exited()`` is called either before or after
|
|
|
|
|
``pipe_connection_lost()``. For example, if the subprocess creates a
|
|
|
|
|
sub-subprocess that shares its stdin/stdout/stderr and then itself
|
|
|
|
|
exits, ``process_exited()`` may be called while all the pipes are
|
2016-07-11 11:14:08 -04:00
|
|
|
|
still open. On the other hand, when the subprocess closes its
|
2013-11-14 16:25:58 -05:00
|
|
|
|
stdin/stdout/stderr but does not exit, ``pipe_connection_lost()`` may
|
|
|
|
|
be called for all three pipes without ``process_exited()`` being
|
|
|
|
|
called. If (as is the more common case) the subprocess exits and
|
|
|
|
|
thereby implicitly closes all pipes, the calling order is undefined.
|
2013-10-31 18:48:48 -04:00
|
|
|
|
|
2012-12-13 01:47:17 -05:00
|
|
|
|
Callback Style
|
|
|
|
|
--------------
|
|
|
|
|
|
|
|
|
|
Most interfaces taking a callback also take positional arguments. For
|
|
|
|
|
instance, to arrange for ``foo("abc", 42)`` to be called soon, you
|
2013-04-26 17:16:59 -04:00
|
|
|
|
call ``loop.call_soon(foo, "abc", 42)``. To schedule the call
|
|
|
|
|
``foo()``, use ``loop.call_soon(foo)``. This convention greatly
|
|
|
|
|
reduces the number of small lambdas required in typical callback
|
|
|
|
|
programming.
|
2012-12-13 01:47:17 -05:00
|
|
|
|
|
2012-12-13 14:05:52 -05:00
|
|
|
|
This convention specifically does *not* support keyword arguments.
|
2012-12-13 01:47:17 -05:00
|
|
|
|
Keyword arguments are used to pass optional extra information about
|
|
|
|
|
the callback. This allows graceful evolution of the API without
|
|
|
|
|
having to worry about whether a keyword might be significant to a
|
2012-12-13 14:05:52 -05:00
|
|
|
|
callee somewhere. If you have a callback that *must* be called with a
|
2013-10-31 18:48:48 -04:00
|
|
|
|
keyword argument, you can use a lambda. For example::
|
2012-12-13 01:47:17 -05:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
loop.call_soon(lambda: foo('abc', repeat=42))
|
2012-12-13 01:47:17 -05:00
|
|
|
|
|
|
|
|
|
|
2012-12-13 20:47:40 -05:00
|
|
|
|
Coroutines and the Scheduler
|
|
|
|
|
============================
|
|
|
|
|
|
|
|
|
|
This is a separate toplevel section because its status is different
|
|
|
|
|
from the event loop interface. Usage of coroutines is optional, and
|
|
|
|
|
it is perfectly fine to write code using callbacks only. On the other
|
|
|
|
|
hand, there is only one implementation of the scheduler/coroutine API,
|
|
|
|
|
and if you're using coroutines, that's the one you're using.
|
|
|
|
|
|
2012-12-14 13:13:20 -05:00
|
|
|
|
Coroutines
|
|
|
|
|
----------
|
|
|
|
|
|
2012-12-13 20:47:40 -05:00
|
|
|
|
A coroutine is a generator that follows certain conventions. For
|
|
|
|
|
documentation purposes, all coroutines should be decorated with
|
2013-10-31 18:48:48 -04:00
|
|
|
|
``@asyncio.coroutine``, but this cannot be strictly enforced.
|
2012-12-13 20:47:40 -05:00
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
|
Coroutines use the ``yield from`` syntax introduced in :pep:`380`,
|
2012-12-13 20:47:40 -05:00
|
|
|
|
instead of the original ``yield`` syntax.
|
|
|
|
|
|
2012-12-14 13:13:20 -05:00
|
|
|
|
The word "coroutine", like the word "generator", is used for two
|
|
|
|
|
different (though related) concepts:
|
2012-12-13 20:47:40 -05:00
|
|
|
|
|
|
|
|
|
- The function that defines a coroutine (a function definition
|
2013-10-31 18:48:48 -04:00
|
|
|
|
decorated with ``asyncio.coroutine``). If disambiguation is needed
|
2013-04-30 00:24:46 -04:00
|
|
|
|
we will call this a *coroutine function*.
|
2012-12-13 20:47:40 -05:00
|
|
|
|
|
|
|
|
|
- The object obtained by calling a coroutine function. This object
|
|
|
|
|
represents a computation or an I/O operation (usually a combination)
|
2013-04-30 00:24:46 -04:00
|
|
|
|
that will complete eventually. If disambiguation is needed we will
|
|
|
|
|
call it a *coroutine object*.
|
2012-12-13 20:47:40 -05:00
|
|
|
|
|
|
|
|
|
Things a coroutine can do:
|
|
|
|
|
|
|
|
|
|
- ``result = yield from future`` -- suspends the coroutine until the
|
2013-04-30 00:24:46 -04:00
|
|
|
|
future is done, then returns the future's result, or raises an
|
|
|
|
|
exception, which will be propagated. (If the future is cancelled,
|
|
|
|
|
it will raise a ``CancelledError`` exception.) Note that tasks are
|
|
|
|
|
futures, and everything said about futures also applies to tasks.
|
2012-12-13 20:47:40 -05:00
|
|
|
|
|
|
|
|
|
- ``result = yield from coroutine`` -- wait for another coroutine to
|
|
|
|
|
produce a result (or raise an exception, which will be propagated).
|
|
|
|
|
The ``coroutine`` expression must be a *call* to another coroutine.
|
|
|
|
|
|
2013-04-30 00:24:46 -04:00
|
|
|
|
- ``return expression`` -- produce a result to the coroutine that is
|
2012-12-13 20:47:40 -05:00
|
|
|
|
waiting for this one using ``yield from``.
|
|
|
|
|
|
|
|
|
|
- ``raise exception`` -- raise an exception in the coroutine that is
|
|
|
|
|
waiting for this one using ``yield from``.
|
|
|
|
|
|
|
|
|
|
Calling a coroutine does not start its code running -- it is just a
|
|
|
|
|
generator, and the coroutine object returned by the call is really a
|
|
|
|
|
generator object, which doesn't do anything until you iterate over it.
|
|
|
|
|
In the case of a coroutine object, there are two basic ways to start
|
|
|
|
|
it running: call ``yield from coroutine`` from another coroutine
|
|
|
|
|
(assuming the other coroutine is already running!), or convert it to a
|
2013-01-09 20:07:40 -05:00
|
|
|
|
Task (see below).
|
2012-12-13 20:47:40 -05:00
|
|
|
|
|
2013-04-30 00:24:46 -04:00
|
|
|
|
Coroutines (and tasks) can only run when the event loop is running.
|
2012-12-13 20:47:40 -05:00
|
|
|
|
|
2013-01-09 20:07:40 -05:00
|
|
|
|
Waiting for Multiple Coroutines
|
|
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
|
|
To wait for multiple coroutines or Futures, two APIs similar to the
|
|
|
|
|
``wait()`` and ``as_completed()`` APIs in the ``concurrent.futures``
|
|
|
|
|
package are provided:
|
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- ``asyncio.wait(fs, timeout=None, return_when=ALL_COMPLETED)``. This
|
2013-01-09 20:07:40 -05:00
|
|
|
|
is a coroutine that waits for the Futures or coroutines given by
|
|
|
|
|
``fs`` to complete. Coroutine arguments will be wrapped in Tasks
|
|
|
|
|
(see below). This returns a Future whose result on success is a
|
|
|
|
|
tuple of two sets of Futures, ``(done, pending)``, where ``done`` is
|
|
|
|
|
the set of original Futures (or wrapped coroutines) that are done
|
|
|
|
|
(or cancelled), and ``pending`` is the rest, i.e. those that are
|
2013-04-30 00:24:46 -04:00
|
|
|
|
still not done (nor cancelled). Note that with the defaults for
|
|
|
|
|
``timeout`` and ``return_when``, ``done`` will always be an empty
|
|
|
|
|
list. Optional arguments ``timeout`` and ``return_when`` have the
|
|
|
|
|
same meaning and defaults as for ``concurrent.futures.wait()``:
|
|
|
|
|
``timeout``, if not ``None``, specifies a timeout for the overall
|
|
|
|
|
operation; ``return_when``, specifies when to stop. The constants
|
|
|
|
|
``FIRST_COMPLETED``, ``FIRST_EXCEPTION``, ``ALL_COMPLETED`` are
|
2022-01-21 06:03:51 -05:00
|
|
|
|
defined with the same values and the same meanings as in :pep:`3148`:
|
2013-01-09 20:07:40 -05:00
|
|
|
|
|
2013-09-08 22:11:55 -04:00
|
|
|
|
- ``ALL_COMPLETED`` (default): Wait until all Futures are done (or
|
|
|
|
|
until the timeout occurs).
|
2013-01-09 20:07:40 -05:00
|
|
|
|
|
2013-09-08 22:11:55 -04:00
|
|
|
|
- ``FIRST_COMPLETED``: Wait until at least one Future is done (or
|
|
|
|
|
until the timeout occurs).
|
2013-01-09 20:07:40 -05:00
|
|
|
|
|
2013-09-08 22:11:55 -04:00
|
|
|
|
- ``FIRST_EXCEPTION``: Wait until at least one Future is done but
|
|
|
|
|
not cancelled with an exception set. (The exclusion of cancelled
|
2022-01-21 06:03:51 -05:00
|
|
|
|
Futures from the condition is surprising, but :pep:`3148` does it
|
2013-05-29 18:33:55 -04:00
|
|
|
|
this way.)
|
2013-01-09 20:07:40 -05:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- ``asyncio.as_completed(fs, timeout=None)``. Returns an iterator whose
|
2013-05-29 18:33:55 -04:00
|
|
|
|
values are Futures or coroutines; waiting for successive values
|
|
|
|
|
waits until the next Future or coroutine from the set ``fs``
|
|
|
|
|
completes, and returns its result (or raises its exception). The
|
|
|
|
|
optional argument ``timeout`` has the same meaning and default as it
|
|
|
|
|
does for ``concurrent.futures.wait()``: when the timeout occurs, the
|
|
|
|
|
next Future returned by the iterator will raise ``TimeoutError``
|
|
|
|
|
when waited for. Example of use::
|
2013-01-09 20:07:40 -05:00
|
|
|
|
|
|
|
|
|
for f in as_completed(fs):
|
|
|
|
|
result = yield from f # May raise an exception.
|
|
|
|
|
# Use result.
|
|
|
|
|
|
2013-05-29 18:33:55 -04:00
|
|
|
|
Note: if you do not wait for the values produced by the iterator,
|
|
|
|
|
your ``for`` loop may not make progress (since you are not allowing
|
|
|
|
|
other tasks to run).
|
2013-04-30 00:24:46 -04:00
|
|
|
|
|
2014-01-22 17:48:12 -05:00
|
|
|
|
- ``asyncio.wait_for(f, timeout)``. This is a convenience to wait for
|
|
|
|
|
a single coroutine or Future with a timeout. When a timeout occurs,
|
|
|
|
|
it cancels the task and raises TimeoutError. To avoid the task
|
2017-02-08 21:44:15 -05:00
|
|
|
|
cancellation, wrap it in ``shield()``.
|
2013-08-20 15:38:58 -04:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- ``asyncio.gather(f1, f2, ...)``. Returns a Future which waits until
|
2013-09-08 22:11:55 -04:00
|
|
|
|
all arguments (Futures or coroutines) are done and return a list of
|
|
|
|
|
their corresponding results. If one or more of the arguments is
|
|
|
|
|
cancelled or raises an exception, the returned Future is cancelled
|
|
|
|
|
or has its exception set (matching what happened to the first
|
|
|
|
|
argument), and the remaining arguments are left running in the
|
|
|
|
|
background. Cancelling the returned Future does not affect the
|
|
|
|
|
arguments. Note that coroutine arguments are converted to Futures
|
2013-10-31 18:48:48 -04:00
|
|
|
|
using ``asyncio.async()``.
|
2013-09-08 22:11:55 -04:00
|
|
|
|
|
2013-11-14 16:25:58 -05:00
|
|
|
|
- ``asyncio.shield(f)``. Wait for a Future, shielding it from
|
|
|
|
|
cancellation. This returns a Future whose result or exception
|
|
|
|
|
is exactly the same as the argument; however, if the returned
|
|
|
|
|
Future is cancelled, the argument Future is unaffected.
|
|
|
|
|
|
2013-11-25 01:43:36 -05:00
|
|
|
|
A use case for this function would be a coroutine that caches a
|
|
|
|
|
query result for a coroutine that handles a request in an HTTP
|
|
|
|
|
server. When the request is cancelled by the client, we could
|
|
|
|
|
(arguably) want the query-caching coroutine to continue to run, so
|
|
|
|
|
that when the client reconnects, the query result is (hopefully)
|
|
|
|
|
cached. This could be written e.g. as follows::
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
|
def handle_request(self, request):
|
|
|
|
|
...
|
|
|
|
|
cached_query = self.get_cache(...)
|
|
|
|
|
if cached_query is None:
|
|
|
|
|
cached_query = yield from asyncio.shield(self.fill_cache(...))
|
|
|
|
|
...
|
|
|
|
|
|
2013-04-30 00:24:46 -04:00
|
|
|
|
Sleeping
|
|
|
|
|
--------
|
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
The coroutine ``asyncio.sleep(delay)`` returns after a given time delay.
|
2013-04-30 00:24:46 -04:00
|
|
|
|
|
2012-12-13 20:47:40 -05:00
|
|
|
|
Tasks
|
|
|
|
|
-----
|
|
|
|
|
|
|
|
|
|
A Task is an object that manages an independently running coroutine.
|
2013-04-30 00:24:46 -04:00
|
|
|
|
The Task interface is the same as the Future interface, and in fact
|
|
|
|
|
``Task`` is a subclass of ``Future``. The task becomes done when its
|
|
|
|
|
coroutine returns or raises an exception; if it returns a result, that
|
|
|
|
|
becomes the task's result, if it raises an exception, that becomes the
|
|
|
|
|
task's exception.
|
2012-12-13 20:47:40 -05:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
Cancelling a task that's not done yet throws an
|
|
|
|
|
``asyncio.CancelledError`` exception into the coroutine. If the
|
|
|
|
|
coroutine doesn't catch this (or if it re-raises it) the task will be
|
2014-02-24 17:22:46 -05:00
|
|
|
|
marked as cancelled (i.e., ``cancelled()`` will return ``True``); but
|
2013-11-25 01:43:36 -05:00
|
|
|
|
if the coroutine somehow catches and ignores the exception it may
|
2014-02-24 17:22:46 -05:00
|
|
|
|
continue to execute (and ``cancelled()`` will return ``False``).
|
2012-12-13 20:47:40 -05:00
|
|
|
|
|
|
|
|
|
Tasks are also useful for interoperating between coroutines and
|
|
|
|
|
callback-based frameworks like Twisted. After converting a coroutine
|
|
|
|
|
into a Task, callbacks can be added to the Task.
|
|
|
|
|
|
2013-09-08 22:11:55 -04:00
|
|
|
|
To convert a coroutine into a task, call the coroutine function and
|
2016-11-03 18:21:23 -04:00
|
|
|
|
pass the resulting coroutine object to the ``loop.create_task()``
|
|
|
|
|
method. You may also use ``asyncio.ensure_future()`` for this purpose.
|
2013-04-30 00:24:46 -04:00
|
|
|
|
|
|
|
|
|
You may ask, why not automatically convert all coroutines to Tasks?
|
2013-10-31 18:48:48 -04:00
|
|
|
|
The ``@asyncio.coroutine`` decorator could do this. However, this would
|
2013-04-30 00:24:46 -04:00
|
|
|
|
slow things down considerably in the case where one coroutine calls
|
|
|
|
|
another (and so on), as switching to a "bare" coroutine has much less
|
|
|
|
|
overhead than switching to a Task.
|
2012-12-13 20:47:40 -05:00
|
|
|
|
|
2016-11-03 18:19:44 -04:00
|
|
|
|
The ``Task`` class is derived from ``Future`` adding new methods:
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
|
|
|
|
- ``current_task(loop=None)``. A *class method* returning the
|
|
|
|
|
currently running task in an event loop. If *loop* is ``None`` the
|
2016-11-03 18:19:44 -04:00
|
|
|
|
method returns the current task for the default loop. Every
|
|
|
|
|
coroutine is executed inside a *task context*, either a ``Task``
|
|
|
|
|
created using ``ensure_future()`` or ``loop.create_task()``, or by
|
|
|
|
|
being called from another coroutine using ``yield from`` or
|
|
|
|
|
``await``. This method returns ``None`` when called *outside* a
|
|
|
|
|
coroutine, e.g. in a callback scheduled using ``loop.call_later()``.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
|
|
|
|
- ``all_tasks(loop=None)``. A *class method* returning a set of all
|
2016-11-03 18:19:44 -04:00
|
|
|
|
active tasks for the loop. This uses the default loop if *loop* is
|
2016-11-03 18:21:23 -04:00
|
|
|
|
``None``.
|
|
|
|
|
|
|
|
|
|
|
2012-12-13 20:47:40 -05:00
|
|
|
|
The Scheduler
|
|
|
|
|
-------------
|
|
|
|
|
|
|
|
|
|
The scheduler has no public interface. You interact with it by using
|
|
|
|
|
``yield from future`` and ``yield from task``. In fact, there is no
|
|
|
|
|
single object representing the scheduler -- its behavior is
|
|
|
|
|
implemented by the ``Task`` and ``Future`` classes using only the
|
|
|
|
|
public interface of the event loop, so it will work with third-party
|
|
|
|
|
event loop implementations, too.
|
|
|
|
|
|
2013-11-25 14:44:59 -05:00
|
|
|
|
Convenience Utilities
|
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
|
|
A few functions and classes are provided to simplify the writing of
|
2021-09-17 14:18:24 -04:00
|
|
|
|
basic stream-based clients and servers, such as FTP or HTTP. These
|
2013-11-25 17:54:28 -05:00
|
|
|
|
are:
|
2013-11-25 14:44:59 -05:00
|
|
|
|
|
|
|
|
|
- ``asyncio.open_connection(host, port)``: A wrapper for
|
|
|
|
|
``EventLoop.create_connection()`` that does not require you to
|
|
|
|
|
provide a ``Protocol`` factory or class. This is a coroutine that
|
|
|
|
|
returns a ``(reader, writer)`` pair, where ``reader`` is an instance
|
|
|
|
|
of ``StreamReader`` and ``writer`` is an instance of
|
|
|
|
|
``StreamWriter`` (both described below).
|
|
|
|
|
|
|
|
|
|
- ``asyncio.start_server(client_connected_cb, host, port)``: A wrapper
|
|
|
|
|
for ``EventLoop.create_server()`` that takes a simple callback
|
|
|
|
|
function rather than a ``Protocol`` factory or class. This is a
|
|
|
|
|
coroutine that returns a ``Server`` object just as
|
|
|
|
|
``create_server()`` does. Each time a client connection is
|
|
|
|
|
accepted, ``client_connected_cb(reader, writer)`` is called, where
|
|
|
|
|
``reader`` is an instance of ``StreamReader`` and ``writer`` is an
|
|
|
|
|
instance of ``StreamWriter`` (both described below). If the result
|
|
|
|
|
returned by ``client_connected_cb()`` is a coroutine, it is
|
|
|
|
|
automatically wrapped in a ``Task``.
|
|
|
|
|
|
|
|
|
|
- ``StreamReader``: A class offering an interface not unlike that of a
|
|
|
|
|
read-only binary stream, except that the various reading methods are
|
|
|
|
|
coroutines. It is normally driven by a ``StreamReaderProtocol``
|
|
|
|
|
instance. Note that there should be only one reader. The interface
|
|
|
|
|
for the reader is:
|
|
|
|
|
|
|
|
|
|
- ``readline()``: A coroutine that reads a string of bytes
|
|
|
|
|
representing a line of text ending in ``'\n'``, or until the end
|
|
|
|
|
of the stream, whichever comes first.
|
|
|
|
|
|
|
|
|
|
- ``read(n)``: A coroutine that reads up to ``n`` bytes. If ``n``
|
|
|
|
|
is omitted or negative, it reads until the end of the stream.
|
|
|
|
|
|
|
|
|
|
- ``readexactly(n)``: A coroutine that reads exactly ``n`` bytes, or
|
|
|
|
|
until the end of the stream, whichever comes first.
|
|
|
|
|
|
|
|
|
|
- ``exception()``: Return the exception that has been set on the
|
|
|
|
|
stream using ``set_exception()``, or None if no exception is set.
|
|
|
|
|
|
|
|
|
|
The interface for the driver is:
|
|
|
|
|
|
|
|
|
|
- ``feed_data(data)``: Append ``data`` (a ``bytes`` object) to the
|
|
|
|
|
internal buffer. This unblocks a blocked reading coroutine if it
|
|
|
|
|
provides sufficient data to fulfill the reader's contract.
|
|
|
|
|
|
2014-02-24 17:22:46 -05:00
|
|
|
|
- ``feed_eof()``: Signal the end of the buffer. This unblocks a
|
2013-11-25 14:44:59 -05:00
|
|
|
|
blocked reading coroutine. No more data should be fed to the
|
|
|
|
|
reader after this call.
|
|
|
|
|
|
|
|
|
|
- ``set_exception(exc)``: Set an exception on the stream. All
|
|
|
|
|
subsequent reading methods will raise this exception. No more
|
|
|
|
|
data should be fed to the reader after this call.
|
|
|
|
|
|
|
|
|
|
- ``StreamWriter``: A class offering an interface not unlike that of a
|
|
|
|
|
write-only binary stream. It wraps a transport. The interface is
|
|
|
|
|
an extended subset of the transport interface: the following methods
|
|
|
|
|
behave the same as the corresponding transport methods: ``write()``,
|
|
|
|
|
``writelines()``, ``write_eof()``, ``can_write_eof()``,
|
|
|
|
|
``get_extra_info()``, ``close()``. Note that the writing methods
|
|
|
|
|
are _not_ coroutines (this is the same as for transports, but
|
|
|
|
|
different from the ``StreamReader`` class). The following method is
|
|
|
|
|
in addition to the transport interface:
|
|
|
|
|
|
|
|
|
|
- ``drain()``: This should be called with ``yield from`` after
|
|
|
|
|
writing significant data, for the purpose of flow control. The
|
|
|
|
|
intended use is like this::
|
|
|
|
|
|
|
|
|
|
writer.write(data)
|
|
|
|
|
yield from writer.drain()
|
|
|
|
|
|
|
|
|
|
Note that this is not technically a coroutine: it returns either a
|
|
|
|
|
Future or an empty tuple (both can be passed to ``yield from``).
|
|
|
|
|
Use of this method is optional. However, when it is not used, the
|
|
|
|
|
internal buffer of the transport underlying the ``StreamWriter``
|
|
|
|
|
may fill up with all data that was ever written to the writer. If
|
|
|
|
|
an app does not have a strict limit on how much data it writes, it
|
|
|
|
|
_should_ call ``yield from drain()`` occasionally to avoid filling
|
|
|
|
|
up the transport buffer.
|
|
|
|
|
|
|
|
|
|
- ``StreamReaderProtocol``: A protocol implementation used as an
|
|
|
|
|
adapter between the bidirectional stream transport/protocol
|
|
|
|
|
interface and the ``StreamReader`` and ``StreamWriter`` classes. It
|
|
|
|
|
acts as a driver for a specific ``StreamReader`` instance, calling
|
|
|
|
|
its methods ``feed_data()``, ``feed_eof()``, and ``set_exception()``
|
|
|
|
|
in response to various protocol callbacks. It also controls the
|
|
|
|
|
behavior of the ``drain()`` method of the ``StreamWriter`` instance.
|
|
|
|
|
|
2012-12-13 20:47:40 -05:00
|
|
|
|
|
2013-11-25 01:43:36 -05:00
|
|
|
|
Synchronization
|
|
|
|
|
===============
|
|
|
|
|
|
|
|
|
|
Locks, events, conditions and semaphores modeled after those in the
|
|
|
|
|
``threading`` module are implemented and can be accessed by importing
|
2017-02-08 21:44:15 -05:00
|
|
|
|
the ``asyncio.locks`` submodule. Queues modeled after those in the
|
2013-11-25 01:43:36 -05:00
|
|
|
|
``queue`` module are implemented and can be accessed by importing the
|
|
|
|
|
``asyncio.queues`` submodule.
|
|
|
|
|
|
|
|
|
|
In general these have a close correspondence to their threaded
|
2014-02-24 17:22:46 -05:00
|
|
|
|
counterparts, however, blocking methods (e.g. ``acquire()`` on locks,
|
2013-11-25 01:43:36 -05:00
|
|
|
|
``put()`` and ``get()`` on queues) are coroutines, and timeout
|
|
|
|
|
parameters are not provided (you can use ``asyncio.wait_for()`` to add
|
|
|
|
|
a timeout to a blocking call, however).
|
|
|
|
|
|
|
|
|
|
The docstrings in the modules provide more complete documentation.
|
|
|
|
|
|
|
|
|
|
Locks
|
|
|
|
|
-----
|
|
|
|
|
|
|
|
|
|
The following classes are provided by ``asyncio.locks``. For all
|
|
|
|
|
these except ``Event``, the ``with`` statement may be used in
|
|
|
|
|
combination with ``yield from`` to acquire the lock and ensure that
|
|
|
|
|
the lock is released regardless of how the ``with`` block is left, as
|
|
|
|
|
follows::
|
|
|
|
|
|
|
|
|
|
with (yield from my_lock):
|
|
|
|
|
...
|
2013-10-31 18:48:48 -04:00
|
|
|
|
|
|
|
|
|
|
2013-11-25 01:43:36 -05:00
|
|
|
|
- ``Lock``: a basic mutex, with methods ``acquire()`` (a coroutine),
|
|
|
|
|
``locked()``, and ``release()``.
|
2013-10-31 18:48:48 -04:00
|
|
|
|
|
2013-11-25 01:43:36 -05:00
|
|
|
|
- ``Event``: an event variable, with methods ``wait()`` (a coroutine),
|
|
|
|
|
``set()``, ``clear()``, and ``is_set()``.
|
2013-11-14 16:25:58 -05:00
|
|
|
|
|
2013-11-25 01:43:36 -05:00
|
|
|
|
- ``Condition``: a condition variable, with methods ``acquire()``,
|
|
|
|
|
``wait()``, ``wait_for(predicate)`` (all three coroutines),
|
|
|
|
|
``locked()``, ``release()``, ``notify()``, and ``notify_all()``.
|
2013-11-14 16:25:58 -05:00
|
|
|
|
|
2013-11-25 01:43:36 -05:00
|
|
|
|
- ``Semaphore``: a semaphore, with methods ``acquire()`` (a
|
|
|
|
|
coroutine), ``locked()``, and ``release()``. The constructor
|
|
|
|
|
argument is the initial value (default ``1``).
|
|
|
|
|
|
|
|
|
|
- ``BoundedSemaphore``: a bounded semaphore; this is similar to
|
|
|
|
|
``Semaphore`` but the initial value is also the maximum value.
|
|
|
|
|
|
|
|
|
|
Queues
|
|
|
|
|
------
|
2013-11-14 16:25:58 -05:00
|
|
|
|
|
2013-11-25 01:43:36 -05:00
|
|
|
|
The following classes and exceptions are provided by ``asyncio.queues``.
|
2013-10-31 18:48:48 -04:00
|
|
|
|
|
2013-11-25 01:43:36 -05:00
|
|
|
|
- ``Queue``: a standard queue, with methods ``get()``, ``put()`` (both
|
2014-02-24 17:22:46 -05:00
|
|
|
|
coroutines), ``get_nowait()``, ``put_nowait()``, ``empty()``,
|
2013-11-25 01:43:36 -05:00
|
|
|
|
``full()``, ``qsize()``, and ``maxsize()``.
|
|
|
|
|
|
|
|
|
|
- ``PriorityQueue``: a subclass of ``Queue`` that retrieves entries
|
|
|
|
|
in priority order (lowest first).
|
|
|
|
|
|
2014-02-24 17:22:46 -05:00
|
|
|
|
- ``LifoQueue``: a subclass of ``Queue`` that retrieves the most
|
2013-11-25 01:43:36 -05:00
|
|
|
|
recently added entries first.
|
|
|
|
|
|
|
|
|
|
- ``JoinableQueue``: a subclass of ``Queue`` with ``task_done()`` and
|
|
|
|
|
``join()`` methods (the latter a coroutine).
|
|
|
|
|
|
|
|
|
|
- ``Empty``, ``Full``: exceptions raised when ``get_nowait()`` or
|
|
|
|
|
``put_nowait()`` is called on a queue that is empty or full,
|
|
|
|
|
respectively.
|
|
|
|
|
|
|
|
|
|
|
2013-11-25 17:54:28 -05:00
|
|
|
|
Miscellaneous
|
|
|
|
|
=============
|
2013-11-25 01:43:36 -05:00
|
|
|
|
|
2013-11-25 17:54:28 -05:00
|
|
|
|
Logging
|
|
|
|
|
-------
|
2013-11-25 01:43:36 -05:00
|
|
|
|
|
2013-11-25 17:54:28 -05:00
|
|
|
|
All logging performed by the ``asyncio`` package uses a single
|
|
|
|
|
``logging.Logger`` object, ``asyncio.logger``. To customize logging
|
|
|
|
|
you can use the standard ``Logger`` API on this object. (Do not
|
|
|
|
|
replace the object though.)
|
|
|
|
|
|
|
|
|
|
``SIGCHLD`` handling on UNIX
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
Efficient implementation of the ``process_exited()`` method on
|
|
|
|
|
subprocess protocols requires a ``SIGCHLD`` signal handler. However,
|
|
|
|
|
signal handlers can only be set on the event loop associated with the
|
|
|
|
|
main thread. In order to support spawning subprocesses from event
|
|
|
|
|
loops running in other threads, a mechanism exists to allow sharing a
|
|
|
|
|
``SIGCHLD`` handler between multiple event loops. There are two
|
|
|
|
|
additional functions, ``asyncio.get_child_watcher()`` and
|
|
|
|
|
``asyncio.set_child_watcher()``, and corresponding methods on the
|
|
|
|
|
event loop policy.
|
|
|
|
|
|
|
|
|
|
There are two child watcher implementation classes,
|
|
|
|
|
``FastChildWatcher`` and ``SafeChildWatcher``. Both use ``SIGCHLD``.
|
|
|
|
|
The ``SafeChildWatcher`` class is used by default; it is inefficient
|
|
|
|
|
when many subprocesses exist simultaneously. The ``FastChildWatcher``
|
|
|
|
|
class is efficient, but it may interfere with other code (either C
|
|
|
|
|
code or Python code) that spawns subprocesses without using an
|
|
|
|
|
``asyncio`` event loop. If you are sure you are not using other code
|
|
|
|
|
that spawns subprocesses, to use the fast implementation, run the
|
|
|
|
|
following in your main thread::
|
|
|
|
|
|
|
|
|
|
watcher = asyncio.FastChildWatcher()
|
|
|
|
|
asyncio.set_child_watcher(watcher)
|
2013-10-31 18:48:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Wish List
|
|
|
|
|
=========
|
|
|
|
|
|
2013-11-25 01:43:36 -05:00
|
|
|
|
(There is agreement that these features are desirable, but no
|
|
|
|
|
implementation was available when Python 3.4 beta 1 was released, and
|
|
|
|
|
the feature freeze for the rest of the Python 3.4 release cycle
|
|
|
|
|
prohibits adding them in this late stage. However, they will
|
|
|
|
|
hopefully be added in Python 3.5, and perhaps earlier in the PyPI
|
|
|
|
|
distribution.)
|
2013-11-14 16:25:58 -05:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- Support a "start TLS" operation to upgrade a TCP socket to SSL/TLS.
|
|
|
|
|
|
2014-02-20 19:55:12 -05:00
|
|
|
|
Former wish list items that have since been implemented (but aren't
|
|
|
|
|
specified by the PEP):
|
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- UNIX domain sockets.
|
|
|
|
|
|
|
|
|
|
- A per-loop error handling callback.
|
|
|
|
|
|
|
|
|
|
|
2012-12-13 01:47:17 -05:00
|
|
|
|
Open Issues
|
|
|
|
|
===========
|
|
|
|
|
|
2013-11-25 01:43:36 -05:00
|
|
|
|
(Note that these have been resolved de facto in favor of the status
|
|
|
|
|
quo by the acceptance of the PEP. However, the PEP's provisional
|
|
|
|
|
status allows revising these decisions for Python 3.5.)
|
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- Why do ``create_connection()`` and ``create_datagram_endpoint()``
|
|
|
|
|
have a ``proto`` argument but not ``create_server()``? And why are
|
|
|
|
|
the family, flag, proto arguments for ``getaddrinfo()`` sometimes
|
|
|
|
|
zero and sometimes named constants (whose value is also zero)?
|
2013-04-29 14:49:58 -04:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- Do we need another inquiry method to tell whether the loop is in the
|
2014-02-24 17:22:46 -05:00
|
|
|
|
process of stopping?
|
2013-04-29 14:49:58 -04:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- A fuller public API for Handle? What's the use case?
|
2013-04-29 14:49:58 -04:00
|
|
|
|
|
2012-12-20 12:26:11 -05:00
|
|
|
|
- A debugging API? E.g. something that logs a lot of stuff, or logs
|
|
|
|
|
unusual conditions (like queues filling up faster than they drain)
|
|
|
|
|
or even callbacks taking too much time...
|
2012-12-13 14:58:47 -05:00
|
|
|
|
|
|
|
|
|
- Do we need introspection APIs? E.g. asking for the read callback
|
|
|
|
|
given a file descriptor. Or when the next scheduled call is. Or
|
2013-04-29 14:49:58 -04:00
|
|
|
|
the list of file descriptors registered with callbacks. Right now
|
2013-10-31 18:48:48 -04:00
|
|
|
|
these all require using internals.
|
2013-04-29 14:49:58 -04:00
|
|
|
|
|
2013-10-31 18:48:48 -04:00
|
|
|
|
- Do we need more socket I/O methods, e.g. ``sock_sendto()`` and
|
|
|
|
|
``sock_recvfrom()``, and perhaps others like ``pipe_read()``?
|
|
|
|
|
I guess users can write their own (it's not rocket science).
|
2012-12-20 12:26:11 -05:00
|
|
|
|
|
2013-01-05 18:32:00 -05:00
|
|
|
|
- We may need APIs to control various timeouts. E.g. we may want to
|
2013-10-24 11:37:55 -04:00
|
|
|
|
limit the time spent in DNS resolution, connecting, ssl/tls handshake,
|
2013-01-05 18:32:00 -05:00
|
|
|
|
idle connection, close/shutdown, even per session. Possibly it's
|
2013-04-26 17:16:59 -04:00
|
|
|
|
sufficient to add ``timeout`` keyword arguments to some methods,
|
2013-01-05 18:32:00 -05:00
|
|
|
|
and other timeouts can probably be implemented by clever use of
|
|
|
|
|
``call_later()`` and ``Task.cancel()``. But it's possible that some
|
|
|
|
|
operations need default timeouts, and we may want to change the
|
|
|
|
|
default for a specific operation globally (i.e., per event loop).
|
|
|
|
|
|
2012-12-12 20:35:17 -05:00
|
|
|
|
|
2013-01-06 20:27:43 -05:00
|
|
|
|
References
|
|
|
|
|
==========
|
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
|
- :pep:`492` describes the semantics of ``async/await``.
|
2016-11-03 18:21:23 -04:00
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
|
- :pep:`380` describes the semantics of ``yield from``.
|
2013-04-26 17:16:59 -04:00
|
|
|
|
|
|
|
|
|
- Greg Ewing's ``yield from`` tutorials:
|
|
|
|
|
http://www.cosc.canterbury.ac.nz/greg.ewing/python/yield-from/yield_from.html
|
2013-01-06 20:27:43 -05:00
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
|
- :pep:`3148` describes ``concurrent.futures.Future``.
|
2013-01-06 20:27:43 -05:00
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
|
- :pep:`3153`, while rejected, has a good write-up explaining the need
|
2013-01-06 20:27:43 -05:00
|
|
|
|
to separate transports and protocols.
|
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
|
- :pep:`418` discusses the issues of timekeeping.
|
2013-05-02 17:11:08 -04:00
|
|
|
|
|
2013-02-06 17:37:40 -05:00
|
|
|
|
- Tulip repo: http://code.google.com/p/tulip/
|
|
|
|
|
|
2013-10-23 11:17:05 -04:00
|
|
|
|
- PyPI: the Python Package Index at http://pypi.python.org/
|
|
|
|
|
|
2013-01-06 20:27:43 -05:00
|
|
|
|
- Nick Coghlan wrote a nice blog post with some background, thoughts
|
|
|
|
|
about different approaches to async I/O, gevent, and how to use
|
|
|
|
|
futures with constructs like ``while``, ``for`` and ``with``:
|
|
|
|
|
http://python-notes.boredomandlaziness.org/en/latest/pep_ideas/async_programming.html
|
|
|
|
|
|
|
|
|
|
- TBD: references to the relevant parts of Twisted, Tornado, ZeroMQ,
|
|
|
|
|
pyftpdlib, libevent, libev, pyev, libuv, wattle, and so on.
|
|
|
|
|
|
|
|
|
|
|
2012-12-12 20:35:17 -05:00
|
|
|
|
Acknowledgments
|
|
|
|
|
===============
|
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
|
Apart from :pep:`3153`, influences include :pep:`380` and Greg Ewing's
|
2013-10-31 18:48:48 -04:00
|
|
|
|
tutorial for ``yield from``, Twisted, Tornado, ZeroMQ, pyftpdlib, and
|
|
|
|
|
wattle (Steve Dower's counter-proposal). My previous work on
|
|
|
|
|
asynchronous support in the NDB library for Google App Engine provided
|
|
|
|
|
an important starting point.
|
|
|
|
|
|
|
|
|
|
I am grateful for the numerous discussions on python-ideas from
|
|
|
|
|
September through December 2012, and many more on python-tulip since
|
|
|
|
|
then; a Skype session with Steve Dower and Dino Viehland; email
|
|
|
|
|
exchanges with and a visit by Ben Darnell; an audience with Niels
|
|
|
|
|
Provos (original author of libevent); and in-person meetings (as well
|
|
|
|
|
as frequent email exchanges) with several Twisted developers,
|
|
|
|
|
including Glyph, Brian Warner, David Reid, and Duncan McGreggor.
|
|
|
|
|
|
2013-12-26 18:12:56 -05:00
|
|
|
|
Contributors to the implementation include
|
2013-10-31 18:48:48 -04:00
|
|
|
|
Eli Bendersky,
|
2013-11-20 13:34:19 -05:00
|
|
|
|
Gustavo Carneiro (Gambit Research),
|
2013-10-31 18:48:48 -04:00
|
|
|
|
Saúl Ibarra Corretgé,
|
|
|
|
|
Geert Jansen,
|
|
|
|
|
A. Jesse Jiryu Davis,
|
|
|
|
|
Nikolay Kim,
|
2013-12-26 18:12:56 -05:00
|
|
|
|
Charles-François Natali,
|
2013-10-31 18:48:48 -04:00
|
|
|
|
Richard Oudkerk,
|
|
|
|
|
Antoine Pitrou,
|
|
|
|
|
Giampaolo Rodolá,
|
|
|
|
|
Andrew Svetlov,
|
|
|
|
|
and many others who submitted bugs and/or fixes.
|
|
|
|
|
|
|
|
|
|
I thank Antoine Pitrou for his feedback in his role of official PEP
|
|
|
|
|
BDFL.
|
2012-12-12 20:35:17 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
|
|
|
|
=========
|
|
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
..
|
|
|
|
|
Local Variables:
|
|
|
|
|
mode: indented-text
|
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
|
sentence-end-double-space: t
|
|
|
|
|
fill-column: 70
|
|
|
|
|
coding: utf-8
|
|
|
|
|
End:
|