blob: 3b13a81a5b6aa399f8c912df4bb6390b1624c34e [file] [log] [blame]
R David Murray6a143812013-12-20 14:37:39 -05001.. currentmodule:: asyncio
Victor Stinnerea3183f2013-12-03 01:08:00 +01002
Yury Selivanov512d7102018-09-17 19:35:30 -04003
4==========
5Event Loop
6==========
7
8
9.. rubric:: Preface
10
11The event loop is the core of every asyncio application.
12Event loops run asynchronous tasks and callbacks, perform network
13IO operations, and run subprocesses.
14
15Application developers should typically use the high-level asyncio functions,
16such as :func:`asyncio.run`, and should rarely need to reference the loop
17object or call its methods. This section is intended mostly for authors
18of lower-level code, libraries, and frameworks, who need finer control over
19the event loop behavior.
20
21.. rubric:: Obtaining the Event Loop
22
23The following low-level functions can be used to get, set, or create
24an event loop:
25
26.. function:: get_running_loop()
27
28 Return the running event loop in the current OS thread.
29
30 If there is no running event loop a :exc:`RuntimeError` is raised.
31 This function can only be called from a coroutine or a callback.
32
33 .. versionadded:: 3.7
34
35.. function:: get_event_loop()
36
37 Get the current event loop. If there is no current event loop set
38 in the current OS thread and :func:`set_event_loop` has not yet
39 been called, asyncio will create a new event loop and set it as the
40 current one.
41
42 Because this function has rather complex behavior (especially
43 when custom event loop policies are in use), using the
44 :func:`get_running_loop` function is preferred to :func:`get_event_loop`
45 in coroutines and callbacks.
46
47 Consider also using the :func:`asyncio.run` function instead of using
48 lower level functions to manually create and close an event loop.
49
50.. function:: set_event_loop(loop)
51
52 Set *loop* as a current event loop for the current OS thread.
53
54.. function:: new_event_loop()
55
56 Create a new event loop object.
57
58Note that the behaviour of :func:`get_event_loop`, :func:`set_event_loop`,
59and :func:`new_event_loop` functions can be altered by
60:ref:`setting a custom event loop policy <asyncio-policies>`.
61
62
63.. rubric:: Contents
64
65This documentation page contains the following sections:
66
67* The `Event Loop Methods`_ section is the reference documentation of
68 the event loop APIs;
69
70* The `Callback Handles`_ section documents the :class:`Handle` and
71 :class:`TimerHandle` instances which are returned from scheduling
72 methods such as :meth:`loop.call_soon` and :meth:`loop.call_later`;
73
74* The `Server Objects`_ section documents types returned from
75 event loop methods like :meth:`loop.create_server`;
76
77* The `Event Loop Implementations`_ section documents the
78 :class:`SelectorEventLoop` and :class:`ProactorEventLoop` classes;
79
80* The `Examples`_ section showcases how to work with some event
81 loop APIs.
82
83
Victor Stinner9592edb2014-02-02 15:03:02 +010084.. _asyncio-event-loop:
Victor Stinnerea3183f2013-12-03 01:08:00 +010085
Yury Selivanov512d7102018-09-17 19:35:30 -040086Event Loop Methods
87==================
Victor Stinnerea3183f2013-12-03 01:08:00 +010088
Yury Selivanov512d7102018-09-17 19:35:30 -040089Event loops have **low-level** APIs for the following:
lf627d2c82017-07-25 17:03:51 -060090
Yury Selivanov512d7102018-09-17 19:35:30 -040091.. contents::
92 :depth: 1
93 :local:
Victor Stinnerea3183f2013-12-03 01:08:00 +010094
Victor Stinnerea3183f2013-12-03 01:08:00 +010095
Yury Selivanov512d7102018-09-17 19:35:30 -040096Running and stopping the loop
97^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +010098
Yury Selivanov512d7102018-09-17 19:35:30 -040099.. method:: loop.run_until_complete(future)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100100
Yury Selivanov512d7102018-09-17 19:35:30 -0400101 Run until the *future* (an instance of :class:`Future`) has
102 completed.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100103
Yury Selivanov512d7102018-09-17 19:35:30 -0400104 If the argument is a :ref:`coroutine object <coroutine>` it
105 is implicitly scheduled to run as a :class:`asyncio.Task`.
Eli Bendersky136fea22014-02-09 06:55:58 -0800106
Yury Selivanov512d7102018-09-17 19:35:30 -0400107 Return the Future's result or raise its exception.
Guido van Rossumf68afd82016-08-08 09:41:21 -0700108
Yury Selivanov512d7102018-09-17 19:35:30 -0400109.. method:: loop.run_forever()
Guido van Rossumf68afd82016-08-08 09:41:21 -0700110
Yury Selivanov512d7102018-09-17 19:35:30 -0400111 Run the event loop until :meth:`stop` is called.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100112
Yury Selivanov512d7102018-09-17 19:35:30 -0400113 If :meth:`stop` is called before :meth:`run_forever()` is called,
114 the loop will poll the I/O selector once with a timeout of zero,
115 run all callbacks scheduled in response to I/O events (and
116 those that were already scheduled), and then exit.
Victor Stinner83704962015-02-25 14:24:15 +0100117
Guido van Rossum41f69f42015-11-19 13:28:47 -0800118 If :meth:`stop` is called while :meth:`run_forever` is running,
Yury Selivanov512d7102018-09-17 19:35:30 -0400119 the loop will run the current batch of callbacks and then exit.
120 Note that new callbacks scheduled by callbacks will not run in this
121 case; instead, they will run the next time :meth:`run_forever` or
122 :meth:`run_until_complete` is called.
Guido van Rossum41f69f42015-11-19 13:28:47 -0800123
Yury Selivanov512d7102018-09-17 19:35:30 -0400124.. method:: loop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100125
Yury Selivanov512d7102018-09-17 19:35:30 -0400126 Stop the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100127
Yury Selivanov512d7102018-09-17 19:35:30 -0400128.. method:: loop.is_running()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100129
Yury Selivanov512d7102018-09-17 19:35:30 -0400130 Return ``True`` if the event loop is currently running.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100131
Yury Selivanov512d7102018-09-17 19:35:30 -0400132.. method:: loop.is_closed()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100133
Yury Selivanov512d7102018-09-17 19:35:30 -0400134 Return ``True`` if the event loop was closed.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100135
Yury Selivanov512d7102018-09-17 19:35:30 -0400136.. method:: loop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100137
Yury Selivanov512d7102018-09-17 19:35:30 -0400138 Close the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100139
Yury Selivanov512d7102018-09-17 19:35:30 -0400140 The loop must be running when this function is called.
141 Any pending callbacks will be discarded.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100142
Yury Selivanov512d7102018-09-17 19:35:30 -0400143 This method clears all queues and shuts down the executor, but does
144 not wait for the executor to finish.
Guido van Rossum41f69f42015-11-19 13:28:47 -0800145
Yury Selivanov512d7102018-09-17 19:35:30 -0400146 This method is idempotent and irreversible. No other methods
147 should be called after the event loop is closed.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100148
Yury Selivanov512d7102018-09-17 19:35:30 -0400149.. coroutinemethod:: loop.shutdown_asyncgens()
Yury Selivanov03660042016-12-15 17:36:05 -0500150
151 Schedule all currently open :term:`asynchronous generator` objects to
152 close with an :meth:`~agen.aclose()` call. After calling this method,
Yury Selivanov512d7102018-09-17 19:35:30 -0400153 the event loop will issue a warning if a new asynchronous generator
154 is iterated. This should be used to reliably finalize all scheduled
155 asynchronous generators, e.g.::
156
Yury Selivanov03660042016-12-15 17:36:05 -0500157
158 try:
159 loop.run_forever()
160 finally:
161 loop.run_until_complete(loop.shutdown_asyncgens())
162 loop.close()
163
164 .. versionadded:: 3.6
165
166
Yury Selivanov512d7102018-09-17 19:35:30 -0400167Scheduling callbacks
168^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100169
Yury Selivanov512d7102018-09-17 19:35:30 -0400170.. method:: loop.call_soon(callback, *args, context=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100171
Yury Selivanov512d7102018-09-17 19:35:30 -0400172 Schedule a *callback* to be called with *args* arguments at
173 the next iteration of the event loop.
Victor Stinner8464c242014-11-28 13:15:41 +0100174
Yury Selivanov512d7102018-09-17 19:35:30 -0400175 Callbacks are called in the order in which they are registered.
176 Each callback will be called exactly once.
Victor Stinner8464c242014-11-28 13:15:41 +0100177
Yury Selivanov512d7102018-09-17 19:35:30 -0400178 An optional keyword-only *context* argument allows specifying a
179 custom :class:`contextvars.Context` for the *callback* to run in.
180 The current context is used when no *context* is provided.
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700181
Yury Selivanov1096f762015-06-25 13:49:52 -0400182 An instance of :class:`asyncio.Handle` is returned, which can be
Yury Selivanov512d7102018-09-17 19:35:30 -0400183 used later to cancel the callback.
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500184
Yury Selivanov512d7102018-09-17 19:35:30 -0400185 This method is not thread-safe.
Victor Stinner8464c242014-11-28 13:15:41 +0100186
Yury Selivanov512d7102018-09-17 19:35:30 -0400187.. method:: loop.call_soon_threadsafe(callback, *args, context=None)
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700188
Yury Selivanov512d7102018-09-17 19:35:30 -0400189 A thread-safe variant of :meth:`call_soon`. Must be used to
190 schedule callbacks *from another thread*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100191
Victor Stinner83704962015-02-25 14:24:15 +0100192 See the :ref:`concurrency and multithreading <asyncio-multithreading>`
193 section of the documentation.
194
Yury Selivanov512d7102018-09-17 19:35:30 -0400195.. versionchanged:: 3.7
196 The *context* keyword-only parameter was added. See :pep:`567`
197 for more details.
198
199.. _asyncio-pass-keywords:
200
201.. note::
202
203 Most :mod:`asyncio` scheduling functions don't allow passing
204 keyword arguments. To do that, use :func:`functools.partial`::
205
206 # will schedule "print("Hello", flush=True)"
207 loop.call_soon(
208 functools.partial(print, "Hello", flush=True))
209
210 Using partial objects is usually more convenient than using lambdas,
211 as asyncio can render partial objects better in debug and error
212 messages.
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700213
Victor Stinnerea3183f2013-12-03 01:08:00 +0100214
Victor Stinner45b27ed2014-02-01 02:36:43 +0100215.. _asyncio-delayed-calls:
216
Yury Selivanov512d7102018-09-17 19:35:30 -0400217Scheduling delayed callbacks
218^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100219
Yury Selivanov512d7102018-09-17 19:35:30 -0400220Event loop provides mechanisms to schedule callback functions
221to be called at some point in the future. Event loop uses monotonic
222clocks to track time.
Victor Stinnerfd9d3742014-02-18 09:37:43 +0100223
Victor Stinner45b27ed2014-02-01 02:36:43 +0100224
Yury Selivanov512d7102018-09-17 19:35:30 -0400225.. method:: loop.call_later(delay, callback, *args, context=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100226
Yury Selivanov512d7102018-09-17 19:35:30 -0400227 Schedule *callback* to be called after the given *delay*
228 number of seconds (can be either an int or a float).
Victor Stinnerea3183f2013-12-03 01:08:00 +0100229
Yury Selivanov512d7102018-09-17 19:35:30 -0400230 An instance of :class:`asyncio.TimerHandle` is returned which can
231 be used to cancel the callback.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100232
Yury Selivanov512d7102018-09-17 19:35:30 -0400233 *callback* will be called exactly once. If two callbacks are
234 scheduled for exactly the same time, the order in which they
235 are called is undefined.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100236
Yury Selivanov512d7102018-09-17 19:35:30 -0400237 The optional positional *args* will be passed to the callback when
238 it is called. If you want the callback to be called with keyword
239 arguments use :func:`functools.partial`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100240
Yury Selivanov512d7102018-09-17 19:35:30 -0400241 An optional keyword-only *context* argument allows specifying a
242 custom :class:`contextvars.Context` for the *callback* to run in.
243 The current context is used when no *context* is provided.
Victor Stinner8464c242014-11-28 13:15:41 +0100244
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700245 .. versionchanged:: 3.7
246 The *context* keyword-only parameter was added. See :pep:`567`
247 for more details.
248
Yury Selivanov512d7102018-09-17 19:35:30 -0400249 .. versionchanged:: 3.7.1
250 In Python 3.7.0 and earlier with the default event loop implementation,
251 the *delay* could not exceed one day.
252 This has been fixed in Python 3.7.1.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100253
Yury Selivanov512d7102018-09-17 19:35:30 -0400254.. method:: loop.call_at(when, callback, *args, context=None)
255
256 Schedule *callback* to be called at the given absolute timestamp
257 *when* (an int or a float), using the same time reference as
258 :meth:`loop.time`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100259
260 This method's behavior is the same as :meth:`call_later`.
261
Yury Selivanov512d7102018-09-17 19:35:30 -0400262 An instance of :class:`asyncio.TimerHandle` is returned which can
263 be used to cancel the callback.
Victor Stinner8464c242014-11-28 13:15:41 +0100264
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700265 .. versionchanged:: 3.7
266 The *context* keyword-only parameter was added. See :pep:`567`
267 for more details.
268
Yury Selivanov512d7102018-09-17 19:35:30 -0400269 .. versionchanged:: 3.7.1
270 In Python 3.7.0 and earlier with the default event loop implementation,
271 the difference between *when* and the current time could not exceed
272 one day. This has been fixed in Python 3.7.1.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100273
Yury Selivanov512d7102018-09-17 19:35:30 -0400274.. method:: loop.time()
275
276 Return the current time, as a :class:`float` value, according to
277 the event loop's internal monotonic clock.
278
279.. note::
280
281 Timeouts (relative *delay* or absolute *when*) should not
282 exceed one day.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100283
Victor Stinner3e09e322013-12-03 01:22:06 +0100284.. seealso::
285
286 The :func:`asyncio.sleep` function.
287
Victor Stinnerea3183f2013-12-03 01:08:00 +0100288
Yury Selivanov512d7102018-09-17 19:35:30 -0400289Creating Futures and Tasks
290^^^^^^^^^^^^^^^^^^^^^^^^^^
Yury Selivanov950204d2016-05-16 16:23:00 -0400291
Yury Selivanov512d7102018-09-17 19:35:30 -0400292.. method:: loop.create_future()
Yury Selivanov950204d2016-05-16 16:23:00 -0400293
Yury Selivanov512d7102018-09-17 19:35:30 -0400294 Create an :class:`asyncio.Future` object attached to the event loop.
Yury Selivanov950204d2016-05-16 16:23:00 -0400295
Yury Selivanov512d7102018-09-17 19:35:30 -0400296 This is the preferred way to create Futures in asyncio. This lets
297 third-party event loops provide alternative implementations of
298 the Future object (with better performance or instrumentation).
Yury Selivanov950204d2016-05-16 16:23:00 -0400299
300 .. versionadded:: 3.5.2
301
Yury Selivanov512d7102018-09-17 19:35:30 -0400302.. method:: loop.create_task(coro)
Yury Selivanov950204d2016-05-16 16:23:00 -0400303
Yury Selivanov512d7102018-09-17 19:35:30 -0400304 Schedule the execution of a :ref:`coroutine`.
305 Return a :class:`Task` object.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200306
Yury Selivanov512d7102018-09-17 19:35:30 -0400307 Third-party event loops can use their own subclass of :class:`Task`
308 for interoperability. In this case, the result type is a subclass
309 of :class:`Task`.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200310
Yury Selivanov512d7102018-09-17 19:35:30 -0400311.. method:: loop.set_task_factory(factory)
Yury Selivanov71854612015-05-11 16:28:27 -0400312
313 Set a task factory that will be used by
Yury Selivanov512d7102018-09-17 19:35:30 -0400314 :meth:`loop.create_task`.
Yury Selivanov71854612015-05-11 16:28:27 -0400315
316 If *factory* is ``None`` the default task factory will be set.
Yury Selivanov512d7102018-09-17 19:35:30 -0400317 Otherwise, *factory* must be a *callable* with the signature matching
318 ``(loop, coro)``, where *loop* is a reference to the active
319 event loop, and *coro* is a coroutine object. The callable
320 must return a :class:`asyncio.Future`-compatible object.
Yury Selivanov71854612015-05-11 16:28:27 -0400321
Yury Selivanov512d7102018-09-17 19:35:30 -0400322.. method:: loop.get_task_factory()
Yury Selivanov71854612015-05-11 16:28:27 -0400323
Yury Selivanov512d7102018-09-17 19:35:30 -0400324 Return a task factory or ``None`` if the default one is in use.
Yury Selivanov71854612015-05-11 16:28:27 -0400325
Victor Stinner530ef2f2014-07-08 12:39:10 +0200326
Yury Selivanov512d7102018-09-17 19:35:30 -0400327Opening network connections
328^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100329
Yury Selivanov512d7102018-09-17 19:35:30 -0400330.. coroutinemethod:: loop.create_connection(protocol_factory, \
331 host=None, port=None, \*, ssl=None, \
332 family=0, proto=0, flags=0, sock=None, \
333 local_addr=None, server_hostname=None, \
334 ssl_handshake_timeout=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100335
Yury Selivanov512d7102018-09-17 19:35:30 -0400336 Open a streaming transport connection to a given
337 address specified by *host* and *port*.
338
339 The socket family can be either :py:data:`~socket.AF_INET` or
340 :py:data:`~socket.AF_INET6` depending on *host* (or the *family*
341 argument, if provided).
342
343 The socket type will be :py:data:`~socket.SOCK_STREAM`.
344
345 *protocol_factory* must be a callable returning an
346 :ref:`asyncio protocol <asyncio-protocol>` implementation.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100347
Yury Selivanov19a44f62017-12-14 20:53:26 -0500348 This method will try to establish the connection in the background.
349 When successful, it returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100350
351 The chronological synopsis of the underlying operation is as follows:
352
Yury Selivanov512d7102018-09-17 19:35:30 -0400353 #. The connection is established and a :ref:`transport <asyncio-transport>`
354 is created for it.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100355
Yury Selivanov512d7102018-09-17 19:35:30 -0400356 #. *protocol_factory* is called without arguments and is expected to
357 return a :ref:`protocol <asyncio-protocol>` instance.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100358
Yury Selivanov512d7102018-09-17 19:35:30 -0400359 #. The protocol instance is coupled with the transport by calling its
360 :meth:`~BaseProtocol.connection_made` method.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100361
Yury Selivanov512d7102018-09-17 19:35:30 -0400362 #. A ``(transport, protocol)`` tuple is returned on success.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100363
Yury Selivanov512d7102018-09-17 19:35:30 -0400364 The created transport is an implementation-dependent bidirectional
365 stream.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100366
Yury Selivanov512d7102018-09-17 19:35:30 -0400367 Other arguments:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100368
369 * *ssl*: if given and not false, a SSL/TLS transport is created
370 (by default a plain TCP transport is created). If *ssl* is
371 a :class:`ssl.SSLContext` object, this context is used to create
Yury Selivanov512d7102018-09-17 19:35:30 -0400372 the transport; if *ssl* is :const:`True`, a default context returned
373 from :func:`ssl.create_default_context` is used.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100374
Berker Peksag9c1dba22014-09-28 00:00:58 +0300375 .. seealso:: :ref:`SSL/TLS security considerations <ssl-security>`
Antoine Pitrouc5e075f2014-03-22 18:19:11 +0100376
Yury Selivanov512d7102018-09-17 19:35:30 -0400377 * *server_hostname* sets or overrides the hostname that the target
378 server's certificate will be matched against. Should only be passed
379 if *ssl* is not ``None``. By default the value of the *host* argument
Victor Stinnerea3183f2013-12-03 01:08:00 +0100380 is used. If *host* is empty, there is no default and you must pass a
381 value for *server_hostname*. If *server_hostname* is an empty
382 string, hostname matching is disabled (which is a serious security
Yury Selivanov512d7102018-09-17 19:35:30 -0400383 risk, allowing for potential man-in-the-middle attacks).
Victor Stinnerea3183f2013-12-03 01:08:00 +0100384
385 * *family*, *proto*, *flags* are the optional address family, protocol
386 and flags to be passed through to getaddrinfo() for *host* resolution.
387 If given, these should all be integers from the corresponding
388 :mod:`socket` module constants.
389
390 * *sock*, if given, should be an existing, already connected
391 :class:`socket.socket` object to be used by the transport.
392 If *sock* is given, none of *host*, *port*, *family*, *proto*, *flags*
393 and *local_addr* should be specified.
394
395 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
396 to bind the socket to locally. The *local_host* and *local_port*
Yury Selivanov512d7102018-09-17 19:35:30 -0400397 are looked up using ``getaddrinfo()``, similarly to *host* and *port*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100398
Yury Selivanov512d7102018-09-17 19:35:30 -0400399 * *ssl_handshake_timeout* is (for a TLS connection) the time in seconds
400 to wait for the TLS handshake to complete before aborting the connection.
Miss Islington (bot)87936d02018-06-04 09:05:46 -0700401 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000402
403 .. versionadded:: 3.7
404
405 The *ssl_handshake_timeout* parameter.
406
Yury Selivanov512d7102018-09-17 19:35:30 -0400407 .. versionchanged:: 3.6
408
409 The socket option :py:data:`~socket.TCP_NODELAY` is set by default
410 for all TCP connections.
411
Victor Stinner60208a12015-09-15 22:41:52 +0200412 .. versionchanged:: 3.5
413
Yury Selivanov512d7102018-09-17 19:35:30 -0400414 Added support for SSL/TLS in :class:`ProactorEventLoop`.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200415
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100416 .. seealso::
417
Yury Selivanov512d7102018-09-17 19:35:30 -0400418 The :func:`open_connection` function is a high-level alternative
419 API. It returns a pair of (:class:`StreamReader`, :class:`StreamWriter`)
420 that can be used directly in async/await code.
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100421
Yury Selivanov512d7102018-09-17 19:35:30 -0400422.. coroutinemethod:: loop.create_datagram_endpoint(protocol_factory, \
423 local_addr=None, remote_addr=None, \*, \
424 family=0, proto=0, flags=0, \
425 reuse_address=None, reuse_port=None, \
426 allow_broadcast=None, sock=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100427
Yury Selivanov512d7102018-09-17 19:35:30 -0400428 Create a datagram connection.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100429
Yury Selivanov512d7102018-09-17 19:35:30 -0400430 The socket family can be either :py:data:`~socket.AF_INET`,
431 :py:data:`~socket.AF_INET6`, or :py:data:`~socket.AF_UNIX`,
432 depending on *host* (or the *family* argument, if provided).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100433
Yury Selivanov512d7102018-09-17 19:35:30 -0400434 The socket type will be :py:data:`~socket.SOCK_DGRAM`.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100435
Yury Selivanov512d7102018-09-17 19:35:30 -0400436 *protocol_factory* must be a callable returning a
437 :ref:`protocol <asyncio-protocol>` implementation.
438
439 A tuple of ``(transport, protocol)`` is returned on success.
440
441 Other arguments:
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700442
443 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
444 to bind the socket to locally. The *local_host* and *local_port*
445 are looked up using :meth:`getaddrinfo`.
446
447 * *remote_addr*, if given, is a ``(remote_host, remote_port)`` tuple used
448 to connect the socket to a remote address. The *remote_host* and
449 *remote_port* are looked up using :meth:`getaddrinfo`.
450
451 * *family*, *proto*, *flags* are the optional address family, protocol
452 and flags to be passed through to :meth:`getaddrinfo` for *host*
453 resolution. If given, these should all be integers from the
454 corresponding :mod:`socket` module constants.
455
456 * *reuse_address* tells the kernel to reuse a local socket in
Yury Selivanov512d7102018-09-17 19:35:30 -0400457 ``TIME_WAIT`` state, without waiting for its natural timeout to
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300458 expire. If not specified will automatically be set to ``True`` on
Yury Selivanov512d7102018-09-17 19:35:30 -0400459 Unix.
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700460
461 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
462 same port as other existing endpoints are bound to, so long as they all
463 set this flag when being created. This option is not supported on Windows
Yury Selivanov512d7102018-09-17 19:35:30 -0400464 and some Unixes. If the :py:data:`~socket.SO_REUSEPORT` constant is not
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700465 defined then this capability is unsupported.
466
467 * *allow_broadcast* tells the kernel to allow this endpoint to send
468 messages to the broadcast address.
469
470 * *sock* can optionally be specified in order to use a preexisting,
471 already connected, :class:`socket.socket` object to be used by the
472 transport. If specified, *local_addr* and *remote_addr* should be omitted
473 (must be :const:`None`).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100474
Yury Selivanov512d7102018-09-17 19:35:30 -0400475 On Windows, with :class:`ProactorEventLoop`, this method is not supported.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200476
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200477 See :ref:`UDP echo client protocol <asyncio-udp-echo-client-protocol>` and
478 :ref:`UDP echo server protocol <asyncio-udp-echo-server-protocol>` examples.
479
Miss Islington (bot)c6348cf2018-05-14 13:12:38 -0700480 .. versionchanged:: 3.4.4
481 The *family*, *proto*, *flags*, *reuse_address*, *reuse_port,
482 *allow_broadcast*, and *sock* parameters were added.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100483
Yury Selivanov512d7102018-09-17 19:35:30 -0400484.. coroutinemethod:: loop.create_unix_connection(protocol_factory, \
485 path=None, \*, ssl=None, sock=None, \
486 server_hostname=None, ssl_handshake_timeout=None)
Victor Stinnera6919aa2014-02-19 13:32:34 +0100487
Yury Selivanov512d7102018-09-17 19:35:30 -0400488 Create a Unix connection.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100489
Yury Selivanov512d7102018-09-17 19:35:30 -0400490 The socket family will be :py:data:`~socket.AF_UNIX`; socket
491 type will be :py:data:`~socket.SOCK_STREAM`.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100492
Yury Selivanov512d7102018-09-17 19:35:30 -0400493 A tuple of ``(transport, protocol)`` is returned on success.
Guido van Rossum9e80eeb2016-11-03 14:17:25 -0700494
Yury Selivanov512d7102018-09-17 19:35:30 -0400495 *path* is the name of a Unix domain socket and is required,
496 unless a *sock* parameter is specified. Abstract Unix sockets,
497 :class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths are
498 supported.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100499
Yury Selivanov512d7102018-09-17 19:35:30 -0400500 See the documentation of the :meth:`loop.create_connection` method
501 for information about arguments to this method.
502
503 Availability: Unix.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100504
Neil Aspinallf7686c12017-12-19 19:45:42 +0000505 .. versionadded:: 3.7
506
507 The *ssl_handshake_timeout* parameter.
508
Yury Selivanov423fd362017-11-20 17:26:28 -0500509 .. versionchanged:: 3.7
510
Miss Islington (bot)f4dcf492018-06-08 08:54:31 -0700511 The *path* parameter can now be a :term:`path-like object`.
Yury Selivanov423fd362017-11-20 17:26:28 -0500512
Victor Stinnera6919aa2014-02-19 13:32:34 +0100513
Yury Selivanov512d7102018-09-17 19:35:30 -0400514Creating network servers
515^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100516
Yury Selivanov512d7102018-09-17 19:35:30 -0400517.. coroutinemethod:: loop.create_server(protocol_factory, \
518 host=None, port=None, \*, \
519 family=socket.AF_UNSPEC, \
520 flags=socket.AI_PASSIVE, \
521 sock=None, backlog=100, ssl=None, \
522 reuse_address=None, reuse_port=None, \
523 ssl_handshake_timeout=None, start_serving=True)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100524
Yury Selivanov512d7102018-09-17 19:35:30 -0400525 Create a TCP server (socket type :data:`~socket.SOCK_STREAM`) listening
526 on *port* of the *host* address.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200527
Yury Selivanov512d7102018-09-17 19:35:30 -0400528 Returns a :class:`Server` object.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200529
Yury Selivanov512d7102018-09-17 19:35:30 -0400530 Arguments:
Victor Stinner33f6abe2014-10-12 20:36:04 +0200531
Yury Selivanov512d7102018-09-17 19:35:30 -0400532 * *protocol_factory* must be a callable returning a
533 :ref:`protocol <asyncio-protocol>` implementation.
534
535 * The *host* parameter can be set to several types which determine where
536 the server would be listening:
537
538 - If *host* is a string, the TCP server is bound to a single network
539 interface specified by *host*.
540
541 - If *host* is a sequence of strings, the TCP server is bound to all
542 network interfaces specified by the sequence.
543
544 - If *host* is an empty string or ``None``, all interfaces are
545 assumed and a list of multiple sockets will be returned (most likely
546 one for IPv4 and another one for IPv6).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200547
548 * *family* can be set to either :data:`socket.AF_INET` or
Yury Selivanov512d7102018-09-17 19:35:30 -0400549 :data:`~socket.AF_INET6` to force the socket to use IPv4 or IPv6.
550 If not set, the *family* will be determined from host name
551 (defaults to :data:`~socket.AF_UNSPEC`).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200552
553 * *flags* is a bitmask for :meth:`getaddrinfo`.
554
555 * *sock* can optionally be specified in order to use a preexisting
Yury Selivanov512d7102018-09-17 19:35:30 -0400556 socket object. If specified, *host* and *port* must not be specified.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200557
558 * *backlog* is the maximum number of queued connections passed to
559 :meth:`~socket.socket.listen` (defaults to 100).
560
Yury Selivanov512d7102018-09-17 19:35:30 -0400561 * *ssl* can be set to an :class:`~ssl.SSLContext` instance to enable
562 TLS over the accepted connections.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200563
564 * *reuse_address* tells the kernel to reuse a local socket in
Yury Selivanov512d7102018-09-17 19:35:30 -0400565 ``TIME_WAIT`` state, without waiting for its natural timeout to
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300566 expire. If not specified will automatically be set to ``True`` on
Yury Selivanov512d7102018-09-17 19:35:30 -0400567 Unix.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100568
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700569 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
570 same port as other existing endpoints are bound to, so long as they all
571 set this flag when being created. This option is not supported on
572 Windows.
573
Yury Selivanov512d7102018-09-17 19:35:30 -0400574 * *ssl_handshake_timeout* is (for a TLS server) the time in seconds to wait
575 for the TLS handshake to complete before aborting the connection.
Miss Islington (bot)87936d02018-06-04 09:05:46 -0700576 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000577
Yury Selivanovc9070d02018-01-25 18:08:09 -0500578 * *start_serving* set to ``True`` (the default) causes the created server
579 to start accepting connections immediately. When set to ``False``,
580 the user should await on :meth:`Server.start_serving` or
581 :meth:`Server.serve_forever` to make the server to start accepting
582 connections.
583
Neil Aspinallf7686c12017-12-19 19:45:42 +0000584 .. versionadded:: 3.7
585
Yury Selivanov512d7102018-09-17 19:35:30 -0400586 Added *ssl_handshake_timeout* and *start_serving* parameters.
587
588 .. versionchanged:: 3.6
589
590 The socket option :py:data:`~socket.TCP_NODELAY` is set by default
591 for all TCP connections.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000592
Victor Stinner60208a12015-09-15 22:41:52 +0200593 .. versionchanged:: 3.5
594
Yury Selivanov512d7102018-09-17 19:35:30 -0400595 Added support for SSL/TLS in :class:`ProactorEventLoop`.
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100596
Victor Stinner7b58a2b2015-09-21 18:41:05 +0200597 .. versionchanged:: 3.5.1
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200598
Yury Selivanov512d7102018-09-17 19:35:30 -0400599 The *host* parameter can be a sequence of strings.
600
601 .. seealso::
602
603 The :func:`start_server` function is a higher-level alternative API
604 that returns a pair of :class:`StreamReader` and :class:`StreamWriter`
605 that can be used in an async/await code.
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200606
Victor Stinnerea3183f2013-12-03 01:08:00 +0100607
Yury Selivanov512d7102018-09-17 19:35:30 -0400608.. coroutinemethod:: loop.create_unix_server(protocol_factory, path=None, \
609 \*, sock=None, backlog=100, ssl=None, \
610 ssl_handshake_timeout=None, start_serving=True)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100611
Yury Selivanov512d7102018-09-17 19:35:30 -0400612 Similar to :meth:`loop.create_server` but works with the
613 :py:data:`~socket.AF_UNIX` socket family.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100614
Yury Selivanov512d7102018-09-17 19:35:30 -0400615 *path* is the name of a Unix domain socket, and is required,
616 unless a *sock* argument is provided. Abstract Unix sockets,
617 :class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths
618 are supported.
Yury Selivanov423fd362017-11-20 17:26:28 -0500619
Yury Selivanov512d7102018-09-17 19:35:30 -0400620 See the documentation of the :meth:`loop.create_server` method
621 for information about arguments to this method.
622
623 Availability: Unix.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100624
Neil Aspinallf7686c12017-12-19 19:45:42 +0000625 .. versionadded:: 3.7
626
Miss Islington (bot)f4dcf492018-06-08 08:54:31 -0700627 The *ssl_handshake_timeout* and *start_serving* parameters.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000628
Yury Selivanov423fd362017-11-20 17:26:28 -0500629 .. versionchanged:: 3.7
630
631 The *path* parameter can now be a :class:`~pathlib.Path` object.
632
Yury Selivanov512d7102018-09-17 19:35:30 -0400633.. coroutinemethod:: loop.connect_accepted_socket(protocol_factory, \
634 sock, \*, ssl=None, ssl_handshake_timeout=None)
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500635
Yury Selivanov512d7102018-09-17 19:35:30 -0400636 Wrap an already accepted connection into a transport/protocol pair.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500637
Yury Selivanov512d7102018-09-17 19:35:30 -0400638 This method can be used by servers that accept connections outside
639 of asyncio but that use asyncio to handle them.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500640
641 Parameters:
642
Yury Selivanov512d7102018-09-17 19:35:30 -0400643 * *protocol_factory* must be a callable returning a
644 :ref:`protocol <asyncio-protocol>` implementation.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500645
Yury Selivanov512d7102018-09-17 19:35:30 -0400646 * *sock* is a preexisting socket object returned from
647 :meth:`socket.accept <socket.socket.accept>`.
648
649 * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over
650 the accepted connections.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500651
Neil Aspinallf7686c12017-12-19 19:45:42 +0000652 * *ssl_handshake_timeout* is (for an SSL connection) the time in seconds to
653 wait for the SSL handshake to complete before aborting the connection.
Miss Islington (bot)87936d02018-06-04 09:05:46 -0700654 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000655
Yury Selivanov512d7102018-09-17 19:35:30 -0400656 Returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100657
Neil Aspinallf7686c12017-12-19 19:45:42 +0000658 .. versionadded:: 3.7
659
660 The *ssl_handshake_timeout* parameter.
661
AraHaan431665b2017-11-21 11:06:26 -0500662 .. versionadded:: 3.5.3
663
664
Yury Selivanov512d7102018-09-17 19:35:30 -0400665Transferring files
666^^^^^^^^^^^^^^^^^^
Andrew Svetlov7c684072018-01-27 21:22:47 +0200667
Yury Selivanov512d7102018-09-17 19:35:30 -0400668.. coroutinemethod:: loop.sendfile(transport, file, \
669 offset=0, count=None, *, fallback=True)
Andrew Svetlov7c684072018-01-27 21:22:47 +0200670
Yury Selivanov512d7102018-09-17 19:35:30 -0400671 Send a *file* over a *transport*. Return the total number of bytes
672 sent.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200673
674 The method uses high-performance :meth:`os.sendfile` if available.
675
676 *file* must be a regular file object opened in binary mode.
677
678 *offset* tells from where to start reading the file. If specified,
679 *count* is the total number of bytes to transmit as opposed to
Yury Selivanov512d7102018-09-17 19:35:30 -0400680 sending the file until EOF is reached. File position is always updated,
681 even when this method raises an error, and
682 :meth:`file.tell() <io.IOBase.tell>` can be used to obtain the actual
683 number of bytes sent.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200684
685 *fallback* set to ``True`` makes asyncio to manually read and send
Yury Selivanov512d7102018-09-17 19:35:30 -0400686 the file when the platform does not support the sendfile system call
Andrew Svetlov7c684072018-01-27 21:22:47 +0200687 (e.g. Windows or SSL socket on Unix).
688
689 Raise :exc:`SendfileNotAvailableError` if the system does not support
Yury Selivanov512d7102018-09-17 19:35:30 -0400690 the *sendfile* syscall and *fallback* is ``False``.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200691
692 .. versionadded:: 3.7
693
694
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500695TLS Upgrade
Yury Selivanov512d7102018-09-17 19:35:30 -0400696^^^^^^^^^^^
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500697
Yury Selivanov512d7102018-09-17 19:35:30 -0400698.. coroutinemethod:: loop.start_tls(transport, protocol, \
699 sslcontext, \*, server_side=False, \
700 server_hostname=None, ssl_handshake_timeout=None)
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500701
Yury Selivanov512d7102018-09-17 19:35:30 -0400702 Upgrade an existing transport-based connection to TLS.
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500703
Yury Selivanov512d7102018-09-17 19:35:30 -0400704 Return a new transport instance, that the *protocol* must start using
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500705 immediately after the *await*. The *transport* instance passed to
706 the *start_tls* method should never be used again.
707
708 Parameters:
709
710 * *transport* and *protocol* instances that methods like
Yury Selivanov512d7102018-09-17 19:35:30 -0400711 :meth:`~loop.create_server` and
712 :meth:`~loop.create_connection` return.
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500713
714 * *sslcontext*: a configured instance of :class:`~ssl.SSLContext`.
715
716 * *server_side* pass ``True`` when a server-side connection is being
Yury Selivanov512d7102018-09-17 19:35:30 -0400717 upgraded (like the one created by :meth:`~loop.create_server`).
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500718
719 * *server_hostname*: sets or overrides the host name that the target
720 server's certificate will be matched against.
721
Yury Selivanov512d7102018-09-17 19:35:30 -0400722 * *ssl_handshake_timeout* is (for a TLS connection) the time in seconds to
723 wait for the TLS handshake to complete before aborting the connection.
Miss Islington (bot)87936d02018-06-04 09:05:46 -0700724 ``60.0`` seconds if ``None`` (default).
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500725
726 .. versionadded:: 3.7
727
728
Yury Selivanov512d7102018-09-17 19:35:30 -0400729Watching file descriptors
730^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerc1567df2014-02-08 23:22:58 +0100731
Yury Selivanov512d7102018-09-17 19:35:30 -0400732.. method:: loop.add_reader(fd, callback, \*args)
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200733
Yury Selivanov512d7102018-09-17 19:35:30 -0400734 Start monitoring the *fd* file descriptor for read availability and
735 invoke *callback* with the specified arguments once *fd* is available for
736 reading.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200737
Yury Selivanov512d7102018-09-17 19:35:30 -0400738.. method:: loop.remove_reader(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100739
Yury Selivanov512d7102018-09-17 19:35:30 -0400740 Stop monitoring the *fd* file descriptor for read availability.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100741
Yury Selivanov512d7102018-09-17 19:35:30 -0400742.. method:: loop.add_writer(fd, callback, \*args)
Victor Stinner8464c242014-11-28 13:15:41 +0100743
Yury Selivanov512d7102018-09-17 19:35:30 -0400744 Start monitoring the *fd* file descriptor for write availability and
745 invoke *callback* with the specified arguments once *fd* is available for
746 writing.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100747
Yury Selivanov512d7102018-09-17 19:35:30 -0400748 Use :func:`functools.partial` :ref:`to pass keywords
749 <asyncio-pass-keywords>` to *func*.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100750
Yury Selivanov512d7102018-09-17 19:35:30 -0400751.. method:: loop.remove_writer(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100752
Yury Selivanov512d7102018-09-17 19:35:30 -0400753 Stop monitoring the *fd* file descriptor for write availability.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100754
Yury Selivanov512d7102018-09-17 19:35:30 -0400755See also :ref:`Platform Support <asyncio-platform-support>` section
756for some limitations of these methods.
Victor Stinner04e6df32014-10-11 16:16:27 +0200757
Victor Stinnerc1567df2014-02-08 23:22:58 +0100758
Yury Selivanov512d7102018-09-17 19:35:30 -0400759Working with socket objects directly
760^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerc1567df2014-02-08 23:22:58 +0100761
Yury Selivanov512d7102018-09-17 19:35:30 -0400762In general, protocol implementations that use transport-based APIs
763such as :meth:`loop.create_connection` and :meth:`loop.create_server`
764are faster than implementations that work with sockets directly.
765However, there are some use cases when performance is not critical, and
766working with :class:`~socket.socket` objects directly is more
767convenient.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100768
Yury Selivanov512d7102018-09-17 19:35:30 -0400769.. coroutinemethod:: loop.sock_recv(sock, nbytes)
Yury Selivanov55c50842016-06-08 12:48:15 -0400770
Yury Selivanov512d7102018-09-17 19:35:30 -0400771 Receive up to *nbytes* from *sock*. Asynchronous version of
772 :meth:`socket.recv() <socket.socket.recv>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100773
Yury Selivanov512d7102018-09-17 19:35:30 -0400774 Return the received data as a bytes object.
775
776 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200777
Yury Selivanov19a44f62017-12-14 20:53:26 -0500778 .. versionchanged:: 3.7
Yury Selivanov512d7102018-09-17 19:35:30 -0400779 Even though this method was always documented as a coroutine
780 method, releases before Python 3.7 returned a :class:`Future`.
781 Since Python 3.7 this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100782
Yury Selivanov512d7102018-09-17 19:35:30 -0400783.. coroutinemethod:: loop.sock_recv_into(sock, buf)
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200784
Yury Selivanov512d7102018-09-17 19:35:30 -0400785 Receive data from *sock* into the *buf* buffer. Modeled after the blocking
786 :meth:`socket.recv_into() <socket.socket.recv_into>` method.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200787
Yury Selivanov512d7102018-09-17 19:35:30 -0400788 Return the number of bytes written to the buffer.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200789
Yury Selivanov512d7102018-09-17 19:35:30 -0400790 *sock* must be a non-blocking socket.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200791
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200792 .. versionadded:: 3.7
793
Yury Selivanov512d7102018-09-17 19:35:30 -0400794.. coroutinemethod:: loop.sock_sendall(sock, data)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100795
Yury Selivanov512d7102018-09-17 19:35:30 -0400796 Send *data* to the *sock* socket. Asynchronous version of
797 :meth:`socket.sendall() <socket.socket.sendall>`.
Yury Selivanov55c50842016-06-08 12:48:15 -0400798
Yury Selivanov512d7102018-09-17 19:35:30 -0400799 This method continues to send to the socket until either all data
800 in *data* has been sent or an error occurs. ``None`` is returned
801 on success. On error, an exception is raised. Additionally, there is no way
802 to determine how much data, if any, was successfully processed by the
803 receiving end of the connection.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100804
Yury Selivanov512d7102018-09-17 19:35:30 -0400805 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200806
Yury Selivanov19a44f62017-12-14 20:53:26 -0500807 .. versionchanged:: 3.7
808 Even though the method was always documented as a coroutine
809 method, before Python 3.7 it returned an :class:`Future`.
810 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100811
Yury Selivanov512d7102018-09-17 19:35:30 -0400812.. coroutinemethod:: loop.sock_connect(sock, address)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100813
Yury Selivanov512d7102018-09-17 19:35:30 -0400814 Connect *sock* to a remote socket at *address*.
Victor Stinner1b0580b2014-02-13 09:24:37 +0100815
Yury Selivanov512d7102018-09-17 19:35:30 -0400816 Asynchronous version of :meth:`socket.connect() <socket.socket.connect>`.
817
818 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200819
Yury Selivanov55c50842016-06-08 12:48:15 -0400820 .. versionchanged:: 3.5.2
821 ``address`` no longer needs to be resolved. ``sock_connect``
822 will try to check if the *address* is already resolved by calling
823 :func:`socket.inet_pton`. If not,
Yury Selivanov512d7102018-09-17 19:35:30 -0400824 :meth:`loop.getaddrinfo` will be used to resolve the
Yury Selivanov55c50842016-06-08 12:48:15 -0400825 *address*.
826
Victor Stinnerc1567df2014-02-08 23:22:58 +0100827 .. seealso::
828
Yury Selivanov512d7102018-09-17 19:35:30 -0400829 :meth:`loop.create_connection`
Yury Selivanov55c50842016-06-08 12:48:15 -0400830 and :func:`asyncio.open_connection() <open_connection>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100831
832
Yury Selivanov512d7102018-09-17 19:35:30 -0400833.. coroutinemethod:: loop.sock_accept(sock)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100834
Yury Selivanov512d7102018-09-17 19:35:30 -0400835 Accept a connection. Modeled after the blocking
836 :meth:`socket.accept() <socket.socket.accept>` method.
Yury Selivanov55c50842016-06-08 12:48:15 -0400837
838 The socket must be bound to an address and listening
Victor Stinnerc1567df2014-02-08 23:22:58 +0100839 for connections. The return value is a pair ``(conn, address)`` where *conn*
840 is a *new* socket object usable to send and receive data on the connection,
841 and *address* is the address bound to the socket on the other end of the
842 connection.
843
Yury Selivanov512d7102018-09-17 19:35:30 -0400844 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200845
Yury Selivanov19a44f62017-12-14 20:53:26 -0500846 .. versionchanged:: 3.7
847 Even though the method was always documented as a coroutine
848 method, before Python 3.7 it returned a :class:`Future`.
849 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100850
851 .. seealso::
852
Yury Selivanov512d7102018-09-17 19:35:30 -0400853 :meth:`loop.create_server` and :func:`start_server`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100854
Yury Selivanov512d7102018-09-17 19:35:30 -0400855.. coroutinemethod:: loop.sock_sendfile(sock, file, offset=0, count=None, \
856 \*, fallback=True)
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200857
Yury Selivanov512d7102018-09-17 19:35:30 -0400858 Send a file using high-performance :mod:`os.sendfile` if possible.
859 Return the total number of bytes sent.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200860
Yury Selivanov512d7102018-09-17 19:35:30 -0400861 Asynchronous version of :meth:`socket.sendfile() <socket.socket.sendfile>`.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200862
Yury Selivanov512d7102018-09-17 19:35:30 -0400863 *sock* must be a non-blocking :const:`socket.SOCK_STREAM`
864 :class:`~socket.socket`.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200865
Yury Selivanov512d7102018-09-17 19:35:30 -0400866 *file* must be a regular file object open in binary mode.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200867
868 *offset* tells from where to start reading the file. If specified,
869 *count* is the total number of bytes to transmit as opposed to
Yury Selivanov512d7102018-09-17 19:35:30 -0400870 sending the file until EOF is reached. File position is always updated,
871 even when this method raises an error, and
872 :meth:`file.tell() <io.IOBase.tell>` can be used to obtain the actual
873 number of bytes sent.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200874
Yury Selivanov512d7102018-09-17 19:35:30 -0400875 *fallback*, when set to ``True``, makes asyncio manually read and send
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200876 the file when the platform does not support the sendfile syscall
877 (e.g. Windows or SSL socket on Unix).
878
Andrew Svetlov7464e872018-01-19 20:04:29 +0200879 Raise :exc:`SendfileNotAvailableError` if the system does not support
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200880 *sendfile* syscall and *fallback* is ``False``.
881
Yury Selivanov512d7102018-09-17 19:35:30 -0400882 *sock* must be a non-blocking socket.
883
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200884 .. versionadded:: 3.7
885
Victor Stinnerc1567df2014-02-08 23:22:58 +0100886
Yury Selivanov512d7102018-09-17 19:35:30 -0400887DNS
888^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100889
Yury Selivanov512d7102018-09-17 19:35:30 -0400890.. coroutinemethod:: loop.getaddrinfo(host, port, \*, family=0, \
891 type=0, proto=0, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100892
Yury Selivanov512d7102018-09-17 19:35:30 -0400893 Asynchronous version of :meth:`socket.getaddrinfo`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100894
Yury Selivanov512d7102018-09-17 19:35:30 -0400895.. coroutinemethod:: loop.getnameinfo(sockaddr, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100896
Yury Selivanov512d7102018-09-17 19:35:30 -0400897 Asynchronous version of :meth:`socket.getnameinfo`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100898
Yury Selivanovbec23722018-01-28 14:09:40 -0500899.. versionchanged:: 3.7
900 Both *getaddrinfo* and *getnameinfo* methods were always documented
901 to return a coroutine, but prior to Python 3.7 they were, in fact,
902 returning :class:`asyncio.Future` objects. Starting with Python 3.7
903 both methods are coroutines.
904
Victor Stinnerea3183f2013-12-03 01:08:00 +0100905
Yury Selivanov512d7102018-09-17 19:35:30 -0400906Working with pipes
907^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100908
Yury Selivanov512d7102018-09-17 19:35:30 -0400909.. coroutinemethod:: loop.connect_read_pipe(protocol_factory, pipe)
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200910
Yury Selivanov512d7102018-09-17 19:35:30 -0400911 Register the read end of *pipe* in the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100912
Yury Selivanov512d7102018-09-17 19:35:30 -0400913 *protocol_factory* must be a callable returning an
914 :ref:`asyncio protocol <asyncio-protocol>` implementation.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100915
Yury Selivanov512d7102018-09-17 19:35:30 -0400916 *pipe* is a :term:`file-like object <file object>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100917
Victor Stinner2cef3002014-10-23 22:38:46 +0200918 Return pair ``(transport, protocol)``, where *transport* supports
Yury Selivanov512d7102018-09-17 19:35:30 -0400919 the :class:`ReadTransport` interface and *protocol* is an object
920 instantiated by the *protocol_factory*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100921
Victor Stinnerd84fd732014-08-26 01:01:59 +0200922 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
923 non-blocking mode.
924
Yury Selivanov512d7102018-09-17 19:35:30 -0400925.. coroutinemethod:: loop.connect_write_pipe(protocol_factory, pipe)
926
927 Register the write end of *pipe* in the event loop.
928
929 *protocol_factory* must be a callable returning an
930 :ref:`asyncio protocol <asyncio-protocol>` implementation.
931
932 *pipe* is :term:`file-like object <file object>`.
933
934 Return pair ``(transport, protocol)``, where *transport* supports
935 :class:`WriteTransport` interface and *protocol* is an object
936 instantiated by the *protocol_factory*.
937
938 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
939 non-blocking mode.
940
941.. note::
942
943 :class:`SelectorEventLoop` does not support the above methods on
944 Windows. Use :class:`ProactorEventLoop` instead for Windows.
945
Victor Stinner08444382014-02-02 22:43:39 +0100946.. seealso::
947
Yury Selivanov512d7102018-09-17 19:35:30 -0400948 The :meth:`loop.subprocess_exec` and
949 :meth:`loop.subprocess_shell` methods.
Victor Stinner08444382014-02-02 22:43:39 +0100950
Victor Stinnerea3183f2013-12-03 01:08:00 +0100951
Yury Selivanov512d7102018-09-17 19:35:30 -0400952Unix signals
953^^^^^^^^^^^^
Victor Stinner8b863482014-01-27 10:07:50 +0100954
Yury Selivanov512d7102018-09-17 19:35:30 -0400955.. method:: loop.add_signal_handler(signum, callback, \*args)
Victor Stinner8b863482014-01-27 10:07:50 +0100956
Yury Selivanov512d7102018-09-17 19:35:30 -0400957 Set *callback* as the handler for the *signum* signal.
Victor Stinner8b863482014-01-27 10:07:50 +0100958
959 Raise :exc:`ValueError` if the signal number is invalid or uncatchable.
960 Raise :exc:`RuntimeError` if there is a problem setting up the handler.
961
Yury Selivanov512d7102018-09-17 19:35:30 -0400962 Use :func:`functools.partial` :ref:`to pass keywords
963 <asyncio-pass-keywords>` to *func*.
Victor Stinner8464c242014-11-28 13:15:41 +0100964
Yury Selivanov512d7102018-09-17 19:35:30 -0400965.. method:: loop.remove_signal_handler(sig)
Victor Stinner8b863482014-01-27 10:07:50 +0100966
Yury Selivanov512d7102018-09-17 19:35:30 -0400967 Remove the handler for the *sig* signal.
Victor Stinner8b863482014-01-27 10:07:50 +0100968
Yury Selivanov512d7102018-09-17 19:35:30 -0400969 Return ``True`` if the signal handler was removed, or ``False`` if
970 no handler was set for the given signal.
971
972Availability: Unix.
Victor Stinner8b863482014-01-27 10:07:50 +0100973
974.. seealso::
975
976 The :mod:`signal` module.
977
978
Yury Selivanov512d7102018-09-17 19:35:30 -0400979Executing code in thread or process pools
980^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100981
Yury Selivanov512d7102018-09-17 19:35:30 -0400982.. coroutinemethod:: loop.run_in_executor(executor, func, \*args)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100983
Yury Selivanov512d7102018-09-17 19:35:30 -0400984 Arrange for *func* to be called in the specified executor.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100985
Yury Selivanov512d7102018-09-17 19:35:30 -0400986 The *executor* argument should be an :class:`concurrent.futures.Executor`
Larry Hastings3732ed22014-03-15 21:13:56 -0700987 instance. The default executor is used if *executor* is ``None``.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100988
Yury Selivanov512d7102018-09-17 19:35:30 -0400989 Use :func:`functools.partial` :ref:`to pass keywords
990 <asyncio-pass-keywords>` to *func*.
Victor Stinner8464c242014-11-28 13:15:41 +0100991
Yury Selivanovbec23722018-01-28 14:09:40 -0500992 This method returns a :class:`asyncio.Future` object.
993
Yury Selivanove8a60452016-10-21 17:40:42 -0400994 .. versionchanged:: 3.5.3
Yury Selivanov512d7102018-09-17 19:35:30 -0400995 :meth:`loop.run_in_executor` no longer configures the
Yury Selivanove8a60452016-10-21 17:40:42 -0400996 ``max_workers`` of the thread pool executor it creates, instead
997 leaving it up to the thread pool executor
998 (:class:`~concurrent.futures.ThreadPoolExecutor`) to set the
999 default.
1000
Yury Selivanov512d7102018-09-17 19:35:30 -04001001.. method:: loop.set_default_executor(executor)
Victor Stinnerea3183f2013-12-03 01:08:00 +01001002
Yury Selivanov512d7102018-09-17 19:35:30 -04001003 Set *executor* as the default executor used by :meth:`run_in_executor`.
1004 *executor* should be an instance of
1005 :class:`~concurrent.futures.ThreadPoolExecutor`.
1006
1007 .. deprecated:: 3.7
1008 Using an executor that is not an instance of
1009 :class:`~concurrent.futures.ThreadPoolExecutor` is deprecated and
1010 will trigger an error in Python 3.9.
1011
1012 *executor* must be an instance of
1013 :class:`concurrent.futures.ThreadPoolExecutor`.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001014
1015
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001016Error Handling API
Yury Selivanov512d7102018-09-17 19:35:30 -04001017^^^^^^^^^^^^^^^^^^
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001018
Martin Panterc04fb562016-02-10 05:44:01 +00001019Allows customizing how exceptions are handled in the event loop.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001020
Yury Selivanov512d7102018-09-17 19:35:30 -04001021.. method:: loop.set_exception_handler(handler)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001022
1023 Set *handler* as the new event loop exception handler.
1024
1025 If *handler* is ``None``, the default exception handler will
Yury Selivanov512d7102018-09-17 19:35:30 -04001026 be set. Otherwise, *handler* must be a callable with the signature
1027 matching ``(loop, context)``, where ``loop``
1028 is a reference to the active event loop, and ``context``
1029 is a ``dict`` object containing the details of the exception
1030 (see :meth:`call_exception_handler` documentation for details
1031 about context).
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001032
Yury Selivanov512d7102018-09-17 19:35:30 -04001033.. method:: loop.get_exception_handler()
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001034
Yury Selivanov512d7102018-09-17 19:35:30 -04001035 Return the current exception handler, or ``None`` if no custom
1036 exception handler was set.
Yury Selivanov950204d2016-05-16 16:23:00 -04001037
1038 .. versionadded:: 3.5.2
1039
Yury Selivanov512d7102018-09-17 19:35:30 -04001040.. method:: loop.default_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001041
1042 Default exception handler.
1043
1044 This is called when an exception occurs and no exception
Yury Selivanov512d7102018-09-17 19:35:30 -04001045 handler is set. This can be called by a custom exception
1046 handler that wants to defer to the default handler behavior.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001047
1048 *context* parameter has the same meaning as in
1049 :meth:`call_exception_handler`.
1050
Yury Selivanov512d7102018-09-17 19:35:30 -04001051.. method:: loop.call_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001052
1053 Call the current event loop exception handler.
1054
1055 *context* is a ``dict`` object containing the following keys
Yury Selivanov512d7102018-09-17 19:35:30 -04001056 (new keys may be introduced in future Python versions):
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001057
1058 * 'message': Error message;
1059 * 'exception' (optional): Exception object;
1060 * 'future' (optional): :class:`asyncio.Future` instance;
1061 * 'handle' (optional): :class:`asyncio.Handle` instance;
1062 * 'protocol' (optional): :ref:`Protocol <asyncio-protocol>` instance;
1063 * 'transport' (optional): :ref:`Transport <asyncio-transport>` instance;
1064 * 'socket' (optional): :class:`socket.socket` instance.
1065
1066 .. note::
1067
Yury Selivanov512d7102018-09-17 19:35:30 -04001068 This method should not be overloaded in subclassed
1069 event loops. For custom exception handling, use
1070 the :meth:`set_exception_handler()` method.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001071
Yury Selivanov512d7102018-09-17 19:35:30 -04001072Enabling debug mode
1073^^^^^^^^^^^^^^^^^^^
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001074
Yury Selivanov512d7102018-09-17 19:35:30 -04001075.. method:: loop.get_debug()
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001076
Victor Stinner7b7120e2014-06-23 00:12:14 +02001077 Get the debug mode (:class:`bool`) of the event loop.
1078
1079 The default value is ``True`` if the environment variable
1080 :envvar:`PYTHONASYNCIODEBUG` is set to a non-empty string, ``False``
1081 otherwise.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001082
Yury Selivanov512d7102018-09-17 19:35:30 -04001083.. method:: loop.set_debug(enabled: bool)
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001084
1085 Set the debug mode of the event loop.
1086
Yury Selivanov512d7102018-09-17 19:35:30 -04001087 .. versionchanged:: 3.7
1088
1089 The new ``-X dev`` command line option can now also be used
1090 to enable the debug mode.
Victor Stinner64d750b2014-06-18 03:25:23 +02001091
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001092.. seealso::
1093
Victor Stinner62511fd2014-06-23 00:36:11 +02001094 The :ref:`debug mode of asyncio <asyncio-debug-mode>`.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001095
Yury Selivanov512d7102018-09-17 19:35:30 -04001096
1097Running Subprocesses
1098^^^^^^^^^^^^^^^^^^^^
1099
1100Methods described in this subsections are low-level. In regular
1101async/await code consider using the high-level
1102:func:`asyncio.create_subprocess_shell` and
1103:func:`asyncio.create_subprocess_exec` convenience functions instead.
1104
1105.. note::
1106
1107 The default asyncio event loop on **Windows** does not support
1108 subprocesses. See :ref:`Subprocess Support on Windows
1109 <asyncio-windows-subprocess>` for details.
1110
1111.. coroutinemethod:: loop.subprocess_exec(protocol_factory, \*args, \
1112 stdin=subprocess.PIPE, stdout=subprocess.PIPE, \
1113 stderr=subprocess.PIPE, \*\*kwargs)
1114
1115 Create a subprocess from one or more string arguments specified by
1116 *args*.
1117
1118 *args* must be a list of strings represented by:
1119
1120 * :class:`str`;
1121 * or :class:`bytes`, encoded to the
1122 :ref:`filesystem encoding <filesystem-encoding>`.
1123
1124 The first string specifies the program executable,
1125 and the remaining strings specify the arguments. Together, string
1126 arguments form the ``argv`` of the program.
1127
1128 This is similar to the standard library :class:`subprocess.Popen`
1129 class called with ``shell=False`` and the list of strings passed as
1130 the first argument; however, where :class:`~subprocess.Popen` takes
1131 a single argument which is list of strings, *subprocess_exec*
1132 takes multiple string arguments.
1133
1134 The *protocol_factory* must be a callable returning a subclass of the
1135 :class:`asyncio.SubprocessProtocol` class.
1136
1137 Other parameters:
1138
1139 * *stdin*: either a file-like object representing a pipe to be
1140 connected to the subprocess's standard input stream using
1141 :meth:`~loop.connect_write_pipe`, or the
1142 :const:`subprocess.PIPE` constant (default). By default a new
1143 pipe will be created and connected.
1144
1145 * *stdout*: either a file-like object representing the pipe to be
1146 connected to the subprocess's standard output stream using
1147 :meth:`~loop.connect_read_pipe`, or the
1148 :const:`subprocess.PIPE` constant (default). By default a new pipe
1149 will be created and connected.
1150
1151 * *stderr*: either a file-like object representing the pipe to be
1152 connected to the subprocess's standard error stream using
1153 :meth:`~loop.connect_read_pipe`, or one of
1154 :const:`subprocess.PIPE` (default) or :const:`subprocess.STDOUT`
1155 constants.
1156
1157 By default a new pipe will be created and connected. When
1158 :const:`subprocess.STDOUT` is specified, the subprocess' standard
1159 error stream will be connected to the same pipe as the standard
1160 output stream.
1161
1162 * All other keyword arguments are passed to :class:`subprocess.Popen`
1163 without interpretation, except for *bufsize*, *universal_newlines*
1164 and *shell*, which should not be specified at all.
1165
1166 See the constructor of the :class:`subprocess.Popen` class
1167 for documentation on other arguments.
1168
1169 Returns a pair of ``(transport, protocol)``, where *transport*
1170 conforms to the :class:`asyncio.SubprocessTransport` base class and
1171 *protocol* is an object instantiated by the *protocol_factory*.
1172
1173.. coroutinemethod:: loop.subprocess_shell(protocol_factory, cmd, \*, \
1174 stdin=subprocess.PIPE, stdout=subprocess.PIPE, \
1175 stderr=subprocess.PIPE, \*\*kwargs)
1176
1177 Create a subprocess from *cmd*, which can be a :class:`str` or a
1178 :class:`bytes` string encoded to the
1179 :ref:`filesystem encoding <filesystem-encoding>`,
1180 using the platform's "shell" syntax.
1181
1182 This is similar to the standard library :class:`subprocess.Popen`
1183 class called with ``shell=True``.
1184
1185 The *protocol_factory* must be a callable returning a subclass of the
1186 :class:`SubprocessProtocol` class.
1187
1188 See :meth:`~loop.subprocess_exec` for more details about
1189 the remaining arguments.
1190
1191 Returns a pair of ``(transport, protocol)``, where *transport*
1192 conforms to the :class:`SubprocessTransport` base class and
1193 *protocol* is an object instantiated by the *protocol_factory*.
1194
1195.. note::
1196 It is the application's responsibility to ensure that all whitespace
1197 and special characters are quoted appropriately to avoid `shell injection
1198 <https://en.wikipedia.org/wiki/Shell_injection#Shell_injection>`_
1199 vulnerabilities. The :func:`shlex.quote` function can be used to
1200 properly escape whitespace and special characters in strings that
1201 are going to be used to construct shell commands.
1202
1203
1204Callback Handles
1205================
1206
1207.. class:: Handle
1208
1209 A callback wrapper object returned by :meth:`loop.call_soon`,
1210 :meth:`loop.call_soon_threadsafe`.
1211
1212 .. method:: cancel()
1213
1214 Cancel the callback. If the callback has already been canceled
1215 or executed, this method has no effect.
1216
1217 .. method:: cancelled()
1218
1219 Return ``True`` if the callback was cancelled.
1220
1221 .. versionadded:: 3.7
1222
1223.. class:: TimerHandle
1224
1225 A callback wrapper object returned by :meth:`loop.call_later`,
1226 and :meth:`loop.call_at`.
1227
1228 This class is a subclass of :class:`Handle`.
1229
1230 .. method:: when()
1231
1232 Return a scheduled callback time as :class:`float` seconds.
1233
1234 The time is an absolute timestamp, using the same time
1235 reference as :meth:`loop.time`.
1236
1237 .. versionadded:: 3.7
1238
1239
1240Server Objects
1241==============
1242
1243Server objects are created by :meth:`loop.create_server`,
1244:meth:`loop.create_unix_server`, :func:`start_server`,
1245and :func:`start_unix_server` functions.
1246
1247Do not instantiate the class directly.
Victor Stinner8c462c52014-01-24 18:11:43 +01001248
Victor Stinner8ebeb032014-07-11 23:47:40 +02001249.. class:: Server
Victor Stinner8c462c52014-01-24 18:11:43 +01001250
Yury Selivanovc9070d02018-01-25 18:08:09 -05001251 *Server* objects are asynchronous context managers. When used in an
1252 ``async with`` statement, it's guaranteed that the Server object is
1253 closed and not accepting new connections when the ``async with``
1254 statement is completed::
1255
1256 srv = await loop.create_server(...)
1257
1258 async with srv:
1259 # some code
1260
1261 # At this point, srv is closed and no longer accepts new connections.
1262
1263
1264 .. versionchanged:: 3.7
1265 Server object is an asynchronous context manager since Python 3.7.
Victor Stinner8c462c52014-01-24 18:11:43 +01001266
1267 .. method:: close()
1268
Victor Stinner4bfb14a2014-07-12 03:20:24 +02001269 Stop serving: close listening sockets and set the :attr:`sockets`
1270 attribute to ``None``.
1271
Yury Selivanov512d7102018-09-17 19:35:30 -04001272 The sockets that represent existing incoming client connections
1273 are left open.
Victor Stinner8ebeb032014-07-11 23:47:40 +02001274
Berker Peksag49c9edf2016-01-20 07:14:22 +02001275 The server is closed asynchronously, use the :meth:`wait_closed`
1276 coroutine to wait until the server is closed.
Victor Stinner8c462c52014-01-24 18:11:43 +01001277
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)1634fc22017-12-30 20:39:32 +05301278 .. method:: get_loop()
1279
Yury Selivanov512d7102018-09-17 19:35:30 -04001280 Return the event loop associated with the server object.
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)1634fc22017-12-30 20:39:32 +05301281
1282 .. versionadded:: 3.7
1283
Yury Selivanovc9070d02018-01-25 18:08:09 -05001284 .. coroutinemethod:: start_serving()
1285
1286 Start accepting connections.
1287
1288 This method is idempotent, so it can be called when
1289 the server is already being serving.
1290
Yury Selivanov512d7102018-09-17 19:35:30 -04001291 The *start_serving* keyword-only parameter to
1292 :meth:`loop.create_server` and
1293 :meth:`asyncio.start_server` allows creating a Server object
1294 that is not accepting connections initially. In this case
1295 ``Server.start_serving()``, or :meth:`Server.serve_forever` can be used
1296 to make the Server start accepting connections.
Yury Selivanovc9070d02018-01-25 18:08:09 -05001297
1298 .. versionadded:: 3.7
1299
1300 .. coroutinemethod:: serve_forever()
1301
1302 Start accepting connections until the coroutine is cancelled.
1303 Cancellation of ``serve_forever`` task causes the server
1304 to be closed.
1305
1306 This method can be called if the server is already accepting
1307 connections. Only one ``serve_forever`` task can exist per
1308 one *Server* object.
1309
1310 Example::
1311
1312 async def client_connected(reader, writer):
1313 # Communicate with the client with
1314 # reader/writer streams. For example:
1315 await reader.readline()
1316
1317 async def main(host, port):
1318 srv = await asyncio.start_server(
1319 client_connected, host, port)
Miss Islington (bot)aeb5d732018-02-17 10:02:46 -08001320 await srv.serve_forever()
Yury Selivanovc9070d02018-01-25 18:08:09 -05001321
1322 asyncio.run(main('127.0.0.1', 0))
1323
1324 .. versionadded:: 3.7
1325
1326 .. method:: is_serving()
1327
1328 Return ``True`` if the server is accepting new connections.
1329
1330 .. versionadded:: 3.7
1331
Victor Stinnerbdd574d2015-02-12 22:49:18 +01001332 .. coroutinemethod:: wait_closed()
Victor Stinner8c462c52014-01-24 18:11:43 +01001333
Victor Stinner8ebeb032014-07-11 23:47:40 +02001334 Wait until the :meth:`close` method completes.
1335
Victor Stinner8ebeb032014-07-11 23:47:40 +02001336 .. attribute:: sockets
1337
Yury Selivanov512d7102018-09-17 19:35:30 -04001338 List of :class:`socket.socket` objects the server is listening on,
1339 or ``None`` if the server is closed.
Victor Stinner8c462c52014-01-24 18:11:43 +01001340
Yury Selivanovc9070d02018-01-25 18:08:09 -05001341 .. versionchanged:: 3.7
Yury Selivanov512d7102018-09-17 19:35:30 -04001342 Prior to Python 3.7 ``Server.sockets`` used to return an
1343 internal list of server sockets directly. In 3.7 a copy
Yury Selivanovc9070d02018-01-25 18:08:09 -05001344 of that list is returned.
1345
Victor Stinner8c462c52014-01-24 18:11:43 +01001346
Yury Selivanov512d7102018-09-17 19:35:30 -04001347.. _asyncio-event-loops:
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001348
Yury Selivanov512d7102018-09-17 19:35:30 -04001349Event Loop Implementations
1350==========================
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001351
Yury Selivanov512d7102018-09-17 19:35:30 -04001352asyncio ships with two different event loop implementations:
1353:class:`SelectorEventLoop` and :class:`ProactorEventLoop`.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001354
Yury Selivanov512d7102018-09-17 19:35:30 -04001355By default asyncio is configured to use :class:`SelectorEventLoop`
1356on all platforms.
Miss Islington (bot)71a0b0e2018-02-01 11:56:03 -08001357
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001358
Yury Selivanov512d7102018-09-17 19:35:30 -04001359.. class:: SelectorEventLoop
1360
1361 An event loop based on the :mod:`selectors` module.
1362
1363 Uses the most efficient *selector* available for the given
1364 platform. It is also possible to manually configure the
1365 exact selector implementation to be used::
1366
1367 import asyncio
1368 import selectors
1369
1370 selector = selectors.SelectSelector()
1371 loop = asyncio.SelectorEventLoop(selector)
1372 asyncio.set_event_loop(loop)
Andrew Svetlov7464e872018-01-19 20:04:29 +02001373
1374
Yury Selivanov512d7102018-09-17 19:35:30 -04001375 Availability: Unix, Windows.
Andrew Svetlov7464e872018-01-19 20:04:29 +02001376
1377
Yury Selivanov512d7102018-09-17 19:35:30 -04001378.. class:: ProactorEventLoop
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001379
Yury Selivanov512d7102018-09-17 19:35:30 -04001380 An event loop for Windows that uses "I/O Completion Ports" (IOCP).
1381
1382 Availability: Windows.
1383
1384 An example how to use :class:`ProactorEventLoop` on Windows::
1385
1386 import asyncio
1387 import sys
1388
1389 if sys.platform == 'win32':
1390 loop = asyncio.ProactorEventLoop()
1391 asyncio.set_event_loop(loop)
1392
1393 .. seealso::
1394
1395 `MSDN documentation on I/O Completion Ports
1396 <https://docs.microsoft.com/en-ca/windows/desktop/FileIO/i-o-completion-ports>`_.
1397
1398
1399.. class:: AbstractEventLoop
1400
1401 Abstract base class for asyncio-compliant event loops.
1402
1403 The :ref:`Event Loop Methods <asyncio-event-loop>` section lists all
1404 methods that an alternative implementation of ``AbstractEventLoop``
1405 should have defined.
1406
1407
1408Examples
1409========
1410
1411Note that all examples in this section **purposefully** show how
1412to use the low-level event loop APIs, such as :meth:`loop.run_forever`
1413and :meth:`loop.call_soon`. Modern asyncio applications rarely
1414need to be written this way; consider using the high-level functions
1415like :func:`asyncio.run`.
1416
1417
1418.. _asyncio_example_lowlevel_helloworld:
Victor Stinnerea3183f2013-12-03 01:08:00 +01001419
Victor Stinner7f314ed2014-10-15 18:49:16 +02001420Hello World with call_soon()
Victor Stinnera092a612015-01-09 15:58:41 +01001421^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +01001422
Yury Selivanov512d7102018-09-17 19:35:30 -04001423An example using the :meth:`loop.call_soon` method to schedule a
1424callback. The callback displays ``"Hello World"`` and then stops the
1425event loop::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001426
1427 import asyncio
1428
Victor Stinner7f314ed2014-10-15 18:49:16 +02001429 def hello_world(loop):
Yury Selivanov512d7102018-09-17 19:35:30 -04001430 """A callback to print 'Hello World' and stop the event loop"""
Victor Stinnerea3183f2013-12-03 01:08:00 +01001431 print('Hello World')
Victor Stinner7f314ed2014-10-15 18:49:16 +02001432 loop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001433
1434 loop = asyncio.get_event_loop()
Victor Stinner7f314ed2014-10-15 18:49:16 +02001435
1436 # Schedule a call to hello_world()
1437 loop.call_soon(hello_world, loop)
1438
1439 # Blocking call interrupted by loop.stop()
Yury Selivanov512d7102018-09-17 19:35:30 -04001440 try:
1441 loop.run_forever()
1442 finally:
1443 loop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001444
Victor Stinner3e09e322013-12-03 01:22:06 +01001445.. seealso::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001446
Yury Selivanov512d7102018-09-17 19:35:30 -04001447 A similar :ref:`Hello World <coroutine>`
1448 example created with a coroutine and the :func:`run` function.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001449
Victor Stinner8b863482014-01-27 10:07:50 +01001450
Yury Selivanov512d7102018-09-17 19:35:30 -04001451.. _asyncio_example_call_later:
Victor Stinner7f314ed2014-10-15 18:49:16 +02001452
1453Display the current date with call_later()
Victor Stinnera092a612015-01-09 15:58:41 +01001454^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner7f314ed2014-10-15 18:49:16 +02001455
Yury Selivanov512d7102018-09-17 19:35:30 -04001456An example of a callback displaying the current date every second. The
1457callback uses the :meth:`loop.call_later` method to reschedule itself
1458after 5 seconds, and then stops the event loop::
Victor Stinner7f314ed2014-10-15 18:49:16 +02001459
1460 import asyncio
1461 import datetime
1462
1463 def display_date(end_time, loop):
1464 print(datetime.datetime.now())
1465 if (loop.time() + 1.0) < end_time:
1466 loop.call_later(1, display_date, end_time, loop)
1467 else:
1468 loop.stop()
1469
1470 loop = asyncio.get_event_loop()
1471
1472 # Schedule the first call to display_date()
1473 end_time = loop.time() + 5.0
1474 loop.call_soon(display_date, end_time, loop)
1475
1476 # Blocking call interrupted by loop.stop()
Yury Selivanov512d7102018-09-17 19:35:30 -04001477 try:
1478 loop.run_forever()
1479 finally:
1480 loop.close()
Victor Stinner7f314ed2014-10-15 18:49:16 +02001481
1482.. seealso::
1483
Yury Selivanov512d7102018-09-17 19:35:30 -04001484 A similar :ref:`current date <asyncio_example_sleep>` example
1485 created with a coroutine and the :func:`run` function.
Victor Stinner7f314ed2014-10-15 18:49:16 +02001486
1487
Yury Selivanov512d7102018-09-17 19:35:30 -04001488.. _asyncio_example_watch_fd:
Victor Stinner8b863482014-01-27 10:07:50 +01001489
Victor Stinner04e6df32014-10-11 16:16:27 +02001490Watch a file descriptor for read events
Victor Stinnera092a612015-01-09 15:58:41 +01001491^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001492
1493Wait until a file descriptor received some data using the
Yury Selivanov512d7102018-09-17 19:35:30 -04001494:meth:`loop.add_reader` method and then close the event loop::
Victor Stinner04e6df32014-10-11 16:16:27 +02001495
1496 import asyncio
Victor Stinnerac577d72017-11-28 21:33:20 +01001497 from socket import socketpair
Victor Stinner04e6df32014-10-11 16:16:27 +02001498
1499 # Create a pair of connected file descriptors
Victor Stinnerccd8e342014-10-11 16:30:02 +02001500 rsock, wsock = socketpair()
Yury Selivanov512d7102018-09-17 19:35:30 -04001501
Victor Stinner04e6df32014-10-11 16:16:27 +02001502 loop = asyncio.get_event_loop()
1503
1504 def reader():
1505 data = rsock.recv(100)
1506 print("Received:", data.decode())
Yury Selivanov512d7102018-09-17 19:35:30 -04001507
Victor Stinner2cef3002014-10-23 22:38:46 +02001508 # We are done: unregister the file descriptor
Victor Stinner04e6df32014-10-11 16:16:27 +02001509 loop.remove_reader(rsock)
Yury Selivanov512d7102018-09-17 19:35:30 -04001510
Victor Stinner04e6df32014-10-11 16:16:27 +02001511 # Stop the event loop
1512 loop.stop()
1513
Victor Stinner2cef3002014-10-23 22:38:46 +02001514 # Register the file descriptor for read event
Victor Stinner04e6df32014-10-11 16:16:27 +02001515 loop.add_reader(rsock, reader)
1516
1517 # Simulate the reception of data from the network
1518 loop.call_soon(wsock.send, 'abc'.encode())
1519
Yury Selivanov512d7102018-09-17 19:35:30 -04001520 try:
1521 # Run the event loop
1522 loop.run_forever()
1523 finally:
1524 # We are done. Close sockets and the event loop.
1525 rsock.close()
1526 wsock.close()
1527 loop.close()
Victor Stinner04e6df32014-10-11 16:16:27 +02001528
1529.. seealso::
1530
Yury Selivanov512d7102018-09-17 19:35:30 -04001531 * A similar :ref:`example <asyncio_example_create_connection>`
1532 using transports, protocols, and the
1533 :meth:`loop.create_connection` method.
Victor Stinner04e6df32014-10-11 16:16:27 +02001534
Yury Selivanov512d7102018-09-17 19:35:30 -04001535 * Another similar :ref:`example <asyncio_example_create_connection-streams>`
1536 using the high-level :func:`asyncio.open_connection` function
1537 and streams.
Victor Stinner04e6df32014-10-11 16:16:27 +02001538
1539
Yury Selivanov512d7102018-09-17 19:35:30 -04001540.. _asyncio_example_unix_signals:
1541
Victor Stinner04e6df32014-10-11 16:16:27 +02001542Set signal handlers for SIGINT and SIGTERM
Victor Stinnera092a612015-01-09 15:58:41 +01001543^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001544
Yury Selivanov512d7102018-09-17 19:35:30 -04001545(This ``signals`` example only works on Unix.)
1546
1547Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM`
1548using the :meth:`loop.add_signal_handler` method::
Victor Stinner8b863482014-01-27 10:07:50 +01001549
1550 import asyncio
1551 import functools
1552 import os
1553 import signal
1554
1555 def ask_exit(signame):
1556 print("got signal %s: exit" % signame)
1557 loop.stop()
1558
Yury Selivanov512d7102018-09-17 19:35:30 -04001559 async def main():
1560 loop = asyncio.get_running_loop()
Victor Stinner8b863482014-01-27 10:07:50 +01001561
Yury Selivanov512d7102018-09-17 19:35:30 -04001562 for signame in {'SIGINT', 'SIGTERM'}:
1563 loop.add_signal_handler(
1564 getattr(signal, signame),
1565 functools.partial(ask_exit, signame))
Victor Stinner2cef3002014-10-23 22:38:46 +02001566
Yury Selivanov512d7102018-09-17 19:35:30 -04001567 await asyncio.sleep(3600)
1568
1569 print("Event loop running for 1 hour, press Ctrl+C to interrupt.")
1570 print(f"pid {os.getpid()}: send SIGINT or SIGTERM to exit.")
1571
1572 asyncio.run(main())