blob: f763fd5f036d68fe7226b7aead164e604d44306b [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
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040011The event loop is the core 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
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040015Application 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.
Yury Selivanov7c7605f2018-09-11 09:54:40 -070020
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040021.. rubric:: Obtaining the Event Loop
Yury Selivanov7c7605f2018-09-11 09:54:40 -070022
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
Carol Willing5b7cbd62018-09-12 17:05:17 -070042 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.
Yury Selivanov7c7605f2018-09-11 09:54:40 -070046
Carol Willing5b7cbd62018-09-12 17:05:17 -070047 Consider also using the :func:`asyncio.run` function instead of using
48 lower level functions to manually create and close an event loop.
Yury Selivanov7c7605f2018-09-11 09:54:40 -070049
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
Carol Willing5b7cbd62018-09-12 17:05:17 -070067* The `Event Loop Methods`_ section is the reference documentation of
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040068 the event loop APIs;
Yury Selivanov7c7605f2018-09-11 09:54:40 -070069
70* The `Callback Handles`_ section documents the :class:`Handle` and
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040071 :class:`TimerHandle` instances which are returned from scheduling
72 methods such as :meth:`loop.call_soon` and :meth:`loop.call_later`;
Yury Selivanov7c7605f2018-09-11 09:54:40 -070073
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040074* The `Server Objects`_ section documents types returned from
Yury Selivanov7c7605f2018-09-11 09:54:40 -070075 event loop methods like :meth:`loop.create_server`;
76
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040077* The `Event Loop Implementations`_ section documents the
Yury Selivanov7c7605f2018-09-11 09:54:40 -070078 :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 Selivanov7c7605f2018-09-11 09:54:40 -070086Event Loop Methods
87==================
Victor Stinnerea3183f2013-12-03 01:08:00 +010088
Carol Willing5b7cbd62018-09-12 17:05:17 -070089Event loops have **low-level** APIs for the following:
lf627d2c82017-07-25 17:03:51 -060090
Yury Selivanov7c7605f2018-09-11 09:54:40 -070091.. contents::
92 :depth: 1
93 :local:
Victor Stinnerea3183f2013-12-03 01:08:00 +010094
Victor Stinnerea3183f2013-12-03 01:08:00 +010095
Yury Selivanov7c7605f2018-09-11 09:54:40 -070096Running and stopping the loop
97^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +010098
Yury Selivanov7c7605f2018-09-11 09:54:40 -070099.. method:: loop.run_until_complete(future)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100100
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400101 Run until the *future* (an instance of :class:`Future`) has
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700102 completed.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100103
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700104 If the argument is a :ref:`coroutine object <coroutine>` it
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400105 is implicitly scheduled to run as a :class:`asyncio.Task`.
Eli Bendersky136fea22014-02-09 06:55:58 -0800106
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700107 Return the Future's result or raise its exception.
Guido van Rossumf68afd82016-08-08 09:41:21 -0700108
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700109.. method:: loop.run_forever()
Guido van Rossumf68afd82016-08-08 09:41:21 -0700110
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700111 Run the event loop until :meth:`stop` is called.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100112
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700113 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 Selivanov7c7605f2018-09-11 09:54:40 -0700119 the loop will run the current batch of callbacks and then exit.
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400120 Note that new callbacks scheduled by callbacks will not run in this
Carol Willing5b7cbd62018-09-12 17:05:17 -0700121 case; instead, they will run the next time :meth:`run_forever` or
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700122 :meth:`run_until_complete` is called.
Guido van Rossum41f69f42015-11-19 13:28:47 -0800123
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700124.. method:: loop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100125
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700126 Stop the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100127
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700128.. method:: loop.is_running()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100129
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700130 Return ``True`` if the event loop is currently running.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100131
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700132.. method:: loop.is_closed()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100133
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700134 Return ``True`` if the event loop was closed.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100135
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700136.. method:: loop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100137
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700138 Close the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100139
Andriy Maletskyb83d9172018-10-29 23:39:21 +0200140 The loop must not be running when this function is called.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700141 Any pending callbacks will be discarded.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100142
Łukasz Langa7f9a2ae2019-06-04 13:03:20 +0200143 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 Selivanov7c7605f2018-09-11 09:54:40 -0700146 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 Selivanov7c7605f2018-09-11 09:54:40 -0700149.. 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 Selivanov7c7605f2018-09-11 09:54:40 -0700153 the event loop will issue a warning if a new asynchronous generator
Carol Willing5b7cbd62018-09-12 17:05:17 -0700154 is iterated. This should be used to reliably finalize all scheduled
Yury Selivanovac94e382018-09-17 23:58:00 -0400155 asynchronous generators.
Carol Willing5b7cbd62018-09-12 17:05:17 -0700156
Yury Selivanovac94e382018-09-17 23:58:00 -0400157 Note that there is no need to call this function when
158 :func:`asyncio.run` is used.
159
160 Example::
Yury Selivanov03660042016-12-15 17:36:05 -0500161
162 try:
163 loop.run_forever()
164 finally:
165 loop.run_until_complete(loop.shutdown_asyncgens())
166 loop.close()
167
168 .. versionadded:: 3.6
169
170
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700171Scheduling callbacks
172^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100173
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700174.. method:: loop.call_soon(callback, *args, context=None)
Victor Stinner8464c242014-11-28 13:15:41 +0100175
Carol Willing5b7cbd62018-09-12 17:05:17 -0700176 Schedule a *callback* to be called with *args* arguments at
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700177 the next iteration of the event loop.
Victor Stinner8464c242014-11-28 13:15:41 +0100178
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700179 Callbacks are called in the order in which they are registered.
180 Each callback will be called exactly once.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100181
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700182 An optional keyword-only *context* argument allows specifying a
183 custom :class:`contextvars.Context` for the *callback* to run in.
184 The current context is used when no *context* is provided.
Yury Selivanov28b91782018-05-23 13:35:04 -0400185
Yury Selivanov1096f762015-06-25 13:49:52 -0400186 An instance of :class:`asyncio.Handle` is returned, which can be
Carol Willing5b7cbd62018-09-12 17:05:17 -0700187 used later to cancel the callback.
188
189 This method is not thread-safe.
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500190
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700191.. method:: loop.call_soon_threadsafe(callback, *args, context=None)
Victor Stinner8464c242014-11-28 13:15:41 +0100192
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700193 A thread-safe variant of :meth:`call_soon`. Must be used to
194 schedule callbacks *from another thread*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100195
Victor Stinner83704962015-02-25 14:24:15 +0100196 See the :ref:`concurrency and multithreading <asyncio-multithreading>`
197 section of the documentation.
198
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700199.. versionchanged:: 3.7
200 The *context* keyword-only parameter was added. See :pep:`567`
201 for more details.
202
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400203.. _asyncio-pass-keywords:
204
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700205.. note::
206
Carol Willing5b7cbd62018-09-12 17:05:17 -0700207 Most :mod:`asyncio` scheduling functions don't allow passing
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400208 keyword arguments. To do that, use :func:`functools.partial`::
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700209
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,
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400215 as asyncio can render partial objects better in debug and error
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700216 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
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400238 scheduled for exactly the same time, the order in which they
239 are called is undefined.
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
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400253 .. versionchanged:: 3.8
254 In Python 3.7 and earlier with the default event loop implementation,
255 the *delay* could not exceed one day.
256 This has been fixed in Python 3.8.
257
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700258.. method:: loop.call_at(when, callback, *args, context=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100259
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700260 Schedule *callback* to be called at the given absolute timestamp
261 *when* (an int or a float), using the same time reference as
262 :meth:`loop.time`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100263
264 This method's behavior is the same as :meth:`call_later`.
265
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700266 An instance of :class:`asyncio.TimerHandle` is returned which can
267 be used to cancel the callback.
Victor Stinner8464c242014-11-28 13:15:41 +0100268
Yury Selivanov28b91782018-05-23 13:35:04 -0400269 .. versionchanged:: 3.7
270 The *context* keyword-only parameter was added. See :pep:`567`
271 for more details.
272
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400273 .. versionchanged:: 3.8
274 In Python 3.7 and earlier with the default event loop implementation,
275 the difference between *when* and the current time could not exceed
276 one day. This has been fixed in Python 3.8.
277
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700278.. method:: loop.time()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100279
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700280 Return the current time, as a :class:`float` value, according to
281 the event loop's internal monotonic clock.
282
283.. note::
Enrico Alarico Carbognani7e954e72019-04-18 14:43:14 +0200284 .. versionchanged:: 3.8
285 In Python 3.7 and earlier timeouts (relative *delay* or absolute *when*)
286 should not exceed one day. This has been fixed in Python 3.8.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100287
Victor Stinner3e09e322013-12-03 01:22:06 +0100288.. seealso::
289
290 The :func:`asyncio.sleep` function.
291
Victor Stinnerea3183f2013-12-03 01:08:00 +0100292
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700293Creating Futures and Tasks
294^^^^^^^^^^^^^^^^^^^^^^^^^^
Yury Selivanov950204d2016-05-16 16:23:00 -0400295
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700296.. method:: loop.create_future()
Yury Selivanov950204d2016-05-16 16:23:00 -0400297
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700298 Create an :class:`asyncio.Future` object attached to the event loop.
Yury Selivanov950204d2016-05-16 16:23:00 -0400299
Carol Willing5b7cbd62018-09-12 17:05:17 -0700300 This is the preferred way to create Futures in asyncio. This lets
301 third-party event loops provide alternative implementations of
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700302 the Future object (with better performance or instrumentation).
Yury Selivanov950204d2016-05-16 16:23:00 -0400303
304 .. versionadded:: 3.5.2
305
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700306.. method:: loop.create_task(coro, \*, name=None)
Yury Selivanov950204d2016-05-16 16:23:00 -0400307
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700308 Schedule the execution of a :ref:`coroutine`.
309 Return a :class:`Task` object.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200310
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700311 Third-party event loops can use their own subclass of :class:`Task`
312 for interoperability. In this case, the result type is a subclass
313 of :class:`Task`.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200314
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700315 If the *name* argument is provided and not ``None``, it is set as
316 the name of the task using :meth:`Task.set_name`.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200317
Alex Grönholmcca4eec2018-08-09 00:06:47 +0300318 .. versionchanged:: 3.8
319 Added the ``name`` parameter.
320
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700321.. method:: loop.set_task_factory(factory)
Yury Selivanov71854612015-05-11 16:28:27 -0400322
323 Set a task factory that will be used by
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700324 :meth:`loop.create_task`.
Yury Selivanov71854612015-05-11 16:28:27 -0400325
326 If *factory* is ``None`` the default task factory will be set.
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400327 Otherwise, *factory* must be a *callable* with the signature matching
328 ``(loop, coro)``, where *loop* is a reference to the active
329 event loop, and *coro* is a coroutine object. The callable
330 must return a :class:`asyncio.Future`-compatible object.
Yury Selivanov71854612015-05-11 16:28:27 -0400331
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700332.. method:: loop.get_task_factory()
Yury Selivanov71854612015-05-11 16:28:27 -0400333
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700334 Return a task factory or ``None`` if the default one is in use.
Yury Selivanov71854612015-05-11 16:28:27 -0400335
Victor Stinner530ef2f2014-07-08 12:39:10 +0200336
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700337Opening network connections
338^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100339
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700340.. coroutinemethod:: loop.create_connection(protocol_factory, \
341 host=None, port=None, \*, ssl=None, \
342 family=0, proto=0, flags=0, sock=None, \
343 local_addr=None, server_hostname=None, \
344 ssl_handshake_timeout=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100345
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700346 Open a streaming transport connection to a given
347 address specified by *host* and *port*.
348
349 The socket family can be either :py:data:`~socket.AF_INET` or
350 :py:data:`~socket.AF_INET6` depending on *host* (or the *family*
351 argument, if provided).
352
353 The socket type will be :py:data:`~socket.SOCK_STREAM`.
354
355 *protocol_factory* must be a callable returning an
356 :ref:`asyncio protocol <asyncio-protocol>` implementation.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100357
Yury Selivanov19a44f62017-12-14 20:53:26 -0500358 This method will try to establish the connection in the background.
359 When successful, it returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100360
361 The chronological synopsis of the underlying operation is as follows:
362
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700363 #. The connection is established and a :ref:`transport <asyncio-transport>`
364 is created for it.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100365
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700366 #. *protocol_factory* is called without arguments and is expected to
367 return a :ref:`protocol <asyncio-protocol>` instance.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100368
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700369 #. The protocol instance is coupled with the transport by calling its
370 :meth:`~BaseProtocol.connection_made` method.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100371
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700372 #. A ``(transport, protocol)`` tuple is returned on success.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100373
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700374 The created transport is an implementation-dependent bidirectional
375 stream.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100376
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700377 Other arguments:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100378
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400379 * *ssl*: if given and not false, a SSL/TLS transport is created
Victor Stinnerea3183f2013-12-03 01:08:00 +0100380 (by default a plain TCP transport is created). If *ssl* is
381 a :class:`ssl.SSLContext` object, this context is used to create
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400382 the transport; if *ssl* is :const:`True`, a default context returned
383 from :func:`ssl.create_default_context` is used.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100384
Berker Peksag9c1dba22014-09-28 00:00:58 +0300385 .. seealso:: :ref:`SSL/TLS security considerations <ssl-security>`
Antoine Pitrouc5e075f2014-03-22 18:19:11 +0100386
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400387 * *server_hostname* sets or overrides the hostname that the target
388 server's certificate will be matched against. Should only be passed
389 if *ssl* is not ``None``. By default the value of the *host* argument
Victor Stinnerea3183f2013-12-03 01:08:00 +0100390 is used. If *host* is empty, there is no default and you must pass a
391 value for *server_hostname*. If *server_hostname* is an empty
392 string, hostname matching is disabled (which is a serious security
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400393 risk, allowing for potential man-in-the-middle attacks).
Victor Stinnerea3183f2013-12-03 01:08:00 +0100394
395 * *family*, *proto*, *flags* are the optional address family, protocol
396 and flags to be passed through to getaddrinfo() for *host* resolution.
397 If given, these should all be integers from the corresponding
398 :mod:`socket` module constants.
399
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800400 * *happy_eyeballs_delay*, if given, enables Happy Eyeballs for this
401 connection. It should
402 be a floating-point number representing the amount of time in seconds
403 to wait for a connection attempt to complete, before starting the next
404 attempt in parallel. This is the "Connection Attempt Delay" as defined
405 in :rfc:`8305`. A sensible default value recommended by the RFC is ``0.25``
406 (250 milliseconds).
407
408 * *interleave* controls address reordering when a host name resolves to
409 multiple IP addresses.
410 If ``0`` or unspecified, no reordering is done, and addresses are
411 tried in the order returned by :meth:`getaddrinfo`. If a positive integer
412 is specified, the addresses are interleaved by address family, and the
413 given integer is interpreted as "First Address Family Count" as defined
414 in :rfc:`8305`. The default is ``0`` if *happy_eyeballs_delay* is not
415 specified, and ``1`` if it is.
416
Victor Stinnerea3183f2013-12-03 01:08:00 +0100417 * *sock*, if given, should be an existing, already connected
418 :class:`socket.socket` object to be used by the transport.
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800419 If *sock* is given, none of *host*, *port*, *family*, *proto*, *flags*,
420 *happy_eyeballs_delay*, *interleave*
Victor Stinnerea3183f2013-12-03 01:08:00 +0100421 and *local_addr* should be specified.
422
423 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
424 to bind the socket to locally. The *local_host* and *local_port*
Carol Willing5b7cbd62018-09-12 17:05:17 -0700425 are looked up using ``getaddrinfo()``, similarly to *host* and *port*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100426
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400427 * *ssl_handshake_timeout* is (for a TLS connection) the time in seconds
428 to wait for the TLS handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400429 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000430
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800431 .. versionadded:: 3.8
432
433 The *happy_eyeballs_delay* and *interleave* parameters.
434
Neil Aspinallf7686c12017-12-19 19:45:42 +0000435 .. versionadded:: 3.7
436
437 The *ssl_handshake_timeout* parameter.
438
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700439 .. versionchanged:: 3.6
440
441 The socket option :py:data:`~socket.TCP_NODELAY` is set by default
442 for all TCP connections.
443
Victor Stinner60208a12015-09-15 22:41:52 +0200444 .. versionchanged:: 3.5
445
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400446 Added support for SSL/TLS in :class:`ProactorEventLoop`.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200447
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100448 .. seealso::
449
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700450 The :func:`open_connection` function is a high-level alternative
451 API. It returns a pair of (:class:`StreamReader`, :class:`StreamWriter`)
452 that can be used directly in async/await code.
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100453
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700454.. coroutinemethod:: loop.create_datagram_endpoint(protocol_factory, \
455 local_addr=None, remote_addr=None, \*, \
456 family=0, proto=0, flags=0, \
457 reuse_address=None, reuse_port=None, \
458 allow_broadcast=None, sock=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100459
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700460 Create a datagram connection.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100461
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700462 The socket family can be either :py:data:`~socket.AF_INET`,
463 :py:data:`~socket.AF_INET6`, or :py:data:`~socket.AF_UNIX`,
464 depending on *host* (or the *family* argument, if provided).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100465
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700466 The socket type will be :py:data:`~socket.SOCK_DGRAM`.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100467
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700468 *protocol_factory* must be a callable returning a
469 :ref:`protocol <asyncio-protocol>` implementation.
470
471 A tuple of ``(transport, protocol)`` is returned on success.
472
473 Other arguments:
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700474
475 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
476 to bind the socket to locally. The *local_host* and *local_port*
477 are looked up using :meth:`getaddrinfo`.
478
479 * *remote_addr*, if given, is a ``(remote_host, remote_port)`` tuple used
480 to connect the socket to a remote address. The *remote_host* and
481 *remote_port* are looked up using :meth:`getaddrinfo`.
482
483 * *family*, *proto*, *flags* are the optional address family, protocol
484 and flags to be passed through to :meth:`getaddrinfo` for *host*
485 resolution. If given, these should all be integers from the
486 corresponding :mod:`socket` module constants.
487
488 * *reuse_address* tells the kernel to reuse a local socket in
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700489 ``TIME_WAIT`` state, without waiting for its natural timeout to
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300490 expire. If not specified will automatically be set to ``True`` on
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400491 Unix.
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700492
493 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
494 same port as other existing endpoints are bound to, so long as they all
495 set this flag when being created. This option is not supported on Windows
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400496 and some Unixes. If the :py:data:`~socket.SO_REUSEPORT` constant is not
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700497 defined then this capability is unsupported.
498
499 * *allow_broadcast* tells the kernel to allow this endpoint to send
500 messages to the broadcast address.
501
502 * *sock* can optionally be specified in order to use a preexisting,
503 already connected, :class:`socket.socket` object to be used by the
504 transport. If specified, *local_addr* and *remote_addr* should be omitted
505 (must be :const:`None`).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100506
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200507 See :ref:`UDP echo client protocol <asyncio-udp-echo-client-protocol>` and
508 :ref:`UDP echo server protocol <asyncio-udp-echo-server-protocol>` examples.
509
Romuald Brunet0ded5802018-05-14 18:22:00 +0200510 .. versionchanged:: 3.4.4
511 The *family*, *proto*, *flags*, *reuse_address*, *reuse_port,
512 *allow_broadcast*, and *sock* parameters were added.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100513
Andrew Svetlovbafd4b52019-05-28 12:52:15 +0300514 .. versionchanged:: 3.8
515 Added support for Windows.
516
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700517.. coroutinemethod:: loop.create_unix_connection(protocol_factory, \
518 path=None, \*, ssl=None, sock=None, \
519 server_hostname=None, ssl_handshake_timeout=None)
Victor Stinnera6919aa2014-02-19 13:32:34 +0100520
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400521 Create a Unix connection.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100522
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700523 The socket family will be :py:data:`~socket.AF_UNIX`; socket
524 type will be :py:data:`~socket.SOCK_STREAM`.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100525
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700526 A tuple of ``(transport, protocol)`` is returned on success.
Guido van Rossum9e80eeb2016-11-03 14:17:25 -0700527
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400528 *path* is the name of a Unix domain socket and is required,
529 unless a *sock* parameter is specified. Abstract Unix sockets,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700530 :class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths are
531 supported.
532
533 See the documentation of the :meth:`loop.create_connection` method
534 for information about arguments to this method.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100535
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400536 .. availability:: Unix.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100537
Neil Aspinallf7686c12017-12-19 19:45:42 +0000538 .. versionadded:: 3.7
539
540 The *ssl_handshake_timeout* parameter.
541
Yury Selivanov423fd362017-11-20 17:26:28 -0500542 .. versionchanged:: 3.7
543
Elvis Pranskevichusc0d062f2018-06-08 11:36:00 -0400544 The *path* parameter can now be a :term:`path-like object`.
Yury Selivanov423fd362017-11-20 17:26:28 -0500545
Victor Stinnera6919aa2014-02-19 13:32:34 +0100546
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700547Creating network servers
548^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100549
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700550.. coroutinemethod:: loop.create_server(protocol_factory, \
551 host=None, port=None, \*, \
552 family=socket.AF_UNSPEC, \
553 flags=socket.AI_PASSIVE, \
554 sock=None, backlog=100, ssl=None, \
555 reuse_address=None, reuse_port=None, \
556 ssl_handshake_timeout=None, start_serving=True)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100557
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700558 Create a TCP server (socket type :data:`~socket.SOCK_STREAM`) listening
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400559 on *port* of the *host* address.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200560
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700561 Returns a :class:`Server` object.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200562
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700563 Arguments:
Victor Stinner33f6abe2014-10-12 20:36:04 +0200564
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400565 * *protocol_factory* must be a callable returning a
566 :ref:`protocol <asyncio-protocol>` implementation.
567
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400568 * The *host* parameter can be set to several types which determine where
569 the server would be listening:
570
571 - If *host* is a string, the TCP server is bound to a single network
572 interface specified by *host*.
573
574 - If *host* is a sequence of strings, the TCP server is bound to all
575 network interfaces specified by the sequence.
576
577 - If *host* is an empty string or ``None``, all interfaces are
578 assumed and a list of multiple sockets will be returned (most likely
579 one for IPv4 and another one for IPv6).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200580
581 * *family* can be set to either :data:`socket.AF_INET` or
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700582 :data:`~socket.AF_INET6` to force the socket to use IPv4 or IPv6.
Carol Willing5b7cbd62018-09-12 17:05:17 -0700583 If not set, the *family* will be determined from host name
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700584 (defaults to :data:`~socket.AF_UNSPEC`).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200585
586 * *flags* is a bitmask for :meth:`getaddrinfo`.
587
588 * *sock* can optionally be specified in order to use a preexisting
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400589 socket object. If specified, *host* and *port* must not be specified.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200590
591 * *backlog* is the maximum number of queued connections passed to
592 :meth:`~socket.socket.listen` (defaults to 100).
593
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400594 * *ssl* can be set to an :class:`~ssl.SSLContext` instance to enable
595 TLS over the accepted connections.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200596
597 * *reuse_address* tells the kernel to reuse a local socket in
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700598 ``TIME_WAIT`` state, without waiting for its natural timeout to
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300599 expire. If not specified will automatically be set to ``True`` on
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400600 Unix.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100601
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700602 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
603 same port as other existing endpoints are bound to, so long as they all
604 set this flag when being created. This option is not supported on
605 Windows.
606
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400607 * *ssl_handshake_timeout* is (for a TLS server) the time in seconds to wait
608 for the TLS handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400609 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000610
Yury Selivanovc9070d02018-01-25 18:08:09 -0500611 * *start_serving* set to ``True`` (the default) causes the created server
612 to start accepting connections immediately. When set to ``False``,
613 the user should await on :meth:`Server.start_serving` or
614 :meth:`Server.serve_forever` to make the server to start accepting
615 connections.
616
Neil Aspinallf7686c12017-12-19 19:45:42 +0000617 .. versionadded:: 3.7
618
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700619 Added *ssl_handshake_timeout* and *start_serving* parameters.
620
621 .. versionchanged:: 3.6
622
623 The socket option :py:data:`~socket.TCP_NODELAY` is set by default
624 for all TCP connections.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000625
Victor Stinner60208a12015-09-15 22:41:52 +0200626 .. versionchanged:: 3.5
627
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400628 Added support for SSL/TLS in :class:`ProactorEventLoop`.
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100629
Victor Stinner7b58a2b2015-09-21 18:41:05 +0200630 .. versionchanged:: 3.5.1
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200631
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700632 The *host* parameter can be a sequence of strings.
633
634 .. seealso::
635
636 The :func:`start_server` function is a higher-level alternative API
637 that returns a pair of :class:`StreamReader` and :class:`StreamWriter`
638 that can be used in an async/await code.
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200639
Victor Stinnerea3183f2013-12-03 01:08:00 +0100640
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700641.. coroutinemethod:: loop.create_unix_server(protocol_factory, path=None, \
642 \*, sock=None, backlog=100, ssl=None, \
643 ssl_handshake_timeout=None, start_serving=True)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100644
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700645 Similar to :meth:`loop.create_server` but works with the
646 :py:data:`~socket.AF_UNIX` socket family.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100647
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400648 *path* is the name of a Unix domain socket, and is required,
649 unless a *sock* argument is provided. Abstract Unix sockets,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700650 :class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths
651 are supported.
Yury Selivanov423fd362017-11-20 17:26:28 -0500652
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400653 See the documentation of the :meth:`loop.create_server` method
654 for information about arguments to this method.
655
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400656 .. availability:: Unix.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100657
Neil Aspinallf7686c12017-12-19 19:45:42 +0000658 .. versionadded:: 3.7
659
Elvis Pranskevichusc0d062f2018-06-08 11:36:00 -0400660 The *ssl_handshake_timeout* and *start_serving* parameters.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000661
Yury Selivanov423fd362017-11-20 17:26:28 -0500662 .. versionchanged:: 3.7
663
664 The *path* parameter can now be a :class:`~pathlib.Path` object.
665
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700666.. coroutinemethod:: loop.connect_accepted_socket(protocol_factory, \
667 sock, \*, ssl=None, ssl_handshake_timeout=None)
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500668
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700669 Wrap an already accepted connection into a transport/protocol pair.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500670
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700671 This method can be used by servers that accept connections outside
672 of asyncio but that use asyncio to handle them.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500673
674 Parameters:
675
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400676 * *protocol_factory* must be a callable returning a
677 :ref:`protocol <asyncio-protocol>` implementation.
678
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700679 * *sock* is a preexisting socket object returned from
680 :meth:`socket.accept <socket.socket.accept>`.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500681
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700682 * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over
683 the accepted connections.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500684
Neil Aspinallf7686c12017-12-19 19:45:42 +0000685 * *ssl_handshake_timeout* is (for an SSL connection) the time in seconds to
686 wait for the SSL handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400687 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000688
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700689 Returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100690
Neil Aspinallf7686c12017-12-19 19:45:42 +0000691 .. versionadded:: 3.7
692
693 The *ssl_handshake_timeout* parameter.
694
AraHaan431665b2017-11-21 11:06:26 -0500695 .. versionadded:: 3.5.3
696
697
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700698Transferring files
699^^^^^^^^^^^^^^^^^^
Andrew Svetlov7c684072018-01-27 21:22:47 +0200700
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700701.. coroutinemethod:: loop.sendfile(transport, file, \
702 offset=0, count=None, *, fallback=True)
Andrew Svetlov7c684072018-01-27 21:22:47 +0200703
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700704 Send a *file* over a *transport*. Return the total number of bytes
705 sent.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200706
707 The method uses high-performance :meth:`os.sendfile` if available.
708
709 *file* must be a regular file object opened in binary mode.
710
711 *offset* tells from where to start reading the file. If specified,
712 *count* is the total number of bytes to transmit as opposed to
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400713 sending the file until EOF is reached. File position is always updated,
714 even when this method raises an error, and
715 :meth:`file.tell() <io.IOBase.tell>` can be used to obtain the actual
716 number of bytes sent.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200717
718 *fallback* set to ``True`` makes asyncio to manually read and send
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700719 the file when the platform does not support the sendfile system call
Andrew Svetlov7c684072018-01-27 21:22:47 +0200720 (e.g. Windows or SSL socket on Unix).
721
722 Raise :exc:`SendfileNotAvailableError` if the system does not support
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400723 the *sendfile* syscall and *fallback* is ``False``.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200724
725 .. versionadded:: 3.7
726
727
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500728TLS Upgrade
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700729^^^^^^^^^^^
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500730
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700731.. coroutinemethod:: loop.start_tls(transport, protocol, \
732 sslcontext, \*, server_side=False, \
733 server_hostname=None, ssl_handshake_timeout=None)
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500734
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700735 Upgrade an existing transport-based connection to TLS.
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500736
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700737 Return a new transport instance, that the *protocol* must start using
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500738 immediately after the *await*. The *transport* instance passed to
739 the *start_tls* method should never be used again.
740
741 Parameters:
742
743 * *transport* and *protocol* instances that methods like
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700744 :meth:`~loop.create_server` and
745 :meth:`~loop.create_connection` return.
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500746
747 * *sslcontext*: a configured instance of :class:`~ssl.SSLContext`.
748
749 * *server_side* pass ``True`` when a server-side connection is being
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700750 upgraded (like the one created by :meth:`~loop.create_server`).
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500751
752 * *server_hostname*: sets or overrides the host name that the target
753 server's certificate will be matched against.
754
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400755 * *ssl_handshake_timeout* is (for a TLS connection) the time in seconds to
756 wait for the TLS handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400757 ``60.0`` seconds if ``None`` (default).
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500758
759 .. versionadded:: 3.7
760
761
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700762Watching file descriptors
763^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerc1567df2014-02-08 23:22:58 +0100764
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700765.. method:: loop.add_reader(fd, callback, \*args)
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200766
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400767 Start monitoring the *fd* file descriptor for read availability and
768 invoke *callback* with the specified arguments once *fd* is available for
769 reading.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200770
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700771.. method:: loop.remove_reader(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100772
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400773 Stop monitoring the *fd* file descriptor for read availability.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100774
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700775.. method:: loop.add_writer(fd, callback, \*args)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100776
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400777 Start monitoring the *fd* file descriptor for write availability and
778 invoke *callback* with the specified arguments once *fd* is available for
779 writing.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100780
Yury Selivanove247b462018-09-20 12:43:59 -0400781 Use :func:`functools.partial` :ref:`to pass keyword arguments
Naglis17473342018-12-04 09:31:15 +0200782 <asyncio-pass-keywords>` to *callback*.
Victor Stinner8464c242014-11-28 13:15:41 +0100783
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700784.. method:: loop.remove_writer(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100785
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400786 Stop monitoring the *fd* file descriptor for write availability.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100787
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700788See also :ref:`Platform Support <asyncio-platform-support>` section
789for some limitations of these methods.
Victor Stinner04e6df32014-10-11 16:16:27 +0200790
Victor Stinnerc1567df2014-02-08 23:22:58 +0100791
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700792Working with socket objects directly
793^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerc1567df2014-02-08 23:22:58 +0100794
Carol Willing5b7cbd62018-09-12 17:05:17 -0700795In general, protocol implementations that use transport-based APIs
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700796such as :meth:`loop.create_connection` and :meth:`loop.create_server`
797are faster than implementations that work with sockets directly.
Carol Willing5b7cbd62018-09-12 17:05:17 -0700798However, there are some use cases when performance is not critical, and
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700799working with :class:`~socket.socket` objects directly is more
800convenient.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100801
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700802.. coroutinemethod:: loop.sock_recv(sock, nbytes)
Yury Selivanov55c50842016-06-08 12:48:15 -0400803
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400804 Receive up to *nbytes* from *sock*. Asynchronous version of
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700805 :meth:`socket.recv() <socket.socket.recv>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100806
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400807 Return the received data as a bytes object.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700808
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400809 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200810
Yury Selivanov19a44f62017-12-14 20:53:26 -0500811 .. versionchanged:: 3.7
Carol Willing5b7cbd62018-09-12 17:05:17 -0700812 Even though this method was always documented as a coroutine
813 method, releases before Python 3.7 returned a :class:`Future`.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700814 Since Python 3.7 this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100815
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700816.. coroutinemethod:: loop.sock_recv_into(sock, buf)
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200817
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400818 Receive data from *sock* into the *buf* buffer. Modeled after the blocking
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700819 :meth:`socket.recv_into() <socket.socket.recv_into>` method.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200820
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700821 Return the number of bytes written to the buffer.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200822
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400823 *sock* must be a non-blocking socket.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200824
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200825 .. versionadded:: 3.7
826
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700827.. coroutinemethod:: loop.sock_sendall(sock, data)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100828
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400829 Send *data* to the *sock* socket. Asynchronous version of
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700830 :meth:`socket.sendall() <socket.socket.sendall>`.
Yury Selivanov55c50842016-06-08 12:48:15 -0400831
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400832 This method continues to send to the socket until either all data
833 in *data* has been sent or an error occurs. ``None`` is returned
Carol Willing5b7cbd62018-09-12 17:05:17 -0700834 on success. On error, an exception is raised. Additionally, there is no way
835 to determine how much data, if any, was successfully processed by the
836 receiving end of the connection.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100837
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400838 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200839
Yury Selivanov19a44f62017-12-14 20:53:26 -0500840 .. versionchanged:: 3.7
841 Even though the method was always documented as a coroutine
842 method, before Python 3.7 it returned an :class:`Future`.
843 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100844
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700845.. coroutinemethod:: loop.sock_connect(sock, address)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100846
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400847 Connect *sock* to a remote socket at *address*.
Victor Stinner1b0580b2014-02-13 09:24:37 +0100848
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700849 Asynchronous version of :meth:`socket.connect() <socket.socket.connect>`.
850
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400851 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200852
Yury Selivanov55c50842016-06-08 12:48:15 -0400853 .. versionchanged:: 3.5.2
854 ``address`` no longer needs to be resolved. ``sock_connect``
855 will try to check if the *address* is already resolved by calling
856 :func:`socket.inet_pton`. If not,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700857 :meth:`loop.getaddrinfo` will be used to resolve the
Yury Selivanov55c50842016-06-08 12:48:15 -0400858 *address*.
859
Victor Stinnerc1567df2014-02-08 23:22:58 +0100860 .. seealso::
861
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700862 :meth:`loop.create_connection`
Yury Selivanov55c50842016-06-08 12:48:15 -0400863 and :func:`asyncio.open_connection() <open_connection>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100864
865
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700866.. coroutinemethod:: loop.sock_accept(sock)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100867
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700868 Accept a connection. Modeled after the blocking
869 :meth:`socket.accept() <socket.socket.accept>` method.
Yury Selivanov55c50842016-06-08 12:48:15 -0400870
871 The socket must be bound to an address and listening
Victor Stinnerc1567df2014-02-08 23:22:58 +0100872 for connections. The return value is a pair ``(conn, address)`` where *conn*
873 is a *new* socket object usable to send and receive data on the connection,
874 and *address* is the address bound to the socket on the other end of the
875 connection.
876
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400877 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200878
Yury Selivanov19a44f62017-12-14 20:53:26 -0500879 .. versionchanged:: 3.7
880 Even though the method was always documented as a coroutine
881 method, before Python 3.7 it returned a :class:`Future`.
882 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100883
884 .. seealso::
885
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700886 :meth:`loop.create_server` and :func:`start_server`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100887
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700888.. coroutinemethod:: loop.sock_sendfile(sock, file, offset=0, count=None, \
889 \*, fallback=True)
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200890
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700891 Send a file using high-performance :mod:`os.sendfile` if possible.
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400892 Return the total number of bytes sent.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200893
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700894 Asynchronous version of :meth:`socket.sendfile() <socket.socket.sendfile>`.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200895
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400896 *sock* must be a non-blocking :const:`socket.SOCK_STREAM`
897 :class:`~socket.socket`.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200898
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400899 *file* must be a regular file object open in binary mode.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200900
901 *offset* tells from where to start reading the file. If specified,
902 *count* is the total number of bytes to transmit as opposed to
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400903 sending the file until EOF is reached. File position is always updated,
904 even when this method raises an error, and
905 :meth:`file.tell() <io.IOBase.tell>` can be used to obtain the actual
906 number of bytes sent.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200907
Carol Willing5b7cbd62018-09-12 17:05:17 -0700908 *fallback*, when set to ``True``, makes asyncio manually read and send
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200909 the file when the platform does not support the sendfile syscall
910 (e.g. Windows or SSL socket on Unix).
911
Andrew Svetlov7464e872018-01-19 20:04:29 +0200912 Raise :exc:`SendfileNotAvailableError` if the system does not support
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200913 *sendfile* syscall and *fallback* is ``False``.
914
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400915 *sock* must be a non-blocking socket.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700916
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200917 .. versionadded:: 3.7
918
Victor Stinnerc1567df2014-02-08 23:22:58 +0100919
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700920DNS
921^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100922
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700923.. coroutinemethod:: loop.getaddrinfo(host, port, \*, family=0, \
924 type=0, proto=0, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100925
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700926 Asynchronous version of :meth:`socket.getaddrinfo`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100927
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700928.. coroutinemethod:: loop.getnameinfo(sockaddr, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100929
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700930 Asynchronous version of :meth:`socket.getnameinfo`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100931
Yury Selivanovbec23722018-01-28 14:09:40 -0500932.. versionchanged:: 3.7
933 Both *getaddrinfo* and *getnameinfo* methods were always documented
934 to return a coroutine, but prior to Python 3.7 they were, in fact,
935 returning :class:`asyncio.Future` objects. Starting with Python 3.7
936 both methods are coroutines.
937
Victor Stinnerea3183f2013-12-03 01:08:00 +0100938
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700939Working with pipes
940^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100941
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700942.. coroutinemethod:: loop.connect_read_pipe(protocol_factory, pipe)
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200943
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400944 Register the read end of *pipe* in the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100945
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700946 *protocol_factory* must be a callable returning an
947 :ref:`asyncio protocol <asyncio-protocol>` implementation.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100948
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700949 *pipe* is a :term:`file-like object <file object>`.
950
951 Return pair ``(transport, protocol)``, where *transport* supports
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400952 the :class:`ReadTransport` interface and *protocol* is an object
953 instantiated by the *protocol_factory*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100954
Victor Stinnerd84fd732014-08-26 01:01:59 +0200955 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
956 non-blocking mode.
957
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700958.. coroutinemethod:: loop.connect_write_pipe(protocol_factory, pipe)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100959
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400960 Register the write end of *pipe* in the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100961
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700962 *protocol_factory* must be a callable returning an
963 :ref:`asyncio protocol <asyncio-protocol>` implementation.
964
965 *pipe* is :term:`file-like object <file object>`.
966
Victor Stinner2cef3002014-10-23 22:38:46 +0200967 Return pair ``(transport, protocol)``, where *transport* supports
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400968 :class:`WriteTransport` interface and *protocol* is an object
969 instantiated by the *protocol_factory*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100970
Victor Stinnerd84fd732014-08-26 01:01:59 +0200971 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
972 non-blocking mode.
973
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700974.. note::
975
976 :class:`SelectorEventLoop` does not support the above methods on
Carol Willing5b7cbd62018-09-12 17:05:17 -0700977 Windows. Use :class:`ProactorEventLoop` instead for Windows.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700978
Victor Stinner08444382014-02-02 22:43:39 +0100979.. seealso::
980
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700981 The :meth:`loop.subprocess_exec` and
982 :meth:`loop.subprocess_shell` methods.
Victor Stinner08444382014-02-02 22:43:39 +0100983
Victor Stinnerea3183f2013-12-03 01:08:00 +0100984
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400985Unix signals
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700986^^^^^^^^^^^^
Victor Stinner8b863482014-01-27 10:07:50 +0100987
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700988.. method:: loop.add_signal_handler(signum, callback, \*args)
Victor Stinner8b863482014-01-27 10:07:50 +0100989
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400990 Set *callback* as the handler for the *signum* signal.
Victor Stinner8b863482014-01-27 10:07:50 +0100991
Hrvoje Nikšiće3666fc2018-12-18 22:31:29 +0100992 The callback will be invoked by *loop*, along with other queued callbacks
993 and runnable coroutines of that event loop. Unlike signal handlers
994 registered using :func:`signal.signal`, a callback registered with this
995 function is allowed to interact with the event loop.
996
Victor Stinner8b863482014-01-27 10:07:50 +0100997 Raise :exc:`ValueError` if the signal number is invalid or uncatchable.
998 Raise :exc:`RuntimeError` if there is a problem setting up the handler.
999
Yury Selivanove247b462018-09-20 12:43:59 -04001000 Use :func:`functools.partial` :ref:`to pass keyword arguments
Naglis17473342018-12-04 09:31:15 +02001001 <asyncio-pass-keywords>` to *callback*.
Victor Stinner8464c242014-11-28 13:15:41 +01001002
Hrvoje Nikšiće3666fc2018-12-18 22:31:29 +01001003 Like :func:`signal.signal`, this function must be invoked in the main
1004 thread.
1005
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001006.. method:: loop.remove_signal_handler(sig)
Victor Stinner8b863482014-01-27 10:07:50 +01001007
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001008 Remove the handler for the *sig* signal.
Victor Stinner8b863482014-01-27 10:07:50 +01001009
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001010 Return ``True`` if the signal handler was removed, or ``False`` if
1011 no handler was set for the given signal.
Victor Stinner8b863482014-01-27 10:07:50 +01001012
Cheryl Sabella2d6097d2018-10-12 10:55:20 -04001013 .. availability:: Unix.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001014
Victor Stinner8b863482014-01-27 10:07:50 +01001015.. seealso::
1016
1017 The :mod:`signal` module.
1018
1019
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001020Executing code in thread or process pools
1021^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +01001022
Yury Selivanov47150392018-09-18 17:55:44 -04001023.. awaitablemethod:: loop.run_in_executor(executor, func, \*args)
Victor Stinnerea3183f2013-12-03 01:08:00 +01001024
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001025 Arrange for *func* to be called in the specified executor.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001026
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001027 The *executor* argument should be an :class:`concurrent.futures.Executor`
Larry Hastings3732ed22014-03-15 21:13:56 -07001028 instance. The default executor is used if *executor* is ``None``.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001029
Yury Selivanove247b462018-09-20 12:43:59 -04001030 Example::
1031
1032 import asyncio
1033 import concurrent.futures
1034
1035 def blocking_io():
1036 # File operations (such as logging) can block the
1037 # event loop: run them in a thread pool.
1038 with open('/dev/urandom', 'rb') as f:
1039 return f.read(100)
1040
1041 def cpu_bound():
1042 # CPU-bound operations will block the event loop:
1043 # in general it is preferable to run them in a
1044 # process pool.
1045 return sum(i * i for i in range(10 ** 7))
1046
1047 async def main():
1048 loop = asyncio.get_running_loop()
1049
1050 ## Options:
1051
1052 # 1. Run in the default loop's executor:
1053 result = await loop.run_in_executor(
1054 None, blocking_io)
1055 print('default thread pool', result)
1056
1057 # 2. Run in a custom thread pool:
1058 with concurrent.futures.ThreadPoolExecutor() as pool:
1059 result = await loop.run_in_executor(
1060 pool, blocking_io)
1061 print('custom thread pool', result)
1062
1063 # 3. Run in a custom process pool:
1064 with concurrent.futures.ProcessPoolExecutor() as pool:
1065 result = await loop.run_in_executor(
1066 pool, cpu_bound)
1067 print('custom process pool', result)
1068
1069 asyncio.run(main())
Victor Stinner8464c242014-11-28 13:15:41 +01001070
Yury Selivanovbec23722018-01-28 14:09:40 -05001071 This method returns a :class:`asyncio.Future` object.
1072
Yury Selivanove247b462018-09-20 12:43:59 -04001073 Use :func:`functools.partial` :ref:`to pass keyword arguments
1074 <asyncio-pass-keywords>` to *func*.
1075
Yury Selivanove8a60452016-10-21 17:40:42 -04001076 .. versionchanged:: 3.5.3
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001077 :meth:`loop.run_in_executor` no longer configures the
Yury Selivanove8a60452016-10-21 17:40:42 -04001078 ``max_workers`` of the thread pool executor it creates, instead
1079 leaving it up to the thread pool executor
1080 (:class:`~concurrent.futures.ThreadPoolExecutor`) to set the
1081 default.
1082
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001083.. method:: loop.set_default_executor(executor)
Victor Stinnerea3183f2013-12-03 01:08:00 +01001084
Elvis Pranskevichus22d25082018-07-30 11:42:43 +01001085 Set *executor* as the default executor used by :meth:`run_in_executor`.
1086 *executor* should be an instance of
1087 :class:`~concurrent.futures.ThreadPoolExecutor`.
1088
1089 .. deprecated:: 3.8
1090 Using an executor that is not an instance of
1091 :class:`~concurrent.futures.ThreadPoolExecutor` is deprecated and
1092 will trigger an error in Python 3.9.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001093
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001094 *executor* must be an instance of
1095 :class:`concurrent.futures.ThreadPoolExecutor`.
1096
Victor Stinnerea3183f2013-12-03 01:08:00 +01001097
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001098Error Handling API
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001099^^^^^^^^^^^^^^^^^^
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001100
Martin Panterc04fb562016-02-10 05:44:01 +00001101Allows customizing how exceptions are handled in the event loop.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001102
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001103.. method:: loop.set_exception_handler(handler)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001104
1105 Set *handler* as the new event loop exception handler.
1106
1107 If *handler* is ``None``, the default exception handler will
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001108 be set. Otherwise, *handler* must be a callable with the signature
1109 matching ``(loop, context)``, where ``loop``
1110 is a reference to the active event loop, and ``context``
1111 is a ``dict`` object containing the details of the exception
1112 (see :meth:`call_exception_handler` documentation for details
1113 about context).
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001114
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001115.. method:: loop.get_exception_handler()
Yury Selivanov950204d2016-05-16 16:23:00 -04001116
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001117 Return the current exception handler, or ``None`` if no custom
1118 exception handler was set.
Yury Selivanov950204d2016-05-16 16:23:00 -04001119
1120 .. versionadded:: 3.5.2
1121
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001122.. method:: loop.default_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001123
1124 Default exception handler.
1125
1126 This is called when an exception occurs and no exception
Carol Willing5b7cbd62018-09-12 17:05:17 -07001127 handler is set. This can be called by a custom exception
1128 handler that wants to defer to the default handler behavior.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001129
1130 *context* parameter has the same meaning as in
1131 :meth:`call_exception_handler`.
1132
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001133.. method:: loop.call_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001134
1135 Call the current event loop exception handler.
1136
1137 *context* is a ``dict`` object containing the following keys
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001138 (new keys may be introduced in future Python versions):
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001139
1140 * 'message': Error message;
1141 * 'exception' (optional): Exception object;
1142 * 'future' (optional): :class:`asyncio.Future` instance;
1143 * 'handle' (optional): :class:`asyncio.Handle` instance;
1144 * 'protocol' (optional): :ref:`Protocol <asyncio-protocol>` instance;
1145 * 'transport' (optional): :ref:`Transport <asyncio-transport>` instance;
1146 * 'socket' (optional): :class:`socket.socket` instance.
1147
1148 .. note::
1149
Carol Willing5b7cbd62018-09-12 17:05:17 -07001150 This method should not be overloaded in subclassed
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001151 event loops. For custom exception handling, use
1152 the :meth:`set_exception_handler()` method.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001153
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001154Enabling debug mode
1155^^^^^^^^^^^^^^^^^^^
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001156
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001157.. method:: loop.get_debug()
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001158
Victor Stinner7b7120e2014-06-23 00:12:14 +02001159 Get the debug mode (:class:`bool`) of the event loop.
1160
1161 The default value is ``True`` if the environment variable
1162 :envvar:`PYTHONASYNCIODEBUG` is set to a non-empty string, ``False``
1163 otherwise.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001164
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001165.. method:: loop.set_debug(enabled: bool)
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001166
1167 Set the debug mode of the event loop.
1168
Yury Selivanov805e27e2018-09-14 16:57:11 -07001169 .. versionchanged:: 3.7
1170
1171 The new ``-X dev`` command line option can now also be used
1172 to enable the debug mode.
1173
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001174.. seealso::
1175
Victor Stinner62511fd2014-06-23 00:36:11 +02001176 The :ref:`debug mode of asyncio <asyncio-debug-mode>`.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001177
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001178
1179Running Subprocesses
1180^^^^^^^^^^^^^^^^^^^^
1181
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001182Methods described in this subsections are low-level. In regular
1183async/await code consider using the high-level
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001184:func:`asyncio.create_subprocess_shell` and
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001185:func:`asyncio.create_subprocess_exec` convenience functions instead.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001186
1187.. note::
1188
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001189 The default asyncio event loop on **Windows** does not support
1190 subprocesses. See :ref:`Subprocess Support on Windows
1191 <asyncio-windows-subprocess>` for details.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001192
1193.. coroutinemethod:: loop.subprocess_exec(protocol_factory, \*args, \
1194 stdin=subprocess.PIPE, stdout=subprocess.PIPE, \
1195 stderr=subprocess.PIPE, \*\*kwargs)
1196
1197 Create a subprocess from one or more string arguments specified by
1198 *args*.
1199
1200 *args* must be a list of strings represented by:
1201
1202 * :class:`str`;
1203 * or :class:`bytes`, encoded to the
1204 :ref:`filesystem encoding <filesystem-encoding>`.
1205
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001206 The first string specifies the program executable,
Carol Willing5b7cbd62018-09-12 17:05:17 -07001207 and the remaining strings specify the arguments. Together, string
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001208 arguments form the ``argv`` of the program.
1209
1210 This is similar to the standard library :class:`subprocess.Popen`
1211 class called with ``shell=False`` and the list of strings passed as
1212 the first argument; however, where :class:`~subprocess.Popen` takes
1213 a single argument which is list of strings, *subprocess_exec*
1214 takes multiple string arguments.
1215
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001216 The *protocol_factory* must be a callable returning a subclass of the
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001217 :class:`asyncio.SubprocessProtocol` class.
1218
1219 Other parameters:
1220
sbstpf0d4c642019-05-27 19:51:19 -04001221 * *stdin* can be any of these:
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001222
sbstpf0d4c642019-05-27 19:51:19 -04001223 * a file-like object representing a pipe to be connected to the
1224 subprocess's standard input stream using
1225 :meth:`~loop.connect_write_pipe`
1226 * the :const:`subprocess.PIPE` constant (default) which will create a new
1227 pipe and connect it,
1228 * the value ``None`` which will make the subprocess inherit the file
1229 descriptor from this process
1230 * the :const:`subprocess.DEVNULL` constant which indicates that the
1231 special :data:`os.devnull` file will be used
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001232
sbstpf0d4c642019-05-27 19:51:19 -04001233 * *stdout* can be any of these:
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001234
sbstpf0d4c642019-05-27 19:51:19 -04001235 * a file-like object representing a pipe to be connected to the
1236 subprocess's standard output stream using
1237 :meth:`~loop.connect_write_pipe`
1238 * the :const:`subprocess.PIPE` constant (default) which will create a new
1239 pipe and connect it,
1240 * the value ``None`` which will make the subprocess inherit the file
1241 descriptor from this process
1242 * the :const:`subprocess.DEVNULL` constant which indicates that the
1243 special :data:`os.devnull` file will be used
1244
1245 * *stderr* can be any of these:
1246
1247 * a file-like object representing a pipe to be connected to the
1248 subprocess's standard error stream using
1249 :meth:`~loop.connect_write_pipe`
1250 * the :const:`subprocess.PIPE` constant (default) which will create a new
1251 pipe and connect it,
1252 * the value ``None`` which will make the subprocess inherit the file
1253 descriptor from this process
1254 * the :const:`subprocess.DEVNULL` constant which indicates that the
1255 special :data:`os.devnull` file will be used
1256 * the :const:`subprocess.STDOUT` constant which will connect the standard
1257 error stream to the process' standard output stream
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001258
1259 * All other keyword arguments are passed to :class:`subprocess.Popen`
sbstpf0d4c642019-05-27 19:51:19 -04001260 without interpretation, except for *bufsize*, *universal_newlines*,
1261 *shell*, *text*, *encoding* and *errors*, which should not be specified
1262 at all.
1263
1264 The ``asyncio`` subprocess API does not support decoding the streams
1265 as text. :func:`bytes.decode` can be used to convert the bytes returned
1266 from the stream to text.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001267
1268 See the constructor of the :class:`subprocess.Popen` class
1269 for documentation on other arguments.
1270
1271 Returns a pair of ``(transport, protocol)``, where *transport*
Bumsik Kim5cc583d2018-09-16 19:40:44 -04001272 conforms to the :class:`asyncio.SubprocessTransport` base class and
1273 *protocol* is an object instantiated by the *protocol_factory*.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001274
1275.. coroutinemethod:: loop.subprocess_shell(protocol_factory, cmd, \*, \
1276 stdin=subprocess.PIPE, stdout=subprocess.PIPE, \
1277 stderr=subprocess.PIPE, \*\*kwargs)
1278
1279 Create a subprocess from *cmd*, which can be a :class:`str` or a
1280 :class:`bytes` string encoded to the
1281 :ref:`filesystem encoding <filesystem-encoding>`,
1282 using the platform's "shell" syntax.
1283
1284 This is similar to the standard library :class:`subprocess.Popen`
1285 class called with ``shell=True``.
1286
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001287 The *protocol_factory* must be a callable returning a subclass of the
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001288 :class:`SubprocessProtocol` class.
1289
1290 See :meth:`~loop.subprocess_exec` for more details about
1291 the remaining arguments.
1292
1293 Returns a pair of ``(transport, protocol)``, where *transport*
Bumsik Kim5cc583d2018-09-16 19:40:44 -04001294 conforms to the :class:`SubprocessTransport` base class and
1295 *protocol* is an object instantiated by the *protocol_factory*.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001296
1297.. note::
1298 It is the application's responsibility to ensure that all whitespace
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001299 and special characters are quoted appropriately to avoid `shell injection
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001300 <https://en.wikipedia.org/wiki/Shell_injection#Shell_injection>`_
1301 vulnerabilities. The :func:`shlex.quote` function can be used to
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001302 properly escape whitespace and special characters in strings that
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001303 are going to be used to construct shell commands.
1304
1305
1306Callback Handles
1307================
1308
1309.. class:: Handle
1310
1311 A callback wrapper object returned by :meth:`loop.call_soon`,
1312 :meth:`loop.call_soon_threadsafe`.
1313
1314 .. method:: cancel()
1315
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001316 Cancel the callback. If the callback has already been canceled
1317 or executed, this method has no effect.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001318
1319 .. method:: cancelled()
1320
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001321 Return ``True`` if the callback was cancelled.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001322
1323 .. versionadded:: 3.7
1324
1325.. class:: TimerHandle
1326
1327 A callback wrapper object returned by :meth:`loop.call_later`,
1328 and :meth:`loop.call_at`.
1329
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001330 This class is a subclass of :class:`Handle`.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001331
1332 .. method:: when()
1333
1334 Return a scheduled callback time as :class:`float` seconds.
1335
1336 The time is an absolute timestamp, using the same time
1337 reference as :meth:`loop.time`.
1338
1339 .. versionadded:: 3.7
1340
1341
1342Server Objects
1343==============
1344
1345Server objects are created by :meth:`loop.create_server`,
1346:meth:`loop.create_unix_server`, :func:`start_server`,
1347and :func:`start_unix_server` functions.
1348
1349Do not instantiate the class directly.
Victor Stinner8c462c52014-01-24 18:11:43 +01001350
Victor Stinner8ebeb032014-07-11 23:47:40 +02001351.. class:: Server
Victor Stinner8c462c52014-01-24 18:11:43 +01001352
Yury Selivanovc9070d02018-01-25 18:08:09 -05001353 *Server* objects are asynchronous context managers. When used in an
1354 ``async with`` statement, it's guaranteed that the Server object is
1355 closed and not accepting new connections when the ``async with``
1356 statement is completed::
1357
1358 srv = await loop.create_server(...)
1359
1360 async with srv:
1361 # some code
1362
Carol Willing5b7cbd62018-09-12 17:05:17 -07001363 # At this point, srv is closed and no longer accepts new connections.
Yury Selivanovc9070d02018-01-25 18:08:09 -05001364
1365
1366 .. versionchanged:: 3.7
1367 Server object is an asynchronous context manager since Python 3.7.
Victor Stinner8c462c52014-01-24 18:11:43 +01001368
1369 .. method:: close()
1370
Victor Stinner4bfb14a2014-07-12 03:20:24 +02001371 Stop serving: close listening sockets and set the :attr:`sockets`
1372 attribute to ``None``.
1373
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001374 The sockets that represent existing incoming client connections
1375 are left open.
Victor Stinner8ebeb032014-07-11 23:47:40 +02001376
Berker Peksag49c9edf2016-01-20 07:14:22 +02001377 The server is closed asynchronously, use the :meth:`wait_closed`
1378 coroutine to wait until the server is closed.
Victor Stinner8c462c52014-01-24 18:11:43 +01001379
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)1634fc22017-12-30 20:39:32 +05301380 .. method:: get_loop()
1381
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001382 Return the event loop associated with the server object.
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)1634fc22017-12-30 20:39:32 +05301383
1384 .. versionadded:: 3.7
1385
Yury Selivanovc9070d02018-01-25 18:08:09 -05001386 .. coroutinemethod:: start_serving()
1387
1388 Start accepting connections.
1389
1390 This method is idempotent, so it can be called when
1391 the server is already being serving.
1392
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001393 The *start_serving* keyword-only parameter to
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001394 :meth:`loop.create_server` and
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001395 :meth:`asyncio.start_server` allows creating a Server object
1396 that is not accepting connections initially. In this case
1397 ``Server.start_serving()``, or :meth:`Server.serve_forever` can be used
1398 to make the Server start accepting connections.
Yury Selivanovc9070d02018-01-25 18:08:09 -05001399
1400 .. versionadded:: 3.7
1401
1402 .. coroutinemethod:: serve_forever()
1403
1404 Start accepting connections until the coroutine is cancelled.
1405 Cancellation of ``serve_forever`` task causes the server
1406 to be closed.
1407
1408 This method can be called if the server is already accepting
1409 connections. Only one ``serve_forever`` task can exist per
1410 one *Server* object.
1411
1412 Example::
1413
1414 async def client_connected(reader, writer):
1415 # Communicate with the client with
1416 # reader/writer streams. For example:
1417 await reader.readline()
1418
1419 async def main(host, port):
1420 srv = await asyncio.start_server(
1421 client_connected, host, port)
Andrew Svetlov17ab8f02018-02-17 19:44:35 +02001422 await srv.serve_forever()
Yury Selivanovc9070d02018-01-25 18:08:09 -05001423
1424 asyncio.run(main('127.0.0.1', 0))
1425
1426 .. versionadded:: 3.7
1427
1428 .. method:: is_serving()
1429
1430 Return ``True`` if the server is accepting new connections.
1431
1432 .. versionadded:: 3.7
1433
Victor Stinnerbdd574d2015-02-12 22:49:18 +01001434 .. coroutinemethod:: wait_closed()
Victor Stinner8c462c52014-01-24 18:11:43 +01001435
Victor Stinner8ebeb032014-07-11 23:47:40 +02001436 Wait until the :meth:`close` method completes.
1437
Victor Stinner8ebeb032014-07-11 23:47:40 +02001438 .. attribute:: sockets
1439
Emmanuel Ariasdf5cdc12019-02-22 14:34:41 -03001440 List of :class:`socket.socket` objects the server is listening on.
Victor Stinner8c462c52014-01-24 18:11:43 +01001441
Yury Selivanovc9070d02018-01-25 18:08:09 -05001442 .. versionchanged:: 3.7
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001443 Prior to Python 3.7 ``Server.sockets`` used to return an
1444 internal list of server sockets directly. In 3.7 a copy
Yury Selivanovc9070d02018-01-25 18:08:09 -05001445 of that list is returned.
1446
Victor Stinner8c462c52014-01-24 18:11:43 +01001447
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001448.. _asyncio-event-loops:
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001449
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001450Event Loop Implementations
1451==========================
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001452
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001453asyncio ships with two different event loop implementations:
1454:class:`SelectorEventLoop` and :class:`ProactorEventLoop`.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001455
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001456By default asyncio is configured to use :class:`SelectorEventLoop`
Ben Darnell9ffca672019-06-22 13:38:21 -04001457on Unix and :class:`ProactorEventLoop` on Windows.
Andrew Svetlov3d4dbd82018-02-01 19:59:32 +02001458
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001459
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001460.. class:: SelectorEventLoop
1461
1462 An event loop based on the :mod:`selectors` module.
1463
1464 Uses the most efficient *selector* available for the given
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001465 platform. It is also possible to manually configure the
1466 exact selector implementation to be used::
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001467
1468 import asyncio
1469 import selectors
1470
1471 selector = selectors.SelectSelector()
1472 loop = asyncio.SelectorEventLoop(selector)
1473 asyncio.set_event_loop(loop)
Andrew Svetlov7464e872018-01-19 20:04:29 +02001474
1475
Cheryl Sabella2d6097d2018-10-12 10:55:20 -04001476 .. availability:: Unix, Windows.
Andrew Svetlov7464e872018-01-19 20:04:29 +02001477
1478
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001479.. class:: ProactorEventLoop
1480
1481 An event loop for Windows that uses "I/O Completion Ports" (IOCP).
1482
Cheryl Sabella2d6097d2018-10-12 10:55:20 -04001483 .. availability:: Windows.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001484
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001485 .. seealso::
1486
1487 `MSDN documentation on I/O Completion Ports
1488 <https://docs.microsoft.com/en-ca/windows/desktop/FileIO/i-o-completion-ports>`_.
1489
1490
1491.. class:: AbstractEventLoop
1492
1493 Abstract base class for asyncio-compliant event loops.
1494
1495 The :ref:`Event Loop Methods <asyncio-event-loop>` section lists all
1496 methods that an alternative implementation of ``AbstractEventLoop``
1497 should have defined.
1498
1499
1500Examples
1501========
1502
1503Note that all examples in this section **purposefully** show how
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001504to use the low-level event loop APIs, such as :meth:`loop.run_forever`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001505and :meth:`loop.call_soon`. Modern asyncio applications rarely
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001506need to be written this way; consider using the high-level functions
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001507like :func:`asyncio.run`.
1508
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001509
Yury Selivanov394374e2018-09-17 15:35:24 -04001510.. _asyncio_example_lowlevel_helloworld:
Victor Stinnerea3183f2013-12-03 01:08:00 +01001511
Victor Stinner7f314ed2014-10-15 18:49:16 +02001512Hello World with call_soon()
Victor Stinnera092a612015-01-09 15:58:41 +01001513^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +01001514
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001515An example using the :meth:`loop.call_soon` method to schedule a
1516callback. The callback displays ``"Hello World"`` and then stops the
1517event loop::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001518
1519 import asyncio
1520
Victor Stinner7f314ed2014-10-15 18:49:16 +02001521 def hello_world(loop):
Carol Willing5b7cbd62018-09-12 17:05:17 -07001522 """A callback to print 'Hello World' and stop the event loop"""
Victor Stinnerea3183f2013-12-03 01:08:00 +01001523 print('Hello World')
Victor Stinner7f314ed2014-10-15 18:49:16 +02001524 loop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001525
1526 loop = asyncio.get_event_loop()
Victor Stinner7f314ed2014-10-15 18:49:16 +02001527
1528 # Schedule a call to hello_world()
1529 loop.call_soon(hello_world, loop)
1530
1531 # Blocking call interrupted by loop.stop()
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001532 try:
1533 loop.run_forever()
1534 finally:
1535 loop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001536
Victor Stinner3e09e322013-12-03 01:22:06 +01001537.. seealso::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001538
Yury Selivanov3faaa882018-09-14 13:32:07 -07001539 A similar :ref:`Hello World <coroutine>`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001540 example created with a coroutine and the :func:`run` function.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001541
Victor Stinner8b863482014-01-27 10:07:50 +01001542
Yury Selivanov394374e2018-09-17 15:35:24 -04001543.. _asyncio_example_call_later:
Victor Stinner7f314ed2014-10-15 18:49:16 +02001544
1545Display the current date with call_later()
Victor Stinnera092a612015-01-09 15:58:41 +01001546^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner7f314ed2014-10-15 18:49:16 +02001547
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001548An example of a callback displaying the current date every second. The
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001549callback uses the :meth:`loop.call_later` method to reschedule itself
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001550after 5 seconds, and then stops the event loop::
Victor Stinner7f314ed2014-10-15 18:49:16 +02001551
1552 import asyncio
1553 import datetime
1554
1555 def display_date(end_time, loop):
1556 print(datetime.datetime.now())
1557 if (loop.time() + 1.0) < end_time:
1558 loop.call_later(1, display_date, end_time, loop)
1559 else:
1560 loop.stop()
1561
1562 loop = asyncio.get_event_loop()
1563
1564 # Schedule the first call to display_date()
1565 end_time = loop.time() + 5.0
1566 loop.call_soon(display_date, end_time, loop)
1567
1568 # Blocking call interrupted by loop.stop()
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001569 try:
1570 loop.run_forever()
1571 finally:
1572 loop.close()
Victor Stinner7f314ed2014-10-15 18:49:16 +02001573
1574.. seealso::
1575
Yury Selivanov7372c3b2018-09-14 15:11:24 -07001576 A similar :ref:`current date <asyncio_example_sleep>` example
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001577 created with a coroutine and the :func:`run` function.
Victor Stinner7f314ed2014-10-15 18:49:16 +02001578
1579
Yury Selivanov394374e2018-09-17 15:35:24 -04001580.. _asyncio_example_watch_fd:
Victor Stinner8b863482014-01-27 10:07:50 +01001581
Victor Stinner04e6df32014-10-11 16:16:27 +02001582Watch a file descriptor for read events
Victor Stinnera092a612015-01-09 15:58:41 +01001583^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001584
1585Wait until a file descriptor received some data using the
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001586:meth:`loop.add_reader` method and then close the event loop::
Victor Stinner04e6df32014-10-11 16:16:27 +02001587
1588 import asyncio
Victor Stinnerac577d72017-11-28 21:33:20 +01001589 from socket import socketpair
Victor Stinner04e6df32014-10-11 16:16:27 +02001590
1591 # Create a pair of connected file descriptors
Victor Stinnerccd8e342014-10-11 16:30:02 +02001592 rsock, wsock = socketpair()
Carol Willing5b7cbd62018-09-12 17:05:17 -07001593
Victor Stinner04e6df32014-10-11 16:16:27 +02001594 loop = asyncio.get_event_loop()
1595
1596 def reader():
1597 data = rsock.recv(100)
1598 print("Received:", data.decode())
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001599
Victor Stinner2cef3002014-10-23 22:38:46 +02001600 # We are done: unregister the file descriptor
Victor Stinner04e6df32014-10-11 16:16:27 +02001601 loop.remove_reader(rsock)
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001602
Victor Stinner04e6df32014-10-11 16:16:27 +02001603 # Stop the event loop
1604 loop.stop()
1605
Victor Stinner2cef3002014-10-23 22:38:46 +02001606 # Register the file descriptor for read event
Victor Stinner04e6df32014-10-11 16:16:27 +02001607 loop.add_reader(rsock, reader)
1608
1609 # Simulate the reception of data from the network
1610 loop.call_soon(wsock.send, 'abc'.encode())
1611
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001612 try:
1613 # Run the event loop
1614 loop.run_forever()
1615 finally:
Carol Willing5b7cbd62018-09-12 17:05:17 -07001616 # We are done. Close sockets and the event loop.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001617 rsock.close()
1618 wsock.close()
1619 loop.close()
Victor Stinner04e6df32014-10-11 16:16:27 +02001620
1621.. seealso::
1622
Yury Selivanov394374e2018-09-17 15:35:24 -04001623 * A similar :ref:`example <asyncio_example_create_connection>`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001624 using transports, protocols, and the
1625 :meth:`loop.create_connection` method.
Victor Stinner04e6df32014-10-11 16:16:27 +02001626
Yury Selivanov394374e2018-09-17 15:35:24 -04001627 * Another similar :ref:`example <asyncio_example_create_connection-streams>`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001628 using the high-level :func:`asyncio.open_connection` function
1629 and streams.
Victor Stinner04e6df32014-10-11 16:16:27 +02001630
1631
Yury Selivanov394374e2018-09-17 15:35:24 -04001632.. _asyncio_example_unix_signals:
1633
Victor Stinner04e6df32014-10-11 16:16:27 +02001634Set signal handlers for SIGINT and SIGTERM
Victor Stinnera092a612015-01-09 15:58:41 +01001635^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001636
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001637(This ``signals`` example only works on Unix.)
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001638
1639Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM`
1640using the :meth:`loop.add_signal_handler` method::
Victor Stinner8b863482014-01-27 10:07:50 +01001641
1642 import asyncio
1643 import functools
1644 import os
1645 import signal
1646
Alexander Vasinceb842e2019-05-03 18:25:36 +03001647 def ask_exit(signame, loop):
Victor Stinner8b863482014-01-27 10:07:50 +01001648 print("got signal %s: exit" % signame)
1649 loop.stop()
1650
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001651 async def main():
1652 loop = asyncio.get_running_loop()
Victor Stinner8b863482014-01-27 10:07:50 +01001653
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001654 for signame in {'SIGINT', 'SIGTERM'}:
1655 loop.add_signal_handler(
1656 getattr(signal, signame),
Alexander Vasinceb842e2019-05-03 18:25:36 +03001657 functools.partial(ask_exit, signame, loop))
Victor Stinner2cef3002014-10-23 22:38:46 +02001658
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001659 await asyncio.sleep(3600)
1660
1661 print("Event loop running for 1 hour, press Ctrl+C to interrupt.")
1662 print(f"pid {os.getpid()}: send SIGINT or SIGTERM to exit.")
1663
1664 asyncio.run(main())