blob: 909d3ea336329f092b8b177acebb70dc81f25805 [file] [log] [blame]
R David Murray6a143812013-12-20 14:37:39 -05001.. currentmodule:: asyncio
Victor Stinnerea3183f2013-12-03 01:08:00 +01002
Yury Selivanov7c7605f2018-09-11 09:54:40 -07003
4==========
5Event Loop
6==========
7
8
9.. rubric:: Preface
10
Carol Willing5b7cbd62018-09-12 17:05:17 -070011The event loop is a central component of every asyncio application.
Yury Selivanov7c7605f2018-09-11 09:54:40 -070012Event loops run asynchronous tasks and callbacks, perform network
Carol Willing5b7cbd62018-09-12 17:05:17 -070013IO operations, and run subprocesses.
Yury Selivanov7c7605f2018-09-11 09:54:40 -070014
Carol Willing5b7cbd62018-09-12 17:05:17 -070015Application developers will typically use high-level asyncio functions
16to interact with the event loop. In general, high-level asyncio applications
17should not need to work directly with event loops and, instead, should use
18the :func:`asyncio.run` function to initialize, manage the event loop, and
19run asynchronous code.
Yury Selivanov7c7605f2018-09-11 09:54:40 -070020
Carol Willing5b7cbd62018-09-12 17:05:17 -070021Alternatively, developers of low-level code, such as libraries and
22framework, may need access to the event loop.
Yury Selivanov7c7605f2018-09-11 09:54:40 -070023
24.. rubric:: Accessing Event Loop
25
26The following low-level functions can be used to get, set, or create
27an event loop:
28
29.. function:: get_running_loop()
30
31 Return the running event loop in the current OS thread.
32
33 If there is no running event loop a :exc:`RuntimeError` is raised.
34 This function can only be called from a coroutine or a callback.
35
36 .. versionadded:: 3.7
37
38.. function:: get_event_loop()
39
40 Get the current event loop. If there is no current event loop set
41 in the current OS thread and :func:`set_event_loop` has not yet
42 been called, asyncio will create a new event loop and set it as the
43 current one.
44
Carol Willing5b7cbd62018-09-12 17:05:17 -070045 Because this function has rather complex behavior (especially
46 when custom event loop policies are in use), using the
47 :func:`get_running_loop` function is preferred to :func:`get_event_loop`
48 in coroutines and callbacks.
Yury Selivanov7c7605f2018-09-11 09:54:40 -070049
Carol Willing5b7cbd62018-09-12 17:05:17 -070050 Consider also using the :func:`asyncio.run` function instead of using
51 lower level functions to manually create and close an event loop.
Yury Selivanov7c7605f2018-09-11 09:54:40 -070052
53.. function:: set_event_loop(loop)
54
55 Set *loop* as a current event loop for the current OS thread.
56
57.. function:: new_event_loop()
58
59 Create a new event loop object.
60
61Note that the behaviour of :func:`get_event_loop`, :func:`set_event_loop`,
62and :func:`new_event_loop` functions can be altered by
63:ref:`setting a custom event loop policy <asyncio-policies>`.
64
65
66.. rubric:: Contents
67
68This documentation page contains the following sections:
69
Carol Willing5b7cbd62018-09-12 17:05:17 -070070* The `Event Loop Methods`_ section is the reference documentation of
Yury Selivanov7c7605f2018-09-11 09:54:40 -070071 event loop APIs;
72
73* The `Callback Handles`_ section documents the :class:`Handle` and
Carol Willing5b7cbd62018-09-12 17:05:17 -070074 :class:`TimerHandle`, instances which are returned from functions, such
75 as :meth:`loop.call_soon` and :meth:`loop.call_later`;
Yury Selivanov7c7605f2018-09-11 09:54:40 -070076
Carol Willing5b7cbd62018-09-12 17:05:17 -070077* The `Server Objects`_ sections document types returned from
Yury Selivanov7c7605f2018-09-11 09:54:40 -070078 event loop methods like :meth:`loop.create_server`;
79
80* The `Event Loops Implementations`_ section documents the
81 :class:`SelectorEventLoop` and :class:`ProactorEventLoop` classes;
82
83* The `Examples`_ section showcases how to work with some event
84 loop APIs.
85
86
Victor Stinner9592edb2014-02-02 15:03:02 +010087.. _asyncio-event-loop:
Victor Stinnerea3183f2013-12-03 01:08:00 +010088
Yury Selivanov7c7605f2018-09-11 09:54:40 -070089Event Loop Methods
90==================
Victor Stinnerea3183f2013-12-03 01:08:00 +010091
Carol Willing5b7cbd62018-09-12 17:05:17 -070092Event loops have **low-level** APIs for the following:
lf627d2c82017-07-25 17:03:51 -060093
Yury Selivanov7c7605f2018-09-11 09:54:40 -070094.. contents::
95 :depth: 1
96 :local:
Victor Stinnerea3183f2013-12-03 01:08:00 +010097
Victor Stinnerea3183f2013-12-03 01:08:00 +010098
Yury Selivanov7c7605f2018-09-11 09:54:40 -070099Running and stopping the loop
100^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100101
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700102.. method:: loop.run_until_complete(future)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100103
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700104 Run until the *future* (an instance of :class:`Future`) is
105 completed.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100106
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700107 If the argument is a :ref:`coroutine object <coroutine>` it
108 is implicitly wrapped into an :class:`asyncio.Task`.
Eli Bendersky136fea22014-02-09 06:55:58 -0800109
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700110 Return the Future's result or raise its exception.
Guido van Rossumf68afd82016-08-08 09:41:21 -0700111
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700112.. method:: loop.run_forever()
Guido van Rossumf68afd82016-08-08 09:41:21 -0700113
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700114 Run the event loop until :meth:`stop` is called.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100115
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700116 If :meth:`stop` is called before :meth:`run_forever()` is called,
117 the loop will poll the I/O selector once with a timeout of zero,
118 run all callbacks scheduled in response to I/O events (and
119 those that were already scheduled), and then exit.
Victor Stinner83704962015-02-25 14:24:15 +0100120
Guido van Rossum41f69f42015-11-19 13:28:47 -0800121 If :meth:`stop` is called while :meth:`run_forever` is running,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700122 the loop will run the current batch of callbacks and then exit.
Carol Willing5b7cbd62018-09-12 17:05:17 -0700123 Note that callbacks scheduled by callbacks will not run in this
124 case; instead, they will run the next time :meth:`run_forever` or
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700125 :meth:`run_until_complete` is called.
Guido van Rossum41f69f42015-11-19 13:28:47 -0800126
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700127.. method:: loop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100128
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700129 Stop the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100130
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700131.. method:: loop.is_running()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100132
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700133 Return ``True`` if the event loop is currently running.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100134
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700135.. method:: loop.is_closed()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100136
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700137 Return ``True`` if the event loop was closed.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100138
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700139.. method:: loop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100140
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700141 Close the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100142
Carol Willing5b7cbd62018-09-12 17:05:17 -0700143 The loop must be running when this function is called.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700144 Any pending callbacks will be discarded.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100145
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700146 This method clears all queues and shuts down the executor, but does
147 not wait for the executor to finish.
Guido van Rossum41f69f42015-11-19 13:28:47 -0800148
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700149 This method is idempotent and irreversible. No other methods
150 should be called after the event loop is closed.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100151
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700152.. coroutinemethod:: loop.shutdown_asyncgens()
Yury Selivanov03660042016-12-15 17:36:05 -0500153
154 Schedule all currently open :term:`asynchronous generator` objects to
155 close with an :meth:`~agen.aclose()` call. After calling this method,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700156 the event loop will issue a warning if a new asynchronous generator
Carol Willing5b7cbd62018-09-12 17:05:17 -0700157 is iterated. This should be used to reliably finalize all scheduled
158 asynchronous generators, e.g.::
159
Yury Selivanov03660042016-12-15 17:36:05 -0500160
161 try:
162 loop.run_forever()
163 finally:
164 loop.run_until_complete(loop.shutdown_asyncgens())
165 loop.close()
166
167 .. versionadded:: 3.6
168
169
Victor Stinner8464c242014-11-28 13:15:41 +0100170.. _asyncio-pass-keywords:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100171
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700172Scheduling callbacks
173^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100174
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700175.. method:: loop.call_soon(callback, *args, context=None)
Victor Stinner8464c242014-11-28 13:15:41 +0100176
Carol Willing5b7cbd62018-09-12 17:05:17 -0700177 Schedule a *callback* to be called with *args* arguments at
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700178 the next iteration of the event loop.
Victor Stinner8464c242014-11-28 13:15:41 +0100179
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700180 Callbacks are called in the order in which they are registered.
181 Each callback will be called exactly once.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100182
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700183 An optional keyword-only *context* argument allows specifying a
184 custom :class:`contextvars.Context` for the *callback* to run in.
185 The current context is used when no *context* is provided.
Yury Selivanov28b91782018-05-23 13:35:04 -0400186
Yury Selivanov1096f762015-06-25 13:49:52 -0400187 An instance of :class:`asyncio.Handle` is returned, which can be
Carol Willing5b7cbd62018-09-12 17:05:17 -0700188 used later to cancel the callback.
189
190 This method is not thread-safe.
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500191
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700192.. method:: loop.call_soon_threadsafe(callback, *args, context=None)
Victor Stinner8464c242014-11-28 13:15:41 +0100193
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700194 A thread-safe variant of :meth:`call_soon`. Must be used to
195 schedule callbacks *from another thread*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100196
Victor Stinner83704962015-02-25 14:24:15 +0100197 See the :ref:`concurrency and multithreading <asyncio-multithreading>`
198 section of the documentation.
199
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700200.. versionchanged:: 3.7
201 The *context* keyword-only parameter was added. See :pep:`567`
202 for more details.
203
204.. note::
205
Carol Willing5b7cbd62018-09-12 17:05:17 -0700206 Most :mod:`asyncio` scheduling functions don't allow passing
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700207 keyword arguments. To do that, use :func:`functools.partial`,
208 e.g.::
209
Carol Willing5b7cbd62018-09-12 17:05:17 -0700210 # will schedule "print("Hello", flush=True)"
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700211 loop.call_soon(
212 functools.partial(print, "Hello", flush=True))
213
214 Using partial objects is usually more convenient than using lambdas,
215 as asyncio can better render partial objects in debug and error
216 messages.
Yury Selivanov28b91782018-05-23 13:35:04 -0400217
Victor Stinnerea3183f2013-12-03 01:08:00 +0100218
Victor Stinner45b27ed2014-02-01 02:36:43 +0100219.. _asyncio-delayed-calls:
220
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700221Scheduling delayed callbacks
222^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100223
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700224Event loop provides mechanisms to schedule callback functions
225to be called at some point in the future. Event loop uses monotonic
226clocks to track time.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100227
Victor Stinner45b27ed2014-02-01 02:36:43 +0100228
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700229.. method:: loop.call_later(delay, callback, *args, context=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100230
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700231 Schedule *callback* to be called after the given *delay*
232 number of seconds (can be either an int or a float).
Victor Stinnerea3183f2013-12-03 01:08:00 +0100233
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700234 An instance of :class:`asyncio.TimerHandle` is returned which can
235 be used to cancel the callback.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100236
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700237 *callback* will be called exactly once. If two callbacks are
Carol Willing5b7cbd62018-09-12 17:05:17 -0700238 scheduled for exactly the same time, it is undefined which one will
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700239 be called first.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100240
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700241 The optional positional *args* will be passed to the callback when
242 it is called. If you want the callback to be called with keyword
243 arguments use :func:`functools.partial`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100244
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700245 An optional keyword-only *context* argument allows specifying a
246 custom :class:`contextvars.Context` for the *callback* to run in.
247 The current context is used when no *context* is provided.
Victor Stinner8464c242014-11-28 13:15:41 +0100248
Yury Selivanov28b91782018-05-23 13:35:04 -0400249 .. versionchanged:: 3.7
250 The *context* keyword-only parameter was added. See :pep:`567`
251 for more details.
252
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700253.. method:: loop.call_at(when, callback, *args, context=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100254
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700255 Schedule *callback* to be called at the given absolute timestamp
256 *when* (an int or a float), using the same time reference as
257 :meth:`loop.time`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100258
259 This method's behavior is the same as :meth:`call_later`.
260
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700261 An instance of :class:`asyncio.TimerHandle` is returned which can
262 be used to cancel the callback.
Victor Stinner8464c242014-11-28 13:15:41 +0100263
Yury Selivanov28b91782018-05-23 13:35:04 -0400264 .. versionchanged:: 3.7
265 The *context* keyword-only parameter was added. See :pep:`567`
266 for more details.
267
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700268.. method:: loop.time()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100269
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700270 Return the current time, as a :class:`float` value, according to
271 the event loop's internal monotonic clock.
272
273.. note::
274
275 Timeouts (relative *delay* or absolute *when*) should not
276 exceed one day.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100277
Victor Stinner3e09e322013-12-03 01:22:06 +0100278.. seealso::
279
280 The :func:`asyncio.sleep` function.
281
Victor Stinnerea3183f2013-12-03 01:08:00 +0100282
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700283Creating Futures and Tasks
284^^^^^^^^^^^^^^^^^^^^^^^^^^
Yury Selivanov950204d2016-05-16 16:23:00 -0400285
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700286.. method:: loop.create_future()
Yury Selivanov950204d2016-05-16 16:23:00 -0400287
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700288 Create an :class:`asyncio.Future` object attached to the event loop.
Yury Selivanov950204d2016-05-16 16:23:00 -0400289
Carol Willing5b7cbd62018-09-12 17:05:17 -0700290 This is the preferred way to create Futures in asyncio. This lets
291 third-party event loops provide alternative implementations of
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700292 the Future object (with better performance or instrumentation).
Yury Selivanov950204d2016-05-16 16:23:00 -0400293
294 .. versionadded:: 3.5.2
295
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700296.. method:: loop.create_task(coro, \*, name=None)
Yury Selivanov950204d2016-05-16 16:23:00 -0400297
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700298 Schedule the execution of a :ref:`coroutine`.
299 Return a :class:`Task` object.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200300
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700301 Third-party event loops can use their own subclass of :class:`Task`
302 for interoperability. In this case, the result type is a subclass
303 of :class:`Task`.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200304
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700305 If the *name* argument is provided and not ``None``, it is set as
306 the name of the task using :meth:`Task.set_name`.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200307
Alex Grönholmcca4eec2018-08-09 00:06:47 +0300308 .. versionchanged:: 3.8
309 Added the ``name`` parameter.
310
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700311.. 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 Selivanov7c7605f2018-09-11 09:54:40 -0700314 :meth:`loop.create_task`.
Yury Selivanov71854612015-05-11 16:28:27 -0400315
316 If *factory* is ``None`` the default task factory will be set.
317
318 If *factory* is a *callable*, it should have a signature matching
319 ``(loop, coro)``, where *loop* will be a reference to the active
320 event loop, *coro* will be a coroutine object. The callable
321 must return an :class:`asyncio.Future` compatible object.
322
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700323.. method:: loop.get_task_factory()
Yury Selivanov71854612015-05-11 16:28:27 -0400324
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700325 Return a task factory or ``None`` if the default one is in use.
Yury Selivanov71854612015-05-11 16:28:27 -0400326
Victor Stinner530ef2f2014-07-08 12:39:10 +0200327
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700328Opening network connections
329^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100330
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700331.. coroutinemethod:: loop.create_connection(protocol_factory, \
332 host=None, port=None, \*, ssl=None, \
333 family=0, proto=0, flags=0, sock=None, \
334 local_addr=None, server_hostname=None, \
335 ssl_handshake_timeout=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100336
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700337 Open a streaming transport connection to a given
338 address specified by *host* and *port*.
339
340 The socket family can be either :py:data:`~socket.AF_INET` or
341 :py:data:`~socket.AF_INET6` depending on *host* (or the *family*
342 argument, if provided).
343
344 The socket type will be :py:data:`~socket.SOCK_STREAM`.
345
346 *protocol_factory* must be a callable returning an
347 :ref:`asyncio protocol <asyncio-protocol>` implementation.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100348
Yury Selivanov19a44f62017-12-14 20:53:26 -0500349 This method will try to establish the connection in the background.
350 When successful, it returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100351
352 The chronological synopsis of the underlying operation is as follows:
353
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700354 #. The connection is established and a :ref:`transport <asyncio-transport>`
355 is created for it.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100356
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700357 #. *protocol_factory* is called without arguments and is expected to
358 return a :ref:`protocol <asyncio-protocol>` instance.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100359
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700360 #. The protocol instance is coupled with the transport by calling its
361 :meth:`~BaseProtocol.connection_made` method.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100362
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700363 #. A ``(transport, protocol)`` tuple is returned on success.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100364
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700365 The created transport is an implementation-dependent bidirectional
366 stream.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100367
368 .. note::
369 *protocol_factory* can be any kind of callable, not necessarily
370 a class. For example, if you want to use a pre-created
371 protocol instance, you can pass ``lambda: my_protocol``.
372
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700373 Other arguments:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100374
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700375 * *ssl*: if given and not false, an SSL/TLS transport is created
Victor Stinnerea3183f2013-12-03 01:08:00 +0100376 (by default a plain TCP transport is created). If *ssl* is
377 a :class:`ssl.SSLContext` object, this context is used to create
378 the transport; if *ssl* is :const:`True`, a context with some
379 unspecified default settings is used.
380
Berker Peksag9c1dba22014-09-28 00:00:58 +0300381 .. seealso:: :ref:`SSL/TLS security considerations <ssl-security>`
Antoine Pitrouc5e075f2014-03-22 18:19:11 +0100382
Victor Stinnerea3183f2013-12-03 01:08:00 +0100383 * *server_hostname*, is only for use together with *ssl*,
384 and sets or overrides the hostname that the target server's certificate
385 will be matched against. By default the value of the *host* argument
386 is used. If *host* is empty, there is no default and you must pass a
387 value for *server_hostname*. If *server_hostname* is an empty
388 string, hostname matching is disabled (which is a serious security
389 risk, allowing for man-in-the-middle-attacks).
390
391 * *family*, *proto*, *flags* are the optional address family, protocol
392 and flags to be passed through to getaddrinfo() for *host* resolution.
393 If given, these should all be integers from the corresponding
394 :mod:`socket` module constants.
395
396 * *sock*, if given, should be an existing, already connected
397 :class:`socket.socket` object to be used by the transport.
398 If *sock* is given, none of *host*, *port*, *family*, *proto*, *flags*
399 and *local_addr* should be specified.
400
401 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
402 to bind the socket to locally. The *local_host* and *local_port*
Carol Willing5b7cbd62018-09-12 17:05:17 -0700403 are looked up using ``getaddrinfo()``, similarly to *host* and *port*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100404
Neil Aspinallf7686c12017-12-19 19:45:42 +0000405 * *ssl_handshake_timeout* is (for an SSL connection) the time in seconds
406 to wait for the SSL handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400407 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000408
409 .. versionadded:: 3.7
410
411 The *ssl_handshake_timeout* parameter.
412
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700413 .. versionchanged:: 3.6
414
415 The socket option :py:data:`~socket.TCP_NODELAY` is set by default
416 for all TCP connections.
417
Victor Stinner60208a12015-09-15 22:41:52 +0200418 .. versionchanged:: 3.5
419
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700420 Added support for SSL/TLS for :class:`ProactorEventLoop`.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200421
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100422 .. seealso::
423
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700424 The :func:`open_connection` function is a high-level alternative
425 API. It returns a pair of (:class:`StreamReader`, :class:`StreamWriter`)
426 that can be used directly in async/await code.
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100427
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700428.. coroutinemethod:: loop.create_datagram_endpoint(protocol_factory, \
429 local_addr=None, remote_addr=None, \*, \
430 family=0, proto=0, flags=0, \
431 reuse_address=None, reuse_port=None, \
432 allow_broadcast=None, sock=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100433
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700434 Create a datagram connection.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100435
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700436 The socket family can be either :py:data:`~socket.AF_INET`,
437 :py:data:`~socket.AF_INET6`, or :py:data:`~socket.AF_UNIX`,
438 depending on *host* (or the *family* argument, if provided).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100439
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700440 The socket type will be :py:data:`~socket.SOCK_DGRAM`.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100441
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700442 *protocol_factory* must be a callable returning a
443 :ref:`protocol <asyncio-protocol>` implementation.
444
445 A tuple of ``(transport, protocol)`` is returned on success.
446
447 Other arguments:
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700448
449 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
450 to bind the socket to locally. The *local_host* and *local_port*
451 are looked up using :meth:`getaddrinfo`.
452
453 * *remote_addr*, if given, is a ``(remote_host, remote_port)`` tuple used
454 to connect the socket to a remote address. The *remote_host* and
455 *remote_port* are looked up using :meth:`getaddrinfo`.
456
457 * *family*, *proto*, *flags* are the optional address family, protocol
458 and flags to be passed through to :meth:`getaddrinfo` for *host*
459 resolution. If given, these should all be integers from the
460 corresponding :mod:`socket` module constants.
461
462 * *reuse_address* tells the kernel to reuse a local socket in
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700463 ``TIME_WAIT`` state, without waiting for its natural timeout to
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300464 expire. If not specified will automatically be set to ``True`` on
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700465 UNIX.
466
467 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
468 same port as other existing endpoints are bound to, so long as they all
469 set this flag when being created. This option is not supported on Windows
470 and some UNIX's. If the :py:data:`~socket.SO_REUSEPORT` constant is not
471 defined then this capability is unsupported.
472
473 * *allow_broadcast* tells the kernel to allow this endpoint to send
474 messages to the broadcast address.
475
476 * *sock* can optionally be specified in order to use a preexisting,
477 already connected, :class:`socket.socket` object to be used by the
478 transport. If specified, *local_addr* and *remote_addr* should be omitted
479 (must be :const:`None`).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100480
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200481 On Windows with :class:`ProactorEventLoop`, this method is not supported.
482
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200483 See :ref:`UDP echo client protocol <asyncio-udp-echo-client-protocol>` and
484 :ref:`UDP echo server protocol <asyncio-udp-echo-server-protocol>` examples.
485
Romuald Brunet0ded5802018-05-14 18:22:00 +0200486 .. versionchanged:: 3.4.4
487 The *family*, *proto*, *flags*, *reuse_address*, *reuse_port,
488 *allow_broadcast*, and *sock* parameters were added.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100489
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700490.. coroutinemethod:: loop.create_unix_connection(protocol_factory, \
491 path=None, \*, ssl=None, sock=None, \
492 server_hostname=None, ssl_handshake_timeout=None)
Victor Stinnera6919aa2014-02-19 13:32:34 +0100493
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700494 Create UNIX connection.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100495
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700496 The socket family will be :py:data:`~socket.AF_UNIX`; socket
497 type will be :py:data:`~socket.SOCK_STREAM`.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100498
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700499 A tuple of ``(transport, protocol)`` is returned on success.
Guido van Rossum9e80eeb2016-11-03 14:17:25 -0700500
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700501 *path* is the name of a UNIX domain socket and is required,
502 unless a *sock* parameter is specified. Abstract UNIX sockets,
503 :class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths are
504 supported.
505
506 See the documentation of the :meth:`loop.create_connection` method
507 for information about arguments to this method.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100508
509 Availability: UNIX.
510
Neil Aspinallf7686c12017-12-19 19:45:42 +0000511 .. versionadded:: 3.7
512
513 The *ssl_handshake_timeout* parameter.
514
Yury Selivanov423fd362017-11-20 17:26:28 -0500515 .. versionchanged:: 3.7
516
Elvis Pranskevichusc0d062f2018-06-08 11:36:00 -0400517 The *path* parameter can now be a :term:`path-like object`.
Yury Selivanov423fd362017-11-20 17:26:28 -0500518
Victor Stinnera6919aa2014-02-19 13:32:34 +0100519
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700520Creating network servers
521^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100522
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700523.. coroutinemethod:: loop.create_server(protocol_factory, \
524 host=None, port=None, \*, \
525 family=socket.AF_UNSPEC, \
526 flags=socket.AI_PASSIVE, \
527 sock=None, backlog=100, ssl=None, \
528 reuse_address=None, reuse_port=None, \
529 ssl_handshake_timeout=None, start_serving=True)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100530
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700531 Create a TCP server (socket type :data:`~socket.SOCK_STREAM`) listening
532 on the *host* and *port* address.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200533
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700534 Returns a :class:`Server` object.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200535
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700536 Arguments:
Victor Stinner33f6abe2014-10-12 20:36:04 +0200537
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400538 * *protocol_factory* must be a callable returning a
539 :ref:`protocol <asyncio-protocol>` implementation.
540
Carol Willing5b7cbd62018-09-12 17:05:17 -0700541 * The *host* parameter can be set to several types which determine behavior:
542 - If *host* is a string, the TCP server is bound to *host* and *port*.
543 - if *host* is a sequence of strings, the TCP server is bound to all
544 hosts of the sequence.
545 - If *host* is an empty string or ``None``, all interfaces are
546 assumed and a list of multiple sockets will be returned (most likely
547 one for IPv4 and another one for IPv6).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200548
549 * *family* can be set to either :data:`socket.AF_INET` or
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700550 :data:`~socket.AF_INET6` to force the socket to use IPv4 or IPv6.
Carol Willing5b7cbd62018-09-12 17:05:17 -0700551 If not set, the *family* will be determined from host name
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700552 (defaults to :data:`~socket.AF_UNSPEC`).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200553
554 * *flags* is a bitmask for :meth:`getaddrinfo`.
555
556 * *sock* can optionally be specified in order to use a preexisting
557 socket object. If specified, *host* and *port* should be omitted (must be
558 :const:`None`).
559
560 * *backlog* is the maximum number of queued connections passed to
561 :meth:`~socket.socket.listen` (defaults to 100).
562
563 * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over the
564 accepted connections.
565
566 * *reuse_address* tells the kernel to reuse a local socket in
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700567 ``TIME_WAIT`` state, without waiting for its natural timeout to
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300568 expire. If not specified will automatically be set to ``True`` on
Victor Stinner33f6abe2014-10-12 20:36:04 +0200569 UNIX.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100570
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700571 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
572 same port as other existing endpoints are bound to, so long as they all
573 set this flag when being created. This option is not supported on
574 Windows.
575
Neil Aspinallf7686c12017-12-19 19:45:42 +0000576 * *ssl_handshake_timeout* is (for an SSL server) the time in seconds to wait
577 for the SSL handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400578 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000579
Yury Selivanovc9070d02018-01-25 18:08:09 -0500580 * *start_serving* set to ``True`` (the default) causes the created server
581 to start accepting connections immediately. When set to ``False``,
582 the user should await on :meth:`Server.start_serving` or
583 :meth:`Server.serve_forever` to make the server to start accepting
584 connections.
585
Neil Aspinallf7686c12017-12-19 19:45:42 +0000586 .. versionadded:: 3.7
587
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700588 Added *ssl_handshake_timeout* and *start_serving* parameters.
589
590 .. versionchanged:: 3.6
591
592 The socket option :py:data:`~socket.TCP_NODELAY` is set by default
593 for all TCP connections.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000594
Victor Stinner60208a12015-09-15 22:41:52 +0200595 .. versionchanged:: 3.5
596
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700597 Added support for SSL/TLS on Windows with
598 :class:`ProactorEventLoop`.
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100599
Victor Stinner7b58a2b2015-09-21 18:41:05 +0200600 .. versionchanged:: 3.5.1
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200601
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700602 The *host* parameter can be a sequence of strings.
603
604 .. seealso::
605
606 The :func:`start_server` function is a higher-level alternative API
607 that returns a pair of :class:`StreamReader` and :class:`StreamWriter`
608 that can be used in an async/await code.
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200609
Victor Stinnerea3183f2013-12-03 01:08:00 +0100610
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700611.. coroutinemethod:: loop.create_unix_server(protocol_factory, path=None, \
612 \*, sock=None, backlog=100, ssl=None, \
613 ssl_handshake_timeout=None, start_serving=True)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100614
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700615 Similar to :meth:`loop.create_server` but works with the
616 :py:data:`~socket.AF_UNIX` socket family.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100617
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700618 *path* is the name of a UNIX domain socket, and is required,
619 unless a *sock* argument is provided. Abstract UNIX sockets,
620 :class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths
621 are supported.
Yury Selivanov423fd362017-11-20 17:26:28 -0500622
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400623 See the documentation of the :meth:`loop.create_server` method
624 for information about arguments to this method.
625
Victor Stinnera6919aa2014-02-19 13:32:34 +0100626 Availability: UNIX.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100627
Neil Aspinallf7686c12017-12-19 19:45:42 +0000628 .. versionadded:: 3.7
629
Elvis Pranskevichusc0d062f2018-06-08 11:36:00 -0400630 The *ssl_handshake_timeout* and *start_serving* parameters.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000631
Yury Selivanov423fd362017-11-20 17:26:28 -0500632 .. versionchanged:: 3.7
633
634 The *path* parameter can now be a :class:`~pathlib.Path` object.
635
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700636.. coroutinemethod:: loop.connect_accepted_socket(protocol_factory, \
637 sock, \*, ssl=None, ssl_handshake_timeout=None)
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500638
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700639 Wrap an already accepted connection into a transport/protocol pair.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500640
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700641 This method can be used by servers that accept connections outside
642 of asyncio but that use asyncio to handle them.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500643
644 Parameters:
645
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400646 * *protocol_factory* must be a callable returning a
647 :ref:`protocol <asyncio-protocol>` implementation.
648
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700649 * *sock* is a preexisting socket object returned from
650 :meth:`socket.accept <socket.socket.accept>`.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500651
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700652 * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over
653 the accepted connections.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500654
Neil Aspinallf7686c12017-12-19 19:45:42 +0000655 * *ssl_handshake_timeout* is (for an SSL connection) the time in seconds to
656 wait for the SSL handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400657 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000658
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700659 Returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100660
Neil Aspinallf7686c12017-12-19 19:45:42 +0000661 .. versionadded:: 3.7
662
663 The *ssl_handshake_timeout* parameter.
664
AraHaan431665b2017-11-21 11:06:26 -0500665 .. versionadded:: 3.5.3
666
667
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700668Transferring files
669^^^^^^^^^^^^^^^^^^
Andrew Svetlov7c684072018-01-27 21:22:47 +0200670
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700671.. coroutinemethod:: loop.sendfile(transport, file, \
672 offset=0, count=None, *, fallback=True)
Andrew Svetlov7c684072018-01-27 21:22:47 +0200673
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700674 Send a *file* over a *transport*. Return the total number of bytes
675 sent.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200676
677 The method uses high-performance :meth:`os.sendfile` if available.
678
679 *file* must be a regular file object opened in binary mode.
680
681 *offset* tells from where to start reading the file. If specified,
682 *count* is the total number of bytes to transmit as opposed to
683 sending the file until EOF is reached. File position is updated on
684 return or also in case of error in which case :meth:`file.tell()
685 <io.IOBase.tell>` can be used to figure out the number of bytes
686 which were sent.
687
688 *fallback* set to ``True`` makes asyncio to manually read and send
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700689 the file when the platform does not support the sendfile system call
Andrew Svetlov7c684072018-01-27 21:22:47 +0200690 (e.g. Windows or SSL socket on Unix).
691
692 Raise :exc:`SendfileNotAvailableError` if the system does not support
693 *sendfile* syscall and *fallback* is ``False``.
694
695 .. versionadded:: 3.7
696
697
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500698TLS Upgrade
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700699^^^^^^^^^^^
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500700
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700701.. coroutinemethod:: loop.start_tls(transport, protocol, \
702 sslcontext, \*, server_side=False, \
703 server_hostname=None, ssl_handshake_timeout=None)
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500704
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700705 Upgrade an existing transport-based connection to TLS.
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500706
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700707 Return a new transport instance, that the *protocol* must start using
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500708 immediately after the *await*. The *transport* instance passed to
709 the *start_tls* method should never be used again.
710
711 Parameters:
712
713 * *transport* and *protocol* instances that methods like
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700714 :meth:`~loop.create_server` and
715 :meth:`~loop.create_connection` return.
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500716
717 * *sslcontext*: a configured instance of :class:`~ssl.SSLContext`.
718
719 * *server_side* pass ``True`` when a server-side connection is being
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700720 upgraded (like the one created by :meth:`~loop.create_server`).
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500721
722 * *server_hostname*: sets or overrides the host name that the target
723 server's certificate will be matched against.
724
725 * *ssl_handshake_timeout* is (for an SSL connection) the time in seconds to
726 wait for the SSL handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400727 ``60.0`` seconds if ``None`` (default).
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500728
729 .. versionadded:: 3.7
730
731
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700732Watching file descriptors
733^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerc1567df2014-02-08 23:22:58 +0100734
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700735.. method:: loop.add_reader(fd, callback, \*args)
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200736
Carol Willing5b7cbd62018-09-12 17:05:17 -0700737 Start watching the file descriptor *fd* for read availability and
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700738 call the *callback* with specified arguments.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200739
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700740.. method:: loop.remove_reader(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100741
Carol Willing5b7cbd62018-09-12 17:05:17 -0700742 Stop watching the file descriptor *fd* for read availability.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100743
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700744.. method:: loop.add_writer(fd, callback, \*args)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100745
Carol Willing5b7cbd62018-09-12 17:05:17 -0700746 Start watching the file descriptor *fd* for write availability and then
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700747 call the *callback* with specified arguments.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100748
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700749 Use :func:`functools.partial` :ref:`to pass keywords
750 <asyncio-pass-keywords>` to *func*.
Victor Stinner8464c242014-11-28 13:15:41 +0100751
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700752.. method:: loop.remove_writer(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100753
Carol Willing5b7cbd62018-09-12 17:05:17 -0700754 Stop watching the file descriptor *fd* for write availability.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100755
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700756See also :ref:`Platform Support <asyncio-platform-support>` section
757for some limitations of these methods.
Victor Stinner04e6df32014-10-11 16:16:27 +0200758
Victor Stinnerc1567df2014-02-08 23:22:58 +0100759
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700760Working with socket objects directly
761^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerc1567df2014-02-08 23:22:58 +0100762
Carol Willing5b7cbd62018-09-12 17:05:17 -0700763In general, protocol implementations that use transport-based APIs
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700764such as :meth:`loop.create_connection` and :meth:`loop.create_server`
765are faster than implementations that work with sockets directly.
Carol Willing5b7cbd62018-09-12 17:05:17 -0700766However, there are some use cases when performance is not critical, and
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700767working with :class:`~socket.socket` objects directly is more
768convenient.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100769
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700770.. coroutinemethod:: loop.sock_recv(sock, nbytes)
Yury Selivanov55c50842016-06-08 12:48:15 -0400771
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700772 Receive data. Asynchronous version of
773 :meth:`socket.recv() <socket.socket.recv>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100774
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700775 The received data is returned as a bytes object. The maximum amount
776 of data to be received is specified by the *nbytes* argument.
777
778 The socket *sock* must be non-blocking.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200779
Yury Selivanov19a44f62017-12-14 20:53:26 -0500780 .. versionchanged:: 3.7
Carol Willing5b7cbd62018-09-12 17:05:17 -0700781 Even though this method was always documented as a coroutine
782 method, releases before Python 3.7 returned a :class:`Future`.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700783 Since Python 3.7 this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100784
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700785.. coroutinemethod:: loop.sock_recv_into(sock, buf)
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200786
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700787 Receive data into a buffer. Modeled after the blocking
788 :meth:`socket.recv_into() <socket.socket.recv_into>` method.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200789
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700790 Return the number of bytes written to the buffer.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200791
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700792 The socket *sock* must be non-blocking.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200793
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200794 .. versionadded:: 3.7
795
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700796.. coroutinemethod:: loop.sock_sendall(sock, data)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100797
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700798 Send data to the socket. Asynchronous version of
799 :meth:`socket.sendall() <socket.socket.sendall>`.
Yury Selivanov55c50842016-06-08 12:48:15 -0400800
Carol Willing5b7cbd62018-09-12 17:05:17 -0700801 This method continues to send data from *data* to the socket until either
802 all data in *data* has been sent or an error occurs. ``None`` is returned
803 on success. On error, an exception is raised. Additionally, there is no way
804 to determine how much data, if any, was successfully processed by the
805 receiving end of the connection.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100806
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700807 The socket *sock* must be non-blocking.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200808
Yury Selivanov19a44f62017-12-14 20:53:26 -0500809 .. versionchanged:: 3.7
810 Even though the method was always documented as a coroutine
811 method, before Python 3.7 it returned an :class:`Future`.
812 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100813
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700814.. coroutinemethod:: loop.sock_connect(sock, address)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100815
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700816 Connect to a remote socket at *address*.
Victor Stinner1b0580b2014-02-13 09:24:37 +0100817
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700818 Asynchronous version of :meth:`socket.connect() <socket.socket.connect>`.
819
820 The socket *sock* must be non-blocking.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200821
Yury Selivanov55c50842016-06-08 12:48:15 -0400822 .. versionchanged:: 3.5.2
823 ``address`` no longer needs to be resolved. ``sock_connect``
824 will try to check if the *address* is already resolved by calling
825 :func:`socket.inet_pton`. If not,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700826 :meth:`loop.getaddrinfo` will be used to resolve the
Yury Selivanov55c50842016-06-08 12:48:15 -0400827 *address*.
828
Victor Stinnerc1567df2014-02-08 23:22:58 +0100829 .. seealso::
830
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700831 :meth:`loop.create_connection`
Yury Selivanov55c50842016-06-08 12:48:15 -0400832 and :func:`asyncio.open_connection() <open_connection>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100833
834
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700835.. coroutinemethod:: loop.sock_accept(sock)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100836
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700837 Accept a connection. Modeled after the blocking
838 :meth:`socket.accept() <socket.socket.accept>` method.
Yury Selivanov55c50842016-06-08 12:48:15 -0400839
840 The socket must be bound to an address and listening
Victor Stinnerc1567df2014-02-08 23:22:58 +0100841 for connections. The return value is a pair ``(conn, address)`` where *conn*
842 is a *new* socket object usable to send and receive data on the connection,
843 and *address* is the address bound to the socket on the other end of the
844 connection.
845
Victor Stinnerec2ce092014-07-29 23:12:22 +0200846 The socket *sock* must be non-blocking.
847
Yury Selivanov19a44f62017-12-14 20:53:26 -0500848 .. versionchanged:: 3.7
849 Even though the method was always documented as a coroutine
850 method, before Python 3.7 it returned a :class:`Future`.
851 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100852
853 .. seealso::
854
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700855 :meth:`loop.create_server` and :func:`start_server`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100856
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700857.. coroutinemethod:: loop.sock_sendfile(sock, file, offset=0, count=None, \
858 \*, fallback=True)
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200859
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700860 Send a file using high-performance :mod:`os.sendfile` if possible.
861 Return the total number of bytes which were sent.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200862
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700863 Asynchronous version of :meth:`socket.sendfile() <socket.socket.sendfile>`.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200864
865 *sock* must be non-blocking :class:`~socket.socket` of
866 :const:`socket.SOCK_STREAM` type.
867
868 *file* must be a regular file object opened in binary mode.
869
870 *offset* tells from where to start reading the file. If specified,
871 *count* is the total number of bytes to transmit as opposed to
872 sending the file until EOF is reached. File position is updated on
873 return or also in case of error in which case :meth:`file.tell()
874 <io.IOBase.tell>` can be used to figure out the number of bytes
875 which were sent.
876
Carol Willing5b7cbd62018-09-12 17:05:17 -0700877 *fallback*, when set to ``True``, makes asyncio manually read and send
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200878 the file when the platform does not support the sendfile syscall
879 (e.g. Windows or SSL socket on Unix).
880
Andrew Svetlov7464e872018-01-19 20:04:29 +0200881 Raise :exc:`SendfileNotAvailableError` if the system does not support
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200882 *sendfile* syscall and *fallback* is ``False``.
883
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700884 The socket *sock* must be non-blocking.
885
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200886 .. versionadded:: 3.7
887
Victor Stinnerc1567df2014-02-08 23:22:58 +0100888
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700889DNS
890^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100891
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700892.. coroutinemethod:: loop.getaddrinfo(host, port, \*, family=0, \
893 type=0, proto=0, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100894
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700895 Asynchronous version of :meth:`socket.getaddrinfo`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100896
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700897.. coroutinemethod:: loop.getnameinfo(sockaddr, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100898
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700899 Asynchronous version of :meth:`socket.getnameinfo`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100900
Yury Selivanovbec23722018-01-28 14:09:40 -0500901.. versionchanged:: 3.7
902 Both *getaddrinfo* and *getnameinfo* methods were always documented
903 to return a coroutine, but prior to Python 3.7 they were, in fact,
904 returning :class:`asyncio.Future` objects. Starting with Python 3.7
905 both methods are coroutines.
906
Victor Stinnerea3183f2013-12-03 01:08:00 +0100907
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700908Working with pipes
909^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100910
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700911.. coroutinemethod:: loop.connect_read_pipe(protocol_factory, pipe)
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200912
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700913 Register a read-pipe in the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100914
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700915 *protocol_factory* must be a callable returning an
916 :ref:`asyncio protocol <asyncio-protocol>` implementation.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100917
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700918 *pipe* is a :term:`file-like object <file object>`.
919
920 Return pair ``(transport, protocol)``, where *transport* supports
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400921 the :class:`ReadTransport` interface and *protocol* is an object
922 instantiated by the *protocol_factory*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100923
Victor Stinnerd84fd732014-08-26 01:01:59 +0200924 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
925 non-blocking mode.
926
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700927.. coroutinemethod:: loop.connect_write_pipe(protocol_factory, pipe)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100928
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700929 Register a write-pipe in the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100930
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700931 *protocol_factory* must be a callable returning an
932 :ref:`asyncio protocol <asyncio-protocol>` implementation.
933
934 *pipe* is :term:`file-like object <file object>`.
935
Victor Stinner2cef3002014-10-23 22:38:46 +0200936 Return pair ``(transport, protocol)``, where *transport* supports
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400937 :class:`WriteTransport` interface and *protocol* is an object
938 instantiated by the *protocol_factory*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100939
Victor Stinnerd84fd732014-08-26 01:01:59 +0200940 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
941 non-blocking mode.
942
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700943.. note::
944
945 :class:`SelectorEventLoop` does not support the above methods on
Carol Willing5b7cbd62018-09-12 17:05:17 -0700946 Windows. Use :class:`ProactorEventLoop` instead for Windows.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700947
Victor Stinner08444382014-02-02 22:43:39 +0100948.. seealso::
949
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700950 The :meth:`loop.subprocess_exec` and
951 :meth:`loop.subprocess_shell` methods.
Victor Stinner08444382014-02-02 22:43:39 +0100952
Victor Stinnerea3183f2013-12-03 01:08:00 +0100953
Victor Stinner8b863482014-01-27 10:07:50 +0100954UNIX signals
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700955^^^^^^^^^^^^
Victor Stinner8b863482014-01-27 10:07:50 +0100956
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700957.. method:: loop.add_signal_handler(signum, callback, \*args)
Victor Stinner8b863482014-01-27 10:07:50 +0100958
959 Add a handler for a signal.
960
961 Raise :exc:`ValueError` if the signal number is invalid or uncatchable.
962 Raise :exc:`RuntimeError` if there is a problem setting up the handler.
963
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700964 Use :func:`functools.partial` :ref:`to pass keywords
965 <asyncio-pass-keywords>` to *func*.
Victor Stinner8464c242014-11-28 13:15:41 +0100966
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700967.. method:: loop.remove_signal_handler(sig)
Victor Stinner8b863482014-01-27 10:07:50 +0100968
969 Remove a handler for a signal.
970
971 Return ``True`` if a signal handler was removed, ``False`` if not.
972
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700973Availability: UNIX.
974
Victor Stinner8b863482014-01-27 10:07:50 +0100975.. seealso::
976
977 The :mod:`signal` module.
978
979
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700980Executing code in thread or process pools
981^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100982
Yury Selivanov394374e2018-09-17 15:35:24 -0400983.. coroutinemethod:: loop.run_in_executor(executor, func, \*args)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100984
Andrew Svetlov1c62b522015-10-01 09:48:08 +0300985 Arrange for a *func* to be called in the specified executor.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100986
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700987 The *executor* argument should be an :class:`concurrent.futures.Executor`
Larry Hastings3732ed22014-03-15 21:13:56 -0700988 instance. The default executor is used if *executor* is ``None``.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100989
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700990 Use :func:`functools.partial` :ref:`to pass keywords
991 <asyncio-pass-keywords>` to *func*.
Victor Stinner8464c242014-11-28 13:15:41 +0100992
Yury Selivanovbec23722018-01-28 14:09:40 -0500993 This method returns a :class:`asyncio.Future` object.
994
Yury Selivanove8a60452016-10-21 17:40:42 -0400995 .. versionchanged:: 3.5.3
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700996 :meth:`loop.run_in_executor` no longer configures the
Yury Selivanove8a60452016-10-21 17:40:42 -0400997 ``max_workers`` of the thread pool executor it creates, instead
998 leaving it up to the thread pool executor
999 (:class:`~concurrent.futures.ThreadPoolExecutor`) to set the
1000 default.
1001
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001002.. method:: loop.set_default_executor(executor)
Victor Stinnerea3183f2013-12-03 01:08:00 +01001003
Elvis Pranskevichus22d25082018-07-30 11:42:43 +01001004 Set *executor* as the default executor used by :meth:`run_in_executor`.
1005 *executor* should be an instance of
1006 :class:`~concurrent.futures.ThreadPoolExecutor`.
1007
1008 .. deprecated:: 3.8
1009 Using an executor that is not an instance of
1010 :class:`~concurrent.futures.ThreadPoolExecutor` is deprecated and
1011 will trigger an error in Python 3.9.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001012
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001013 *executor* must be an instance of
1014 :class:`concurrent.futures.ThreadPoolExecutor`.
1015
Victor Stinnerea3183f2013-12-03 01:08:00 +01001016
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001017Error Handling API
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001018^^^^^^^^^^^^^^^^^^
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001019
Martin Panterc04fb562016-02-10 05:44:01 +00001020Allows customizing how exceptions are handled in the event loop.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001021
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001022.. method:: loop.set_exception_handler(handler)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001023
1024 Set *handler* as the new event loop exception handler.
1025
1026 If *handler* is ``None``, the default exception handler will
1027 be set.
1028
1029 If *handler* is a callable object, it should have a
1030 matching signature to ``(loop, context)``, where ``loop``
1031 will be a reference to the active event loop, ``context``
1032 will be a ``dict`` object (see :meth:`call_exception_handler`
1033 documentation for details about context).
1034
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001035.. method:: loop.get_exception_handler()
Yury Selivanov950204d2016-05-16 16:23:00 -04001036
1037 Return the exception handler, or ``None`` if the default one
1038 is in use.
1039
1040 .. versionadded:: 3.5.2
1041
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001042.. method:: loop.default_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001043
1044 Default exception handler.
1045
1046 This is called when an exception occurs and no exception
Carol Willing5b7cbd62018-09-12 17:05:17 -07001047 handler is set. This can be called by a custom exception
1048 handler that wants to defer to the default handler behavior.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001049
1050 *context* parameter has the same meaning as in
1051 :meth:`call_exception_handler`.
1052
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001053.. method:: loop.call_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001054
1055 Call the current event loop exception handler.
1056
1057 *context* is a ``dict`` object containing the following keys
1058 (new keys may be introduced later):
1059
1060 * 'message': Error message;
1061 * 'exception' (optional): Exception object;
1062 * 'future' (optional): :class:`asyncio.Future` instance;
1063 * 'handle' (optional): :class:`asyncio.Handle` instance;
1064 * 'protocol' (optional): :ref:`Protocol <asyncio-protocol>` instance;
1065 * 'transport' (optional): :ref:`Transport <asyncio-transport>` instance;
1066 * 'socket' (optional): :class:`socket.socket` instance.
1067
1068 .. note::
1069
Carol Willing5b7cbd62018-09-12 17:05:17 -07001070 This method should not be overloaded in subclassed
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001071 event loops. For any custom exception handling, use
1072 :meth:`set_exception_handler()` method.
1073
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001074Enabling debug mode
1075^^^^^^^^^^^^^^^^^^^
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001076
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001077.. method:: loop.get_debug()
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001078
Victor Stinner7b7120e2014-06-23 00:12:14 +02001079 Get the debug mode (:class:`bool`) of the event loop.
1080
1081 The default value is ``True`` if the environment variable
1082 :envvar:`PYTHONASYNCIODEBUG` is set to a non-empty string, ``False``
1083 otherwise.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001084
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001085.. method:: loop.set_debug(enabled: bool)
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001086
1087 Set the debug mode of the event loop.
1088
Yury Selivanov805e27e2018-09-14 16:57:11 -07001089 .. versionchanged:: 3.7
1090
1091 The new ``-X dev`` command line option can now also be used
1092 to enable the debug mode.
1093
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001094.. seealso::
1095
Victor Stinner62511fd2014-06-23 00:36:11 +02001096 The :ref:`debug mode of asyncio <asyncio-debug-mode>`.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001097
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001098
1099Running Subprocesses
1100^^^^^^^^^^^^^^^^^^^^
1101
1102Methods described in this subsections are low-level. In an
1103async/await code consider using high-level convenient
1104:func:`asyncio.create_subprocess_shell` and
1105:func:`asyncio.create_subprocess_exec` functions instead.
1106
1107.. note::
1108
1109 The default event loop that asyncio is pre-configured
1110 to use on **Windows** does not support subprocesses.
1111 See :ref:`Subprocess Support on Windows <asyncio-windows-subprocess>`
1112 for details.
1113
1114.. coroutinemethod:: loop.subprocess_exec(protocol_factory, \*args, \
1115 stdin=subprocess.PIPE, stdout=subprocess.PIPE, \
1116 stderr=subprocess.PIPE, \*\*kwargs)
1117
1118 Create a subprocess from one or more string arguments specified by
1119 *args*.
1120
1121 *args* must be a list of strings represented by:
1122
1123 * :class:`str`;
1124 * or :class:`bytes`, encoded to the
1125 :ref:`filesystem encoding <filesystem-encoding>`.
1126
1127 The first string specifies the program to execute,
Carol Willing5b7cbd62018-09-12 17:05:17 -07001128 and the remaining strings specify the arguments. Together, string
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001129 arguments form the ``argv`` of the program.
1130
1131 This is similar to the standard library :class:`subprocess.Popen`
1132 class called with ``shell=False`` and the list of strings passed as
1133 the first argument; however, where :class:`~subprocess.Popen` takes
1134 a single argument which is list of strings, *subprocess_exec*
1135 takes multiple string arguments.
1136
1137 The *protocol_factory* must instantiate a subclass of the
1138 :class:`asyncio.SubprocessProtocol` class.
1139
1140 Other parameters:
1141
1142 * *stdin*: either a file-like object representing a pipe to be
1143 connected to the subprocess's standard input stream using
1144 :meth:`~loop.connect_write_pipe`, or the
1145 :const:`subprocess.PIPE` constant (default). By default a new
1146 pipe will be created and connected.
1147
1148 * *stdout*: either a file-like object representing the pipe to be
1149 connected to the subprocess's standard output stream using
1150 :meth:`~loop.connect_read_pipe`, or the
1151 :const:`subprocess.PIPE` constant (default). By default a new pipe
1152 will be created and connected.
1153
1154 * *stderr*: either a file-like object representing the pipe to be
1155 connected to the subprocess's standard error stream using
1156 :meth:`~loop.connect_read_pipe`, or one of
1157 :const:`subprocess.PIPE` (default) or :const:`subprocess.STDOUT`
1158 constants.
1159
1160 By default a new pipe will be created and connected. When
1161 :const:`subprocess.STDOUT` is specified, the subprocess' standard
1162 error stream will be connected to the same pipe as the standard
1163 output stream.
1164
1165 * All other keyword arguments are passed to :class:`subprocess.Popen`
1166 without interpretation, except for *bufsize*, *universal_newlines*
1167 and *shell*, which should not be specified at all.
1168
1169 See the constructor of the :class:`subprocess.Popen` class
1170 for documentation on other arguments.
1171
1172 Returns a pair of ``(transport, protocol)``, where *transport*
Bumsik Kim5cc583d2018-09-16 19:40:44 -04001173 conforms to the :class:`asyncio.SubprocessTransport` base class and
1174 *protocol* is an object instantiated by the *protocol_factory*.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001175
1176.. coroutinemethod:: loop.subprocess_shell(protocol_factory, cmd, \*, \
1177 stdin=subprocess.PIPE, stdout=subprocess.PIPE, \
1178 stderr=subprocess.PIPE, \*\*kwargs)
1179
1180 Create a subprocess from *cmd*, which can be a :class:`str` or a
1181 :class:`bytes` string encoded to the
1182 :ref:`filesystem encoding <filesystem-encoding>`,
1183 using the platform's "shell" syntax.
1184
1185 This is similar to the standard library :class:`subprocess.Popen`
1186 class called with ``shell=True``.
1187
1188 The *protocol_factory* must instantiate a subclass of the
1189 :class:`SubprocessProtocol` class.
1190
1191 See :meth:`~loop.subprocess_exec` for more details about
1192 the remaining arguments.
1193
1194 Returns a pair of ``(transport, protocol)``, where *transport*
Bumsik Kim5cc583d2018-09-16 19:40:44 -04001195 conforms to the :class:`SubprocessTransport` base class and
1196 *protocol* is an object instantiated by the *protocol_factory*.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001197
1198.. note::
1199 It is the application's responsibility to ensure that all whitespace
1200 and metacharacters are quoted appropriately to avoid `shell injection
1201 <https://en.wikipedia.org/wiki/Shell_injection#Shell_injection>`_
1202 vulnerabilities. The :func:`shlex.quote` function can be used to
1203 properly escape whitespace and shell metacharacters in strings that
1204 are going to be used to construct shell commands.
1205
1206
1207Callback Handles
1208================
1209
1210.. class:: Handle
1211
1212 A callback wrapper object returned by :meth:`loop.call_soon`,
1213 :meth:`loop.call_soon_threadsafe`.
1214
1215 .. method:: cancel()
1216
1217 Cancel the call. If the callback is already canceled or executed,
1218 this method has no effect.
1219
1220 .. method:: cancelled()
1221
1222 Return ``True`` if the call was cancelled.
1223
1224 .. versionadded:: 3.7
1225
1226.. class:: TimerHandle
1227
1228 A callback wrapper object returned by :meth:`loop.call_later`,
1229 and :meth:`loop.call_at`.
1230
1231 The class is inherited from :class:`Handle`.
1232
1233 .. method:: when()
1234
1235 Return a scheduled callback time as :class:`float` seconds.
1236
1237 The time is an absolute timestamp, using the same time
1238 reference as :meth:`loop.time`.
1239
1240 .. versionadded:: 3.7
1241
1242
1243Server Objects
1244==============
1245
1246Server objects are created by :meth:`loop.create_server`,
1247:meth:`loop.create_unix_server`, :func:`start_server`,
1248and :func:`start_unix_server` functions.
1249
1250Do not instantiate the class directly.
Victor Stinner8c462c52014-01-24 18:11:43 +01001251
Victor Stinner8ebeb032014-07-11 23:47:40 +02001252.. class:: Server
Victor Stinner8c462c52014-01-24 18:11:43 +01001253
Yury Selivanovc9070d02018-01-25 18:08:09 -05001254 *Server* objects are asynchronous context managers. When used in an
1255 ``async with`` statement, it's guaranteed that the Server object is
1256 closed and not accepting new connections when the ``async with``
1257 statement is completed::
1258
1259 srv = await loop.create_server(...)
1260
1261 async with srv:
1262 # some code
1263
Carol Willing5b7cbd62018-09-12 17:05:17 -07001264 # At this point, srv is closed and no longer accepts new connections.
Yury Selivanovc9070d02018-01-25 18:08:09 -05001265
1266
1267 .. versionchanged:: 3.7
1268 Server object is an asynchronous context manager since Python 3.7.
Victor Stinner8c462c52014-01-24 18:11:43 +01001269
1270 .. method:: close()
1271
Victor Stinner4bfb14a2014-07-12 03:20:24 +02001272 Stop serving: close listening sockets and set the :attr:`sockets`
1273 attribute to ``None``.
1274
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001275 The sockets that represent existing incoming client connections
1276 are left open.
Victor Stinner8ebeb032014-07-11 23:47:40 +02001277
Berker Peksag49c9edf2016-01-20 07:14:22 +02001278 The server is closed asynchronously, use the :meth:`wait_closed`
1279 coroutine to wait until the server is closed.
Victor Stinner8c462c52014-01-24 18:11:43 +01001280
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)1634fc22017-12-30 20:39:32 +05301281 .. method:: get_loop()
1282
1283 Gives the event loop associated with the server object.
1284
1285 .. versionadded:: 3.7
1286
Yury Selivanovc9070d02018-01-25 18:08:09 -05001287 .. coroutinemethod:: start_serving()
1288
1289 Start accepting connections.
1290
1291 This method is idempotent, so it can be called when
1292 the server is already being serving.
1293
1294 The new *start_serving* keyword-only parameter to
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001295 :meth:`loop.create_server` and
Yury Selivanovc9070d02018-01-25 18:08:09 -05001296 :meth:`asyncio.start_server` allows to create a Server object
1297 that is not accepting connections right away. In which case
1298 this method, or :meth:`Server.serve_forever` can be used
1299 to make the Server object to start accepting connections.
1300
1301 .. versionadded:: 3.7
1302
1303 .. coroutinemethod:: serve_forever()
1304
1305 Start accepting connections until the coroutine is cancelled.
1306 Cancellation of ``serve_forever`` task causes the server
1307 to be closed.
1308
1309 This method can be called if the server is already accepting
1310 connections. Only one ``serve_forever`` task can exist per
1311 one *Server* object.
1312
1313 Example::
1314
1315 async def client_connected(reader, writer):
1316 # Communicate with the client with
1317 # reader/writer streams. For example:
1318 await reader.readline()
1319
1320 async def main(host, port):
1321 srv = await asyncio.start_server(
1322 client_connected, host, port)
Andrew Svetlov17ab8f02018-02-17 19:44:35 +02001323 await srv.serve_forever()
Yury Selivanovc9070d02018-01-25 18:08:09 -05001324
1325 asyncio.run(main('127.0.0.1', 0))
1326
1327 .. versionadded:: 3.7
1328
1329 .. method:: is_serving()
1330
1331 Return ``True`` if the server is accepting new connections.
1332
1333 .. versionadded:: 3.7
1334
Victor Stinnerbdd574d2015-02-12 22:49:18 +01001335 .. coroutinemethod:: wait_closed()
Victor Stinner8c462c52014-01-24 18:11:43 +01001336
Victor Stinner8ebeb032014-07-11 23:47:40 +02001337 Wait until the :meth:`close` method completes.
1338
Victor Stinner8ebeb032014-07-11 23:47:40 +02001339 .. attribute:: sockets
1340
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001341 List of :class:`socket.socket` objects the server is listening to,
1342 or ``None`` if the server is closed.
Victor Stinner8c462c52014-01-24 18:11:43 +01001343
Yury Selivanovc9070d02018-01-25 18:08:09 -05001344 .. versionchanged:: 3.7
1345 Prior to Python 3.7 ``Server.sockets`` used to return the
1346 internal list of server's sockets directly. In 3.7 a copy
1347 of that list is returned.
1348
Victor Stinner8c462c52014-01-24 18:11:43 +01001349
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001350.. _asyncio-event-loops:
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001351
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001352Event Loops Implementations
1353===========================
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001354
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001355asyncio ships with two different event loop implementations:
1356:class:`SelectorEventLoop` and :class:`ProactorEventLoop`.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001357
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001358By default asyncio is configured to use :class:`SelectorEventLoop`
1359on all platforms.
Andrew Svetlov3d4dbd82018-02-01 19:59:32 +02001360
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001361
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001362.. class:: SelectorEventLoop
1363
1364 An event loop based on the :mod:`selectors` module.
1365
1366 Uses the most efficient *selector* available for the given
1367 platform. It is also possible to manually configure what
1368 exact selector implementation should be used::
1369
1370 import asyncio
1371 import selectors
1372
1373 selector = selectors.SelectSelector()
1374 loop = asyncio.SelectorEventLoop(selector)
1375 asyncio.set_event_loop(loop)
Andrew Svetlov7464e872018-01-19 20:04:29 +02001376
1377
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001378 Availability: UNIX, Windows.
Andrew Svetlov7464e872018-01-19 20:04:29 +02001379
1380
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001381.. class:: ProactorEventLoop
1382
1383 An event loop for Windows that uses "I/O Completion Ports" (IOCP).
1384
1385 Availability: Windows.
1386
1387 An example how to use :class:`ProactorEventLoop` on Windows::
1388
1389 import asyncio
1390 import sys
1391
1392 if sys.platform == 'win32':
1393 loop = asyncio.ProactorEventLoop()
1394 asyncio.set_event_loop(loop)
1395
1396 .. seealso::
1397
1398 `MSDN documentation on I/O Completion Ports
1399 <https://docs.microsoft.com/en-ca/windows/desktop/FileIO/i-o-completion-ports>`_.
1400
1401
1402.. class:: AbstractEventLoop
1403
1404 Abstract base class for asyncio-compliant event loops.
1405
1406 The :ref:`Event Loop Methods <asyncio-event-loop>` section lists all
1407 methods that an alternative implementation of ``AbstractEventLoop``
1408 should have defined.
1409
1410
1411Examples
1412========
1413
1414Note that all examples in this section **purposefully** show how
1415to use low-level event loop APIs such as :meth:`loop.run_forever`
1416and :meth:`loop.call_soon`. Modern asyncio applications rarely
1417need to be written this way; consider using high-level functions
1418like :func:`asyncio.run`.
1419
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001420
Yury Selivanov394374e2018-09-17 15:35:24 -04001421.. _asyncio_example_lowlevel_helloworld:
Victor Stinnerea3183f2013-12-03 01:08:00 +01001422
Victor Stinner7f314ed2014-10-15 18:49:16 +02001423Hello World with call_soon()
Victor Stinnera092a612015-01-09 15:58:41 +01001424^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +01001425
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001426An example using the :meth:`loop.call_soon` method to schedule a
1427callback. The callback displays ``"Hello World"`` and then stops the
1428event loop::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001429
1430 import asyncio
1431
Victor Stinner7f314ed2014-10-15 18:49:16 +02001432 def hello_world(loop):
Carol Willing5b7cbd62018-09-12 17:05:17 -07001433 """A callback to print 'Hello World' and stop the event loop"""
Victor Stinnerea3183f2013-12-03 01:08:00 +01001434 print('Hello World')
Victor Stinner7f314ed2014-10-15 18:49:16 +02001435 loop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001436
1437 loop = asyncio.get_event_loop()
Victor Stinner7f314ed2014-10-15 18:49:16 +02001438
1439 # Schedule a call to hello_world()
1440 loop.call_soon(hello_world, loop)
1441
1442 # Blocking call interrupted by loop.stop()
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001443 try:
1444 loop.run_forever()
1445 finally:
1446 loop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001447
Victor Stinner3e09e322013-12-03 01:22:06 +01001448.. seealso::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001449
Yury Selivanov3faaa882018-09-14 13:32:07 -07001450 A similar :ref:`Hello World <coroutine>`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001451 example created with a coroutine and the :func:`run` function.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001452
Victor Stinner8b863482014-01-27 10:07:50 +01001453
Yury Selivanov394374e2018-09-17 15:35:24 -04001454.. _asyncio_example_call_later:
Victor Stinner7f314ed2014-10-15 18:49:16 +02001455
1456Display the current date with call_later()
Victor Stinnera092a612015-01-09 15:58:41 +01001457^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner7f314ed2014-10-15 18:49:16 +02001458
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001459An example of callback displaying the current date every second. The
1460callback uses the :meth:`loop.call_later` method to reschedule itself
1461during 5 seconds, and then stops the event loop::
Victor Stinner7f314ed2014-10-15 18:49:16 +02001462
1463 import asyncio
1464 import datetime
1465
1466 def display_date(end_time, loop):
1467 print(datetime.datetime.now())
1468 if (loop.time() + 1.0) < end_time:
1469 loop.call_later(1, display_date, end_time, loop)
1470 else:
1471 loop.stop()
1472
1473 loop = asyncio.get_event_loop()
1474
1475 # Schedule the first call to display_date()
1476 end_time = loop.time() + 5.0
1477 loop.call_soon(display_date, end_time, loop)
1478
1479 # Blocking call interrupted by loop.stop()
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001480 try:
1481 loop.run_forever()
1482 finally:
1483 loop.close()
Victor Stinner7f314ed2014-10-15 18:49:16 +02001484
1485.. seealso::
1486
Yury Selivanov7372c3b2018-09-14 15:11:24 -07001487 A similar :ref:`current date <asyncio_example_sleep>` example
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001488 created with a coroutine and the :func:`run` function.
Victor Stinner7f314ed2014-10-15 18:49:16 +02001489
1490
Yury Selivanov394374e2018-09-17 15:35:24 -04001491.. _asyncio_example_watch_fd:
Victor Stinner8b863482014-01-27 10:07:50 +01001492
Victor Stinner04e6df32014-10-11 16:16:27 +02001493Watch a file descriptor for read events
Victor Stinnera092a612015-01-09 15:58:41 +01001494^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001495
1496Wait until a file descriptor received some data using the
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001497:meth:`loop.add_reader` method and then close the event loop::
Victor Stinner04e6df32014-10-11 16:16:27 +02001498
1499 import asyncio
Victor Stinnerac577d72017-11-28 21:33:20 +01001500 from socket import socketpair
Victor Stinner04e6df32014-10-11 16:16:27 +02001501
1502 # Create a pair of connected file descriptors
Victor Stinnerccd8e342014-10-11 16:30:02 +02001503 rsock, wsock = socketpair()
Carol Willing5b7cbd62018-09-12 17:05:17 -07001504
Victor Stinner04e6df32014-10-11 16:16:27 +02001505 loop = asyncio.get_event_loop()
1506
1507 def reader():
1508 data = rsock.recv(100)
1509 print("Received:", data.decode())
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001510
Victor Stinner2cef3002014-10-23 22:38:46 +02001511 # We are done: unregister the file descriptor
Victor Stinner04e6df32014-10-11 16:16:27 +02001512 loop.remove_reader(rsock)
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001513
Victor Stinner04e6df32014-10-11 16:16:27 +02001514 # Stop the event loop
1515 loop.stop()
1516
Victor Stinner2cef3002014-10-23 22:38:46 +02001517 # Register the file descriptor for read event
Victor Stinner04e6df32014-10-11 16:16:27 +02001518 loop.add_reader(rsock, reader)
1519
1520 # Simulate the reception of data from the network
1521 loop.call_soon(wsock.send, 'abc'.encode())
1522
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001523 try:
1524 # Run the event loop
1525 loop.run_forever()
1526 finally:
Carol Willing5b7cbd62018-09-12 17:05:17 -07001527 # We are done. Close sockets and the event loop.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001528 rsock.close()
1529 wsock.close()
1530 loop.close()
Victor Stinner04e6df32014-10-11 16:16:27 +02001531
1532.. seealso::
1533
Yury Selivanov394374e2018-09-17 15:35:24 -04001534 * A similar :ref:`example <asyncio_example_create_connection>`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001535 using transports, protocols, and the
1536 :meth:`loop.create_connection` method.
Victor Stinner04e6df32014-10-11 16:16:27 +02001537
Yury Selivanov394374e2018-09-17 15:35:24 -04001538 * Another similar :ref:`example <asyncio_example_create_connection-streams>`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001539 using the high-level :func:`asyncio.open_connection` function
1540 and streams.
Victor Stinner04e6df32014-10-11 16:16:27 +02001541
1542
Yury Selivanov394374e2018-09-17 15:35:24 -04001543.. _asyncio_example_unix_signals:
1544
Victor Stinner04e6df32014-10-11 16:16:27 +02001545Set signal handlers for SIGINT and SIGTERM
Victor Stinnera092a612015-01-09 15:58:41 +01001546^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001547
Carol Willing5b7cbd62018-09-12 17:05:17 -07001548(This ``signals`` example only works on UNIX.)
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001549
1550Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM`
1551using the :meth:`loop.add_signal_handler` method::
Victor Stinner8b863482014-01-27 10:07:50 +01001552
1553 import asyncio
1554 import functools
1555 import os
1556 import signal
1557
1558 def ask_exit(signame):
1559 print("got signal %s: exit" % signame)
1560 loop.stop()
1561
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001562 async def main():
1563 loop = asyncio.get_running_loop()
Victor Stinner8b863482014-01-27 10:07:50 +01001564
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001565 for signame in {'SIGINT', 'SIGTERM'}:
1566 loop.add_signal_handler(
1567 getattr(signal, signame),
1568 functools.partial(ask_exit, signame))
Victor Stinner2cef3002014-10-23 22:38:46 +02001569
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001570 await asyncio.sleep(3600)
1571
1572 print("Event loop running for 1 hour, press Ctrl+C to interrupt.")
1573 print(f"pid {os.getpid()}: send SIGINT or SIGTERM to exit.")
1574
1575 asyncio.run(main())