forked from python/peps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpep-3156.txt
2117 lines (1678 loc) · 93.3 KB
/
pep-3156.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
PEP: 3156
Title: Asynchronous IO Support Rebooted: the "asyncio" Module
Version: $Revision$
Last-Modified: $Date$
Author: Guido van Rossum <[email protected]>
BDFL-Delegate: Antoine Pitrou <[email protected]>
Discussions-To: [email protected]
Status: Final
Type: Standards Track
Content-Type: text/x-rst
Created: 12-Dec-2012
Post-History: 21-Dec-2012
Replaces: 3153
Resolution: https://mail.python.org/pipermail/python-dev/2013-November/130419.html
Abstract
========
This is a proposal for asynchronous I/O in Python 3, starting at
Python 3.3. Consider this the concrete proposal that is missing from
:pep:`3153`. The proposal includes a pluggable event loop, transport and
protocol abstractions similar to those in Twisted, and a higher-level
scheduler based on ``yield from`` (:pep:`380`). The proposed package
name is ``asyncio``.
Introduction
============
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
repo, with changes occasionally merged into the CPython repo.
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
packages, and no C code, except for the (optional) IOCP support on
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
----------------
The event loop is the place where most interoperability occurs. It
should be easy for (Python 3.3 ports of) frameworks like Twisted,
Tornado, or even gevents to either adapt the default event loop
implementation to their needs using a lightweight adapter or proxy, or
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.)
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
are specified:
- An interface for managing the current event loop
- The interface of a conforming event loop
An event loop implementation may provide additional methods and
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
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.)
The event loop API does not depend on ``await/yield from``. Rather, it uses
a combination of callbacks, additional interfaces (transports and
protocols), and Futures. The latter are similar to those defined in
:pep:`3148`, but have a different implementation and are not tied to
threads. In particular, the ``result()`` method raises an exception
instead of blocking when a result is not yet ready; the user is
expected to use callbacks (or ``await/yield from``) to wait for the result.
All event loop methods specified as returning a coroutine are allowed
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
function, ``ensure_future()``, exists to convert an argument that is either a
coroutine or a Future into a Future.)
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
380 ``yield from`` or :pep:`492` ``await`` expressions.
The scheduler is not pluggable;
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.)
For interoperability between code written using coroutines and other
async frameworks, the scheduler defines a Task class that behaves like a
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.
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.
A less ambitious framework may just call the
``loop.set_task_factory()`` to replace the Task class without
implementing its own event loop.
The event loop API provides limited interoperability with threads:
there is an API to submit a function to an executor (see :pep:`3148`)
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.
Transports and Protocols
------------------------
For those not familiar with Twisted, a quick explanation of the
relationship between transports and protocols is in order. At the
highest level, the transport is concerned with *how* bytes are
transmitted, while the protocol determines *which* bytes to transmit
(and to some extent when).
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
been received. Neither transport nor protocol methods "block" -- they
set events into motion and then return.
The most common type of transport is a bidirectional stream transport.
It represents a pair of buffered streams (one in each direction) that
each transmit a sequence of bytes. The most common example of a
bidirectional stream transport is probably a TCP connection. Another
common example is an SSL/TLS connection. But there are some other things
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
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()``.
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
the network (or another process, or whatever low-level interface it
wraps), and the other end talks to the protocol. The former uses
whatever API is necessary to implement the transport; but the
interface between transport and protocol is standardized by this PEP.
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.
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.
Details of the interfaces defined by the various standard types of
transports and protocols are given later.
Event Loop Interface Specification
==================================
Event Loop Policy: Getting and Setting the Current Event Loop
-------------------------------------------------------------
Event loop management is controlled by an event loop policy, which is
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.
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
sequence the policy is set, in order to avoid undefined behavior when
multiple active frameworks want to override the default policy.
(See also "Embedded Event Loops" below.)
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
object, i.e. an instance of ``AbstractEventLoop``, or ``None``.
It is okay to set the current event loop to ``None``, in
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
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
``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.
For the benefit of unit tests and other special cases there's a third
policy function: ``new_event_loop()``, which creates and returns a new
event loop object according to the policy's default rules. To make
this the current event loop, you must call ``set_event_loop()`` with
it.
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
``new_event_loop()``, all behaving like the functions described above.
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
be retrieved by calling ``get_event_loop_policy()``.
TBD: describe child watchers and UNIX quirks for subprocess processing.
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.
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
for the standard library ``time`` module.
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``.
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
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.
- ``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
is not specified by this PEP; it is just an implementation
detail of the ``ProactorEventLoop`` class.)
Event Loop Methods Overview
---------------------------
The methods of a conforming event loop are grouped into several
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. :-)
- Starting, stopping and closing: ``run_forever()``,
``run_until_complete()``, ``stop()``, ``is_running()``, ``close()``,
``is_closed()``.
- Basic and timed callbacks: ``call_soon()``, ``call_later()``,
``call_at()``, ``time()``.
- Thread interaction: ``call_soon_threadsafe()``,
``run_in_executor()``, ``set_default_executor()``.
- Internet name lookups: ``getaddrinfo()``, ``getnameinfo()``.
- Internet connections: ``create_connection()``, ``create_server()``,
``create_datagram_endpoint()``.
- Wrapped socket methods: ``sock_recv()``, ``sock_sendall()``,
``sock_connect()``, ``sock_accept()``.
- Tasks and futures support: ``create_future()``, ``create_task()``,
``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()``.
The second set of categories *may* be supported by conforming event
loop implementations. If not supported, they will raise
``NotImplementedError``. (In the default implementation,
``SelectorEventLoop`` on UNIX systems supports all of these;
``SelectorEventLoop`` on Windows supports the I/O event handling
category; ``ProactorEventLoop`` on Windows supports the pipes and
subprocess category.)
- I/O callbacks: ``add_reader()``, ``remove_reader()``,
``add_writer()``, ``remove_writer()``.
- Pipes and subprocesses: ``connect_read_pipe()``,
``connect_write_pipe()``, ``subprocess_shell()``,
``subprocess_exec()``.
- Signal callbacks: ``add_signal_handler()``,
``remove_signal_handler()``.
Event Loop Methods
------------------
Starting, Stopping and Closing
''''''''''''''''''''''''''''''
An (unclosed) event loop can be in one of two states: running or
stopped. These methods deal with starting and stopping an event loop:
- ``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
called anyway.)
- ``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.
The method creates a new ``Task`` object if the
parameter is a coroutine.
- ``stop()``. Stops the event loop as soon as it is convenient. It
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
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.
- ``is_closed()``. Returns ``True`` if the event loop is closed,
``False`` otherwise. (Primarily intended for error reporting;
please don't implement functionality based on this method.)
Basic Callbacks
'''''''''''''''
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
shared state, each callback is guaranteed that while it is running, the
shared state isn't changed by another callback.
- ``call_soon(callback, *args)``. This schedules a callback to be
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.
- ``call_later(delay, callback, *args)``. Arrange for
``callback(*args)`` to be called approximately ``delay`` seconds in
the future, once, unless cancelled. Returns a ``Handle`` representing
the callback, whose ``cancel()`` method can be used to cancel the
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()``,
but the time is expressed as an absolute time. Returns a similar
``Handle``. There is a simple equivalency: ``loop.call_later(delay,
callback, *args)`` is the same as ``loop.call_at(loop.time() +
delay, callback, *args)``.
- ``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.
(No clock is perfect -- see :pep:`418`.)
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
there are many traps and pitfalls here for the unaware (see :pep:`418`),
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.
Thread interaction
''''''''''''''''''
- ``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
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.
- ``run_in_executor(executor, callback, *args)``. Arrange to call
``callback(*args)`` in an executor (see :pep:`3148`). Returns an
``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.)
- ``set_default_executor(executor)``. Set the default executor used
by ``run_in_executor()``. The argument must be a :pep:`3148`
``Executor`` instance or ``None``, in order to reset the default
executor.
See also the ``wrap_future()`` function described in the section about
Futures.
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()``,
``create_server()`` or ``create_datagram_endpoint()``.
- ``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
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
``run_in_executor()``, but other implementations may choose to
implement their own DNS lookup. The optional arguments *must* be
specified as keyword arguments.
Note: implementations are allowed to implement a subset of the full
socket.getaddrinfo() interface; e.g. they may not support symbolic
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.)
- ``getnameinfo(sockaddr, flags=0)``. Similar to
``socket.getnameinfo()`` but returns a Future. The Future's result
on success will be a tuple ``(host, port)``. Same implementation
remarks as for ``getaddrinfo()``.
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.
- ``create_connection(protocol_factory, host, port, <options>)``.
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
connection. It creates an implementation-dependent bidirectional
stream Transport to represent the connection, then calls
``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.
(*) There is no requirement that ``protocol_factory`` is a class.
If your protocol class needs to have specific arguments passed to
its constructor, you can use ``lambda``.
You can also pass a trivial ``lambda`` that returns a previously
constructed Protocol instance.
The <options> are all specified using optional keyword arguments:
- ``ssl``: Pass ``True`` to create an SSL/TLS transport (by default
a plain TCP transport is created). Or pass an ``ssl.SSLContext``
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``
option, calls ``set_default_verify_paths()`` and sets ``verify_mode``
to ``CERT_REQUIRED``. In addition, whenever the context (default
or otherwise) specifies a ``verify_mode`` of ``CERT_REQUIRED`` or
``CERT_OPTIONAL``, if a hostname is given, immediately after a
successful handshake ``ssl.match_hostname(peercert, hostname)`` is
called, and if this raises an exception the connection is closed.
(To avoid this behavior, pass in an SSL context that has
``verify_mode`` set to ``CERT_NONE``. But this means you are not
secure, and vulnerable to for example man-in-the-middle attacks.)
- ``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
``host``, ``port``, ``family``, ``proto`` and ``flags``
arguments. If this is given, ``host`` and ``port`` must be
explicitly set to ``None``.
- ``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()``.
- ``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.
- ``create_server(protocol_factory, host, port, <options>)``.
Enters a serving loop that accepts connections.
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
to stop the serving loop in a controlled fashion (see below).
Multiple sockets may be bound if the specified address allows
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
the network side of the connection, and the two are tied together by
calling ``protocol.connection_made(transport)``.
(**) See previous footnote for ``create_connection()``. However, since
``protocol_factory()`` is called once for each new incoming
connection, it should return a new Protocol object each time it is
called.
The <options> are all specified using optional keyword arguments:
- ``ssl``: Pass an ``ssl.SSLContext`` object (or an object with the
same interface) to override the default SSL context object to be
used. (Unlike for ``create_connection()``, passing ``True`` does
not make sense here -- the ``SSLContext`` object is needed to
specify the certificate and key.)
- ``backlog``: Backlog value to be passed to the ``listen()`` call.
The default is implementation-dependent; in the default
implementation the default value is ``100``.
- ``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
``host``, ``port``, ``family`` and ``flags`` arguments. If this
is given, ``host`` and ``port`` must be explicitly set to ``None``.
- ``create_datagram_endpoint(protocol_factory, local_addr=None,
remote_addr=None, <options>)``. Creates an endpoint for sending and
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
protocol to send datagrams. The transport returned is a
``DatagramTransport``. The protocol returned is a
``DatagramProtocol``. These are described later.
Mandatory positional argument:
- ``protocol_factory``: A class or factory function that will be
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:
- ``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,
no ``sock_connect()`` call will be made.
The <options> are all specified using optional keyword arguments:
- ``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
``send()``.
- ``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
one kind of pseudo-tty on OS X that is supported by ``select`` and
``poll`` but not by ``kqueue``: it is used by Emacs shell windows.)
- ``add_reader(fd, callback, *args)``. Arrange for
``callback(*args)`` to be called whenever file descriptor ``fd`` is
deemed ready for reading. Calling ``add_reader()`` again for the
same file descriptor implies a call to ``remove_reader()`` for the
same file descriptor.
- ``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
descriptor ``fd``, if one is set. If no callback is currently set
for the file descriptor, this is a no-op and returns ``False``.
Otherwise, it removes the callback arrangement and returns ``True``.
- ``remove_writer(fd)``. This is to ``add_writer()`` as
``remove_reader()`` is to ``add_reader()``.
Pipes and Subprocesses
''''''''''''''''''''''
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.
- ``connect_read_pipe(protocol_factory, pipe)``: Create a
unidrectional stream connection from a file-like object wrapping the
read end of a UNIX pipe, which must be in non-blocking mode. The
transport returned is a ``ReadTransport``.
- ``connect_write_pipe(protocol_factory, pipe)``: Create a
unidrectional stream connection from a file-like object wrapping the
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
``connect_write_pipe()``, or the constant ``subprocess.PIPE`` (the
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
``connect_read_pipe()``, or the constant ``subprocess.PIPE`` (the
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
``connect_read_pipe()``, or one of the constants ``subprocess.PIPE``
(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
pipe as the standard output stream.
- ``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.
Signal callbacks
''''''''''''''''
These methods are only supported on UNIX.
- ``add_signal_handler(sig, callback, *args)``. Whenever signal
``sig`` is received, arrange for ``callback(*args)`` to be called.
Specifying another callback for the same signal replaces the
previous handler (only one handler can be active per signal). The
``sig`` must be a valid signal number defined in the ``signal``
module. If the signal cannot be handled this raises an exception:
``ValueError`` if it is not a valid signal or if it is an
uncatchable signal (e.g. ``SIGKILL``), ``RuntimeError`` if this
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).
- ``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
may raise ``NotImplementedError`` instead of ``RuntimeError``.
Mutual Exclusion of Callbacks
-----------------------------
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()``.
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.
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.)
The event loop passes the latter category into its *exception
handler*. This is a callback which accepts a *context* dict as a
parameter::
def exception_handler(context):
...
*context* may have many different keys but several of them are very
widely used:
- ``'message'``: error message.
- ``'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.
The loop has the following methods related to exception handling:
- ``get_exception_handler()`` returns the current exception handler
registered for the loop.
- ``set_exception_handler(handler)`` sets the exception handler.
- ``default_exception_handler(context)`` the *default* exception