blob: 045787e0ce787551abdadcd6ae47fa55be7df449 [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
Kyle Stanley9fdc64c2019-09-19 08:47:22 -0400170.. coroutinemethod:: loop.shutdown_default_executor()
171
172 Schedule the closure of the default executor and wait for it to join all of
173 the threads in the :class:`ThreadPoolExecutor`. After calling this method, a
174 :exc:`RuntimeError` will be raised if :meth:`loop.run_in_executor` is called
175 while using the default executor.
176
177 Note that there is no need to call this function when
178 :func:`asyncio.run` is used.
179
180 .. versionadded:: 3.9
181
Yury Selivanov03660042016-12-15 17:36:05 -0500182
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700183Scheduling callbacks
184^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100185
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700186.. method:: loop.call_soon(callback, *args, context=None)
Victor Stinner8464c242014-11-28 13:15:41 +0100187
Carol Willing5b7cbd62018-09-12 17:05:17 -0700188 Schedule a *callback* to be called with *args* arguments at
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700189 the next iteration of the event loop.
Victor Stinner8464c242014-11-28 13:15:41 +0100190
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700191 Callbacks are called in the order in which they are registered.
192 Each callback will be called exactly once.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100193
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700194 An optional keyword-only *context* argument allows specifying a
195 custom :class:`contextvars.Context` for the *callback* to run in.
196 The current context is used when no *context* is provided.
Yury Selivanov28b91782018-05-23 13:35:04 -0400197
Yury Selivanov1096f762015-06-25 13:49:52 -0400198 An instance of :class:`asyncio.Handle` is returned, which can be
Carol Willing5b7cbd62018-09-12 17:05:17 -0700199 used later to cancel the callback.
200
201 This method is not thread-safe.
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500202
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700203.. method:: loop.call_soon_threadsafe(callback, *args, context=None)
Victor Stinner8464c242014-11-28 13:15:41 +0100204
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700205 A thread-safe variant of :meth:`call_soon`. Must be used to
206 schedule callbacks *from another thread*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100207
Victor Stinner83704962015-02-25 14:24:15 +0100208 See the :ref:`concurrency and multithreading <asyncio-multithreading>`
209 section of the documentation.
210
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700211.. versionchanged:: 3.7
212 The *context* keyword-only parameter was added. See :pep:`567`
213 for more details.
214
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400215.. _asyncio-pass-keywords:
216
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700217.. note::
218
Carol Willing5b7cbd62018-09-12 17:05:17 -0700219 Most :mod:`asyncio` scheduling functions don't allow passing
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400220 keyword arguments. To do that, use :func:`functools.partial`::
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700221
Carol Willing5b7cbd62018-09-12 17:05:17 -0700222 # will schedule "print("Hello", flush=True)"
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700223 loop.call_soon(
224 functools.partial(print, "Hello", flush=True))
225
226 Using partial objects is usually more convenient than using lambdas,
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400227 as asyncio can render partial objects better in debug and error
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700228 messages.
Yury Selivanov28b91782018-05-23 13:35:04 -0400229
Victor Stinnerea3183f2013-12-03 01:08:00 +0100230
Victor Stinner45b27ed2014-02-01 02:36:43 +0100231.. _asyncio-delayed-calls:
232
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700233Scheduling delayed callbacks
234^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100235
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700236Event loop provides mechanisms to schedule callback functions
237to be called at some point in the future. Event loop uses monotonic
238clocks to track time.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100239
Victor Stinner45b27ed2014-02-01 02:36:43 +0100240
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700241.. method:: loop.call_later(delay, callback, *args, context=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100242
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700243 Schedule *callback* to be called after the given *delay*
244 number of seconds (can be either an int or a float).
Victor Stinnerea3183f2013-12-03 01:08:00 +0100245
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700246 An instance of :class:`asyncio.TimerHandle` is returned which can
247 be used to cancel the callback.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100248
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700249 *callback* will be called exactly once. If two callbacks are
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400250 scheduled for exactly the same time, the order in which they
251 are called is undefined.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100252
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700253 The optional positional *args* will be passed to the callback when
254 it is called. If you want the callback to be called with keyword
255 arguments use :func:`functools.partial`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100256
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700257 An optional keyword-only *context* argument allows specifying a
258 custom :class:`contextvars.Context` for the *callback* to run in.
259 The current context is used when no *context* is provided.
Victor Stinner8464c242014-11-28 13:15:41 +0100260
Yury Selivanov28b91782018-05-23 13:35:04 -0400261 .. versionchanged:: 3.7
262 The *context* keyword-only parameter was added. See :pep:`567`
263 for more details.
264
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400265 .. versionchanged:: 3.8
266 In Python 3.7 and earlier with the default event loop implementation,
267 the *delay* could not exceed one day.
268 This has been fixed in Python 3.8.
269
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700270.. method:: loop.call_at(when, callback, *args, context=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100271
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700272 Schedule *callback* to be called at the given absolute timestamp
273 *when* (an int or a float), using the same time reference as
274 :meth:`loop.time`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100275
276 This method's behavior is the same as :meth:`call_later`.
277
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700278 An instance of :class:`asyncio.TimerHandle` is returned which can
279 be used to cancel the callback.
Victor Stinner8464c242014-11-28 13:15:41 +0100280
Yury Selivanov28b91782018-05-23 13:35:04 -0400281 .. versionchanged:: 3.7
282 The *context* keyword-only parameter was added. See :pep:`567`
283 for more details.
284
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400285 .. versionchanged:: 3.8
286 In Python 3.7 and earlier with the default event loop implementation,
287 the difference between *when* and the current time could not exceed
288 one day. This has been fixed in Python 3.8.
289
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700290.. method:: loop.time()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100291
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700292 Return the current time, as a :class:`float` value, according to
293 the event loop's internal monotonic clock.
294
295.. note::
Enrico Alarico Carbognani7e954e72019-04-18 14:43:14 +0200296 .. versionchanged:: 3.8
297 In Python 3.7 and earlier timeouts (relative *delay* or absolute *when*)
298 should not exceed one day. This has been fixed in Python 3.8.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100299
Victor Stinner3e09e322013-12-03 01:22:06 +0100300.. seealso::
301
302 The :func:`asyncio.sleep` function.
303
Victor Stinnerea3183f2013-12-03 01:08:00 +0100304
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700305Creating Futures and Tasks
306^^^^^^^^^^^^^^^^^^^^^^^^^^
Yury Selivanov950204d2016-05-16 16:23:00 -0400307
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700308.. method:: loop.create_future()
Yury Selivanov950204d2016-05-16 16:23:00 -0400309
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700310 Create an :class:`asyncio.Future` object attached to the event loop.
Yury Selivanov950204d2016-05-16 16:23:00 -0400311
Carol Willing5b7cbd62018-09-12 17:05:17 -0700312 This is the preferred way to create Futures in asyncio. This lets
313 third-party event loops provide alternative implementations of
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700314 the Future object (with better performance or instrumentation).
Yury Selivanov950204d2016-05-16 16:23:00 -0400315
316 .. versionadded:: 3.5.2
317
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700318.. method:: loop.create_task(coro, \*, name=None)
Yury Selivanov950204d2016-05-16 16:23:00 -0400319
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700320 Schedule the execution of a :ref:`coroutine`.
321 Return a :class:`Task` object.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200322
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700323 Third-party event loops can use their own subclass of :class:`Task`
324 for interoperability. In this case, the result type is a subclass
325 of :class:`Task`.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200326
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700327 If the *name* argument is provided and not ``None``, it is set as
328 the name of the task using :meth:`Task.set_name`.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200329
Alex Grönholmcca4eec2018-08-09 00:06:47 +0300330 .. versionchanged:: 3.8
331 Added the ``name`` parameter.
332
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700333.. method:: loop.set_task_factory(factory)
Yury Selivanov71854612015-05-11 16:28:27 -0400334
335 Set a task factory that will be used by
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700336 :meth:`loop.create_task`.
Yury Selivanov71854612015-05-11 16:28:27 -0400337
338 If *factory* is ``None`` the default task factory will be set.
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400339 Otherwise, *factory* must be a *callable* with the signature matching
340 ``(loop, coro)``, where *loop* is a reference to the active
341 event loop, and *coro* is a coroutine object. The callable
342 must return a :class:`asyncio.Future`-compatible object.
Yury Selivanov71854612015-05-11 16:28:27 -0400343
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700344.. method:: loop.get_task_factory()
Yury Selivanov71854612015-05-11 16:28:27 -0400345
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700346 Return a task factory or ``None`` if the default one is in use.
Yury Selivanov71854612015-05-11 16:28:27 -0400347
Victor Stinner530ef2f2014-07-08 12:39:10 +0200348
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700349Opening network connections
350^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100351
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700352.. coroutinemethod:: loop.create_connection(protocol_factory, \
353 host=None, port=None, \*, ssl=None, \
354 family=0, proto=0, flags=0, sock=None, \
355 local_addr=None, server_hostname=None, \
356 ssl_handshake_timeout=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100357
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700358 Open a streaming transport connection to a given
359 address specified by *host* and *port*.
360
361 The socket family can be either :py:data:`~socket.AF_INET` or
362 :py:data:`~socket.AF_INET6` depending on *host* (or the *family*
363 argument, if provided).
364
365 The socket type will be :py:data:`~socket.SOCK_STREAM`.
366
367 *protocol_factory* must be a callable returning an
368 :ref:`asyncio protocol <asyncio-protocol>` implementation.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100369
Yury Selivanov19a44f62017-12-14 20:53:26 -0500370 This method will try to establish the connection in the background.
371 When successful, it returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100372
373 The chronological synopsis of the underlying operation is as follows:
374
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700375 #. The connection is established and a :ref:`transport <asyncio-transport>`
376 is created for it.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100377
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700378 #. *protocol_factory* is called without arguments and is expected to
379 return a :ref:`protocol <asyncio-protocol>` instance.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100380
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700381 #. The protocol instance is coupled with the transport by calling its
382 :meth:`~BaseProtocol.connection_made` method.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100383
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700384 #. A ``(transport, protocol)`` tuple is returned on success.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100385
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700386 The created transport is an implementation-dependent bidirectional
387 stream.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100388
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700389 Other arguments:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100390
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400391 * *ssl*: if given and not false, a SSL/TLS transport is created
Victor Stinnerea3183f2013-12-03 01:08:00 +0100392 (by default a plain TCP transport is created). If *ssl* is
393 a :class:`ssl.SSLContext` object, this context is used to create
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400394 the transport; if *ssl* is :const:`True`, a default context returned
395 from :func:`ssl.create_default_context` is used.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100396
Berker Peksag9c1dba22014-09-28 00:00:58 +0300397 .. seealso:: :ref:`SSL/TLS security considerations <ssl-security>`
Antoine Pitrouc5e075f2014-03-22 18:19:11 +0100398
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400399 * *server_hostname* sets or overrides the hostname that the target
400 server's certificate will be matched against. Should only be passed
401 if *ssl* is not ``None``. By default the value of the *host* argument
Victor Stinnerea3183f2013-12-03 01:08:00 +0100402 is used. If *host* is empty, there is no default and you must pass a
403 value for *server_hostname*. If *server_hostname* is an empty
404 string, hostname matching is disabled (which is a serious security
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400405 risk, allowing for potential man-in-the-middle attacks).
Victor Stinnerea3183f2013-12-03 01:08:00 +0100406
407 * *family*, *proto*, *flags* are the optional address family, protocol
408 and flags to be passed through to getaddrinfo() for *host* resolution.
409 If given, these should all be integers from the corresponding
410 :mod:`socket` module constants.
411
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800412 * *happy_eyeballs_delay*, if given, enables Happy Eyeballs for this
413 connection. It should
414 be a floating-point number representing the amount of time in seconds
415 to wait for a connection attempt to complete, before starting the next
416 attempt in parallel. This is the "Connection Attempt Delay" as defined
417 in :rfc:`8305`. A sensible default value recommended by the RFC is ``0.25``
418 (250 milliseconds).
419
420 * *interleave* controls address reordering when a host name resolves to
421 multiple IP addresses.
422 If ``0`` or unspecified, no reordering is done, and addresses are
423 tried in the order returned by :meth:`getaddrinfo`. If a positive integer
424 is specified, the addresses are interleaved by address family, and the
425 given integer is interpreted as "First Address Family Count" as defined
426 in :rfc:`8305`. The default is ``0`` if *happy_eyeballs_delay* is not
427 specified, and ``1`` if it is.
428
Victor Stinnerea3183f2013-12-03 01:08:00 +0100429 * *sock*, if given, should be an existing, already connected
430 :class:`socket.socket` object to be used by the transport.
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800431 If *sock* is given, none of *host*, *port*, *family*, *proto*, *flags*,
432 *happy_eyeballs_delay*, *interleave*
Victor Stinnerea3183f2013-12-03 01:08:00 +0100433 and *local_addr* should be specified.
434
435 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
436 to bind the socket to locally. The *local_host* and *local_port*
Carol Willing5b7cbd62018-09-12 17:05:17 -0700437 are looked up using ``getaddrinfo()``, similarly to *host* and *port*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100438
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400439 * *ssl_handshake_timeout* is (for a TLS connection) the time in seconds
440 to wait for the TLS handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400441 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000442
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800443 .. versionadded:: 3.8
444
445 The *happy_eyeballs_delay* and *interleave* parameters.
446
Neil Aspinallf7686c12017-12-19 19:45:42 +0000447 .. versionadded:: 3.7
448
449 The *ssl_handshake_timeout* parameter.
450
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700451 .. versionchanged:: 3.6
452
453 The socket option :py:data:`~socket.TCP_NODELAY` is set by default
454 for all TCP connections.
455
Victor Stinner60208a12015-09-15 22:41:52 +0200456 .. versionchanged:: 3.5
457
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400458 Added support for SSL/TLS in :class:`ProactorEventLoop`.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200459
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100460 .. seealso::
461
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700462 The :func:`open_connection` function is a high-level alternative
463 API. It returns a pair of (:class:`StreamReader`, :class:`StreamWriter`)
464 that can be used directly in async/await code.
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100465
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700466.. coroutinemethod:: loop.create_datagram_endpoint(protocol_factory, \
467 local_addr=None, remote_addr=None, \*, \
468 family=0, proto=0, flags=0, \
469 reuse_address=None, reuse_port=None, \
470 allow_broadcast=None, sock=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100471
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700472 Create a datagram connection.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100473
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700474 The socket family can be either :py:data:`~socket.AF_INET`,
475 :py:data:`~socket.AF_INET6`, or :py:data:`~socket.AF_UNIX`,
476 depending on *host* (or the *family* argument, if provided).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100477
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700478 The socket type will be :py:data:`~socket.SOCK_DGRAM`.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100479
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700480 *protocol_factory* must be a callable returning a
481 :ref:`protocol <asyncio-protocol>` implementation.
482
483 A tuple of ``(transport, protocol)`` is returned on success.
484
485 Other arguments:
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700486
487 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
488 to bind the socket to locally. The *local_host* and *local_port*
489 are looked up using :meth:`getaddrinfo`.
490
491 * *remote_addr*, if given, is a ``(remote_host, remote_port)`` tuple used
492 to connect the socket to a remote address. The *remote_host* and
493 *remote_port* are looked up using :meth:`getaddrinfo`.
494
495 * *family*, *proto*, *flags* are the optional address family, protocol
496 and flags to be passed through to :meth:`getaddrinfo` for *host*
497 resolution. If given, these should all be integers from the
498 corresponding :mod:`socket` module constants.
499
500 * *reuse_address* tells the kernel to reuse a local socket in
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700501 ``TIME_WAIT`` state, without waiting for its natural timeout to
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300502 expire. If not specified will automatically be set to ``True`` on
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400503 Unix.
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700504
505 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
506 same port as other existing endpoints are bound to, so long as they all
507 set this flag when being created. This option is not supported on Windows
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400508 and some Unixes. If the :py:data:`~socket.SO_REUSEPORT` constant is not
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700509 defined then this capability is unsupported.
510
511 * *allow_broadcast* tells the kernel to allow this endpoint to send
512 messages to the broadcast address.
513
514 * *sock* can optionally be specified in order to use a preexisting,
515 already connected, :class:`socket.socket` object to be used by the
516 transport. If specified, *local_addr* and *remote_addr* should be omitted
517 (must be :const:`None`).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100518
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200519 See :ref:`UDP echo client protocol <asyncio-udp-echo-client-protocol>` and
520 :ref:`UDP echo server protocol <asyncio-udp-echo-server-protocol>` examples.
521
Romuald Brunet0ded5802018-05-14 18:22:00 +0200522 .. versionchanged:: 3.4.4
523 The *family*, *proto*, *flags*, *reuse_address*, *reuse_port,
524 *allow_broadcast*, and *sock* parameters were added.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100525
Andrew Svetlovbafd4b52019-05-28 12:52:15 +0300526 .. versionchanged:: 3.8
527 Added support for Windows.
528
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700529.. coroutinemethod:: loop.create_unix_connection(protocol_factory, \
530 path=None, \*, ssl=None, sock=None, \
531 server_hostname=None, ssl_handshake_timeout=None)
Victor Stinnera6919aa2014-02-19 13:32:34 +0100532
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400533 Create a Unix connection.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100534
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700535 The socket family will be :py:data:`~socket.AF_UNIX`; socket
536 type will be :py:data:`~socket.SOCK_STREAM`.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100537
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700538 A tuple of ``(transport, protocol)`` is returned on success.
Guido van Rossum9e80eeb2016-11-03 14:17:25 -0700539
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400540 *path* is the name of a Unix domain socket and is required,
541 unless a *sock* parameter is specified. Abstract Unix sockets,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700542 :class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths are
543 supported.
544
545 See the documentation of the :meth:`loop.create_connection` method
546 for information about arguments to this method.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100547
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400548 .. availability:: Unix.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100549
Neil Aspinallf7686c12017-12-19 19:45:42 +0000550 .. versionadded:: 3.7
551
552 The *ssl_handshake_timeout* parameter.
553
Yury Selivanov423fd362017-11-20 17:26:28 -0500554 .. versionchanged:: 3.7
555
Elvis Pranskevichusc0d062f2018-06-08 11:36:00 -0400556 The *path* parameter can now be a :term:`path-like object`.
Yury Selivanov423fd362017-11-20 17:26:28 -0500557
Victor Stinnera6919aa2014-02-19 13:32:34 +0100558
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700559Creating network servers
560^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100561
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700562.. coroutinemethod:: loop.create_server(protocol_factory, \
563 host=None, port=None, \*, \
564 family=socket.AF_UNSPEC, \
565 flags=socket.AI_PASSIVE, \
566 sock=None, backlog=100, ssl=None, \
567 reuse_address=None, reuse_port=None, \
568 ssl_handshake_timeout=None, start_serving=True)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100569
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700570 Create a TCP server (socket type :data:`~socket.SOCK_STREAM`) listening
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400571 on *port* of the *host* address.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200572
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700573 Returns a :class:`Server` object.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200574
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700575 Arguments:
Victor Stinner33f6abe2014-10-12 20:36:04 +0200576
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400577 * *protocol_factory* must be a callable returning a
578 :ref:`protocol <asyncio-protocol>` implementation.
579
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400580 * The *host* parameter can be set to several types which determine where
581 the server would be listening:
582
583 - If *host* is a string, the TCP server is bound to a single network
584 interface specified by *host*.
585
586 - If *host* is a sequence of strings, the TCP server is bound to all
587 network interfaces specified by the sequence.
588
589 - If *host* is an empty string or ``None``, all interfaces are
590 assumed and a list of multiple sockets will be returned (most likely
591 one for IPv4 and another one for IPv6).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200592
593 * *family* can be set to either :data:`socket.AF_INET` or
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700594 :data:`~socket.AF_INET6` to force the socket to use IPv4 or IPv6.
Carol Willing5b7cbd62018-09-12 17:05:17 -0700595 If not set, the *family* will be determined from host name
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700596 (defaults to :data:`~socket.AF_UNSPEC`).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200597
598 * *flags* is a bitmask for :meth:`getaddrinfo`.
599
600 * *sock* can optionally be specified in order to use a preexisting
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400601 socket object. If specified, *host* and *port* must not be specified.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200602
603 * *backlog* is the maximum number of queued connections passed to
604 :meth:`~socket.socket.listen` (defaults to 100).
605
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400606 * *ssl* can be set to an :class:`~ssl.SSLContext` instance to enable
607 TLS over the accepted connections.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200608
609 * *reuse_address* tells the kernel to reuse a local socket in
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700610 ``TIME_WAIT`` state, without waiting for its natural timeout to
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300611 expire. If not specified will automatically be set to ``True`` on
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400612 Unix.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100613
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700614 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
615 same port as other existing endpoints are bound to, so long as they all
616 set this flag when being created. This option is not supported on
617 Windows.
618
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400619 * *ssl_handshake_timeout* is (for a TLS server) the time in seconds to wait
620 for the TLS handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400621 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000622
Yury Selivanovc9070d02018-01-25 18:08:09 -0500623 * *start_serving* set to ``True`` (the default) causes the created server
624 to start accepting connections immediately. When set to ``False``,
625 the user should await on :meth:`Server.start_serving` or
626 :meth:`Server.serve_forever` to make the server to start accepting
627 connections.
628
Neil Aspinallf7686c12017-12-19 19:45:42 +0000629 .. versionadded:: 3.7
630
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700631 Added *ssl_handshake_timeout* and *start_serving* parameters.
632
633 .. versionchanged:: 3.6
634
635 The socket option :py:data:`~socket.TCP_NODELAY` is set by default
636 for all TCP connections.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000637
Victor Stinner60208a12015-09-15 22:41:52 +0200638 .. versionchanged:: 3.5
639
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400640 Added support for SSL/TLS in :class:`ProactorEventLoop`.
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100641
Victor Stinner7b58a2b2015-09-21 18:41:05 +0200642 .. versionchanged:: 3.5.1
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200643
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700644 The *host* parameter can be a sequence of strings.
645
646 .. seealso::
647
648 The :func:`start_server` function is a higher-level alternative API
649 that returns a pair of :class:`StreamReader` and :class:`StreamWriter`
650 that can be used in an async/await code.
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200651
Victor Stinnerea3183f2013-12-03 01:08:00 +0100652
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700653.. coroutinemethod:: loop.create_unix_server(protocol_factory, path=None, \
654 \*, sock=None, backlog=100, ssl=None, \
655 ssl_handshake_timeout=None, start_serving=True)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100656
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700657 Similar to :meth:`loop.create_server` but works with the
658 :py:data:`~socket.AF_UNIX` socket family.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100659
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400660 *path* is the name of a Unix domain socket, and is required,
661 unless a *sock* argument is provided. Abstract Unix sockets,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700662 :class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths
663 are supported.
Yury Selivanov423fd362017-11-20 17:26:28 -0500664
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400665 See the documentation of the :meth:`loop.create_server` method
666 for information about arguments to this method.
667
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400668 .. availability:: Unix.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100669
Neil Aspinallf7686c12017-12-19 19:45:42 +0000670 .. versionadded:: 3.7
671
Elvis Pranskevichusc0d062f2018-06-08 11:36:00 -0400672 The *ssl_handshake_timeout* and *start_serving* parameters.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000673
Yury Selivanov423fd362017-11-20 17:26:28 -0500674 .. versionchanged:: 3.7
675
676 The *path* parameter can now be a :class:`~pathlib.Path` object.
677
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700678.. coroutinemethod:: loop.connect_accepted_socket(protocol_factory, \
679 sock, \*, ssl=None, ssl_handshake_timeout=None)
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500680
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700681 Wrap an already accepted connection into a transport/protocol pair.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500682
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700683 This method can be used by servers that accept connections outside
684 of asyncio but that use asyncio to handle them.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500685
686 Parameters:
687
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400688 * *protocol_factory* must be a callable returning a
689 :ref:`protocol <asyncio-protocol>` implementation.
690
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700691 * *sock* is a preexisting socket object returned from
692 :meth:`socket.accept <socket.socket.accept>`.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500693
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700694 * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over
695 the accepted connections.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500696
Neil Aspinallf7686c12017-12-19 19:45:42 +0000697 * *ssl_handshake_timeout* is (for an SSL connection) the time in seconds to
698 wait for the SSL handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400699 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000700
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700701 Returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100702
Neil Aspinallf7686c12017-12-19 19:45:42 +0000703 .. versionadded:: 3.7
704
705 The *ssl_handshake_timeout* parameter.
706
AraHaan431665b2017-11-21 11:06:26 -0500707 .. versionadded:: 3.5.3
708
709
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700710Transferring files
711^^^^^^^^^^^^^^^^^^
Andrew Svetlov7c684072018-01-27 21:22:47 +0200712
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700713.. coroutinemethod:: loop.sendfile(transport, file, \
714 offset=0, count=None, *, fallback=True)
Andrew Svetlov7c684072018-01-27 21:22:47 +0200715
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700716 Send a *file* over a *transport*. Return the total number of bytes
717 sent.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200718
719 The method uses high-performance :meth:`os.sendfile` if available.
720
721 *file* must be a regular file object opened in binary mode.
722
723 *offset* tells from where to start reading the file. If specified,
724 *count* is the total number of bytes to transmit as opposed to
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400725 sending the file until EOF is reached. File position is always updated,
726 even when this method raises an error, and
727 :meth:`file.tell() <io.IOBase.tell>` can be used to obtain the actual
728 number of bytes sent.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200729
730 *fallback* set to ``True`` makes asyncio to manually read and send
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700731 the file when the platform does not support the sendfile system call
Andrew Svetlov7c684072018-01-27 21:22:47 +0200732 (e.g. Windows or SSL socket on Unix).
733
734 Raise :exc:`SendfileNotAvailableError` if the system does not support
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400735 the *sendfile* syscall and *fallback* is ``False``.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200736
737 .. versionadded:: 3.7
738
739
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500740TLS Upgrade
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700741^^^^^^^^^^^
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500742
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700743.. coroutinemethod:: loop.start_tls(transport, protocol, \
744 sslcontext, \*, server_side=False, \
745 server_hostname=None, ssl_handshake_timeout=None)
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500746
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700747 Upgrade an existing transport-based connection to TLS.
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500748
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700749 Return a new transport instance, that the *protocol* must start using
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500750 immediately after the *await*. The *transport* instance passed to
751 the *start_tls* method should never be used again.
752
753 Parameters:
754
755 * *transport* and *protocol* instances that methods like
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700756 :meth:`~loop.create_server` and
757 :meth:`~loop.create_connection` return.
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500758
759 * *sslcontext*: a configured instance of :class:`~ssl.SSLContext`.
760
761 * *server_side* pass ``True`` when a server-side connection is being
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700762 upgraded (like the one created by :meth:`~loop.create_server`).
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500763
764 * *server_hostname*: sets or overrides the host name that the target
765 server's certificate will be matched against.
766
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400767 * *ssl_handshake_timeout* is (for a TLS connection) the time in seconds to
768 wait for the TLS handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400769 ``60.0`` seconds if ``None`` (default).
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500770
771 .. versionadded:: 3.7
772
773
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700774Watching file descriptors
775^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerc1567df2014-02-08 23:22:58 +0100776
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700777.. method:: loop.add_reader(fd, callback, \*args)
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200778
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400779 Start monitoring the *fd* file descriptor for read availability and
780 invoke *callback* with the specified arguments once *fd* is available for
781 reading.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200782
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700783.. method:: loop.remove_reader(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100784
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400785 Stop monitoring the *fd* file descriptor for read availability.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100786
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700787.. method:: loop.add_writer(fd, callback, \*args)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100788
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400789 Start monitoring the *fd* file descriptor for write availability and
790 invoke *callback* with the specified arguments once *fd* is available for
791 writing.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100792
Yury Selivanove247b462018-09-20 12:43:59 -0400793 Use :func:`functools.partial` :ref:`to pass keyword arguments
Naglis17473342018-12-04 09:31:15 +0200794 <asyncio-pass-keywords>` to *callback*.
Victor Stinner8464c242014-11-28 13:15:41 +0100795
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700796.. method:: loop.remove_writer(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100797
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400798 Stop monitoring the *fd* file descriptor for write availability.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100799
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700800See also :ref:`Platform Support <asyncio-platform-support>` section
801for some limitations of these methods.
Victor Stinner04e6df32014-10-11 16:16:27 +0200802
Victor Stinnerc1567df2014-02-08 23:22:58 +0100803
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700804Working with socket objects directly
805^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerc1567df2014-02-08 23:22:58 +0100806
Carol Willing5b7cbd62018-09-12 17:05:17 -0700807In general, protocol implementations that use transport-based APIs
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700808such as :meth:`loop.create_connection` and :meth:`loop.create_server`
809are faster than implementations that work with sockets directly.
Carol Willing5b7cbd62018-09-12 17:05:17 -0700810However, there are some use cases when performance is not critical, and
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700811working with :class:`~socket.socket` objects directly is more
812convenient.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100813
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700814.. coroutinemethod:: loop.sock_recv(sock, nbytes)
Yury Selivanov55c50842016-06-08 12:48:15 -0400815
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400816 Receive up to *nbytes* from *sock*. Asynchronous version of
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700817 :meth:`socket.recv() <socket.socket.recv>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100818
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400819 Return the received data as a bytes object.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700820
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400821 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200822
Yury Selivanov19a44f62017-12-14 20:53:26 -0500823 .. versionchanged:: 3.7
Carol Willing5b7cbd62018-09-12 17:05:17 -0700824 Even though this method was always documented as a coroutine
825 method, releases before Python 3.7 returned a :class:`Future`.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700826 Since Python 3.7 this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100827
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700828.. coroutinemethod:: loop.sock_recv_into(sock, buf)
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200829
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400830 Receive data from *sock* into the *buf* buffer. Modeled after the blocking
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700831 :meth:`socket.recv_into() <socket.socket.recv_into>` method.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200832
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700833 Return the number of bytes written to the buffer.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200834
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400835 *sock* must be a non-blocking socket.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200836
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200837 .. versionadded:: 3.7
838
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700839.. coroutinemethod:: loop.sock_sendall(sock, data)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100840
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400841 Send *data* to the *sock* socket. Asynchronous version of
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700842 :meth:`socket.sendall() <socket.socket.sendall>`.
Yury Selivanov55c50842016-06-08 12:48:15 -0400843
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400844 This method continues to send to the socket until either all data
845 in *data* has been sent or an error occurs. ``None`` is returned
Carol Willing5b7cbd62018-09-12 17:05:17 -0700846 on success. On error, an exception is raised. Additionally, there is no way
847 to determine how much data, if any, was successfully processed by the
848 receiving end of the connection.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100849
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400850 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200851
Yury Selivanov19a44f62017-12-14 20:53:26 -0500852 .. versionchanged:: 3.7
853 Even though the method was always documented as a coroutine
854 method, before Python 3.7 it returned an :class:`Future`.
855 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100856
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700857.. coroutinemethod:: loop.sock_connect(sock, address)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100858
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400859 Connect *sock* to a remote socket at *address*.
Victor Stinner1b0580b2014-02-13 09:24:37 +0100860
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700861 Asynchronous version of :meth:`socket.connect() <socket.socket.connect>`.
862
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400863 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200864
Yury Selivanov55c50842016-06-08 12:48:15 -0400865 .. versionchanged:: 3.5.2
866 ``address`` no longer needs to be resolved. ``sock_connect``
867 will try to check if the *address* is already resolved by calling
868 :func:`socket.inet_pton`. If not,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700869 :meth:`loop.getaddrinfo` will be used to resolve the
Yury Selivanov55c50842016-06-08 12:48:15 -0400870 *address*.
871
Victor Stinnerc1567df2014-02-08 23:22:58 +0100872 .. seealso::
873
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700874 :meth:`loop.create_connection`
Yury Selivanov55c50842016-06-08 12:48:15 -0400875 and :func:`asyncio.open_connection() <open_connection>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100876
877
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700878.. coroutinemethod:: loop.sock_accept(sock)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100879
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700880 Accept a connection. Modeled after the blocking
881 :meth:`socket.accept() <socket.socket.accept>` method.
Yury Selivanov55c50842016-06-08 12:48:15 -0400882
883 The socket must be bound to an address and listening
Victor Stinnerc1567df2014-02-08 23:22:58 +0100884 for connections. The return value is a pair ``(conn, address)`` where *conn*
885 is a *new* socket object usable to send and receive data on the connection,
886 and *address* is the address bound to the socket on the other end of the
887 connection.
888
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400889 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200890
Yury Selivanov19a44f62017-12-14 20:53:26 -0500891 .. versionchanged:: 3.7
892 Even though the method was always documented as a coroutine
893 method, before Python 3.7 it returned a :class:`Future`.
894 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100895
896 .. seealso::
897
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700898 :meth:`loop.create_server` and :func:`start_server`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100899
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700900.. coroutinemethod:: loop.sock_sendfile(sock, file, offset=0, count=None, \
901 \*, fallback=True)
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200902
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700903 Send a file using high-performance :mod:`os.sendfile` if possible.
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400904 Return the total number of bytes sent.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200905
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700906 Asynchronous version of :meth:`socket.sendfile() <socket.socket.sendfile>`.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200907
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400908 *sock* must be a non-blocking :const:`socket.SOCK_STREAM`
909 :class:`~socket.socket`.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200910
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400911 *file* must be a regular file object open in binary mode.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200912
913 *offset* tells from where to start reading the file. If specified,
914 *count* is the total number of bytes to transmit as opposed to
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400915 sending the file until EOF is reached. File position is always updated,
916 even when this method raises an error, and
917 :meth:`file.tell() <io.IOBase.tell>` can be used to obtain the actual
918 number of bytes sent.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200919
Carol Willing5b7cbd62018-09-12 17:05:17 -0700920 *fallback*, when set to ``True``, makes asyncio manually read and send
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200921 the file when the platform does not support the sendfile syscall
922 (e.g. Windows or SSL socket on Unix).
923
Andrew Svetlov7464e872018-01-19 20:04:29 +0200924 Raise :exc:`SendfileNotAvailableError` if the system does not support
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200925 *sendfile* syscall and *fallback* is ``False``.
926
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400927 *sock* must be a non-blocking socket.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700928
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200929 .. versionadded:: 3.7
930
Victor Stinnerc1567df2014-02-08 23:22:58 +0100931
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700932DNS
933^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100934
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700935.. coroutinemethod:: loop.getaddrinfo(host, port, \*, family=0, \
936 type=0, proto=0, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100937
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700938 Asynchronous version of :meth:`socket.getaddrinfo`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100939
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700940.. coroutinemethod:: loop.getnameinfo(sockaddr, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100941
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700942 Asynchronous version of :meth:`socket.getnameinfo`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100943
Yury Selivanovbec23722018-01-28 14:09:40 -0500944.. versionchanged:: 3.7
945 Both *getaddrinfo* and *getnameinfo* methods were always documented
946 to return a coroutine, but prior to Python 3.7 they were, in fact,
947 returning :class:`asyncio.Future` objects. Starting with Python 3.7
948 both methods are coroutines.
949
Victor Stinnerea3183f2013-12-03 01:08:00 +0100950
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700951Working with pipes
952^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100953
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700954.. coroutinemethod:: loop.connect_read_pipe(protocol_factory, pipe)
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200955
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400956 Register the read end of *pipe* in the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100957
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700958 *protocol_factory* must be a callable returning an
959 :ref:`asyncio protocol <asyncio-protocol>` implementation.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100960
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700961 *pipe* is a :term:`file-like object <file object>`.
962
963 Return pair ``(transport, protocol)``, where *transport* supports
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400964 the :class:`ReadTransport` interface and *protocol* is an object
965 instantiated by the *protocol_factory*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100966
Victor Stinnerd84fd732014-08-26 01:01:59 +0200967 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
968 non-blocking mode.
969
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700970.. coroutinemethod:: loop.connect_write_pipe(protocol_factory, pipe)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100971
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400972 Register the write end of *pipe* in the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100973
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700974 *protocol_factory* must be a callable returning an
975 :ref:`asyncio protocol <asyncio-protocol>` implementation.
976
977 *pipe* is :term:`file-like object <file object>`.
978
Victor Stinner2cef3002014-10-23 22:38:46 +0200979 Return pair ``(transport, protocol)``, where *transport* supports
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400980 :class:`WriteTransport` interface and *protocol* is an object
981 instantiated by the *protocol_factory*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100982
Victor Stinnerd84fd732014-08-26 01:01:59 +0200983 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
984 non-blocking mode.
985
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700986.. note::
987
988 :class:`SelectorEventLoop` does not support the above methods on
Carol Willing5b7cbd62018-09-12 17:05:17 -0700989 Windows. Use :class:`ProactorEventLoop` instead for Windows.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700990
Victor Stinner08444382014-02-02 22:43:39 +0100991.. seealso::
992
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700993 The :meth:`loop.subprocess_exec` and
994 :meth:`loop.subprocess_shell` methods.
Victor Stinner08444382014-02-02 22:43:39 +0100995
Victor Stinnerea3183f2013-12-03 01:08:00 +0100996
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400997Unix signals
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700998^^^^^^^^^^^^
Victor Stinner8b863482014-01-27 10:07:50 +0100999
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001000.. method:: loop.add_signal_handler(signum, callback, \*args)
Victor Stinner8b863482014-01-27 10:07:50 +01001001
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001002 Set *callback* as the handler for the *signum* signal.
Victor Stinner8b863482014-01-27 10:07:50 +01001003
Hrvoje Nikšiće3666fc2018-12-18 22:31:29 +01001004 The callback will be invoked by *loop*, along with other queued callbacks
1005 and runnable coroutines of that event loop. Unlike signal handlers
1006 registered using :func:`signal.signal`, a callback registered with this
1007 function is allowed to interact with the event loop.
1008
Victor Stinner8b863482014-01-27 10:07:50 +01001009 Raise :exc:`ValueError` if the signal number is invalid or uncatchable.
1010 Raise :exc:`RuntimeError` if there is a problem setting up the handler.
1011
Yury Selivanove247b462018-09-20 12:43:59 -04001012 Use :func:`functools.partial` :ref:`to pass keyword arguments
Naglis17473342018-12-04 09:31:15 +02001013 <asyncio-pass-keywords>` to *callback*.
Victor Stinner8464c242014-11-28 13:15:41 +01001014
Hrvoje Nikšiće3666fc2018-12-18 22:31:29 +01001015 Like :func:`signal.signal`, this function must be invoked in the main
1016 thread.
1017
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001018.. method:: loop.remove_signal_handler(sig)
Victor Stinner8b863482014-01-27 10:07:50 +01001019
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001020 Remove the handler for the *sig* signal.
Victor Stinner8b863482014-01-27 10:07:50 +01001021
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001022 Return ``True`` if the signal handler was removed, or ``False`` if
1023 no handler was set for the given signal.
Victor Stinner8b863482014-01-27 10:07:50 +01001024
Cheryl Sabella2d6097d2018-10-12 10:55:20 -04001025 .. availability:: Unix.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001026
Victor Stinner8b863482014-01-27 10:07:50 +01001027.. seealso::
1028
1029 The :mod:`signal` module.
1030
1031
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001032Executing code in thread or process pools
1033^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +01001034
Yury Selivanov47150392018-09-18 17:55:44 -04001035.. awaitablemethod:: loop.run_in_executor(executor, func, \*args)
Victor Stinnerea3183f2013-12-03 01:08:00 +01001036
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001037 Arrange for *func* to be called in the specified executor.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001038
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001039 The *executor* argument should be an :class:`concurrent.futures.Executor`
Larry Hastings3732ed22014-03-15 21:13:56 -07001040 instance. The default executor is used if *executor* is ``None``.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001041
Yury Selivanove247b462018-09-20 12:43:59 -04001042 Example::
1043
1044 import asyncio
1045 import concurrent.futures
1046
1047 def blocking_io():
1048 # File operations (such as logging) can block the
1049 # event loop: run them in a thread pool.
1050 with open('/dev/urandom', 'rb') as f:
1051 return f.read(100)
1052
1053 def cpu_bound():
1054 # CPU-bound operations will block the event loop:
1055 # in general it is preferable to run them in a
1056 # process pool.
1057 return sum(i * i for i in range(10 ** 7))
1058
1059 async def main():
1060 loop = asyncio.get_running_loop()
1061
1062 ## Options:
1063
1064 # 1. Run in the default loop's executor:
1065 result = await loop.run_in_executor(
1066 None, blocking_io)
1067 print('default thread pool', result)
1068
1069 # 2. Run in a custom thread pool:
1070 with concurrent.futures.ThreadPoolExecutor() as pool:
1071 result = await loop.run_in_executor(
1072 pool, blocking_io)
1073 print('custom thread pool', result)
1074
1075 # 3. Run in a custom process pool:
1076 with concurrent.futures.ProcessPoolExecutor() as pool:
1077 result = await loop.run_in_executor(
1078 pool, cpu_bound)
1079 print('custom process pool', result)
1080
1081 asyncio.run(main())
Victor Stinner8464c242014-11-28 13:15:41 +01001082
Yury Selivanovbec23722018-01-28 14:09:40 -05001083 This method returns a :class:`asyncio.Future` object.
1084
Yury Selivanove247b462018-09-20 12:43:59 -04001085 Use :func:`functools.partial` :ref:`to pass keyword arguments
1086 <asyncio-pass-keywords>` to *func*.
1087
Yury Selivanove8a60452016-10-21 17:40:42 -04001088 .. versionchanged:: 3.5.3
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001089 :meth:`loop.run_in_executor` no longer configures the
Yury Selivanove8a60452016-10-21 17:40:42 -04001090 ``max_workers`` of the thread pool executor it creates, instead
1091 leaving it up to the thread pool executor
1092 (:class:`~concurrent.futures.ThreadPoolExecutor`) to set the
1093 default.
1094
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001095.. method:: loop.set_default_executor(executor)
Victor Stinnerea3183f2013-12-03 01:08:00 +01001096
Elvis Pranskevichus22d25082018-07-30 11:42:43 +01001097 Set *executor* as the default executor used by :meth:`run_in_executor`.
1098 *executor* should be an instance of
1099 :class:`~concurrent.futures.ThreadPoolExecutor`.
1100
1101 .. deprecated:: 3.8
1102 Using an executor that is not an instance of
1103 :class:`~concurrent.futures.ThreadPoolExecutor` is deprecated and
1104 will trigger an error in Python 3.9.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001105
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001106 *executor* must be an instance of
1107 :class:`concurrent.futures.ThreadPoolExecutor`.
1108
Victor Stinnerea3183f2013-12-03 01:08:00 +01001109
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001110Error Handling API
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001111^^^^^^^^^^^^^^^^^^
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001112
Martin Panterc04fb562016-02-10 05:44:01 +00001113Allows customizing how exceptions are handled in the event loop.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001114
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001115.. method:: loop.set_exception_handler(handler)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001116
1117 Set *handler* as the new event loop exception handler.
1118
1119 If *handler* is ``None``, the default exception handler will
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001120 be set. Otherwise, *handler* must be a callable with the signature
1121 matching ``(loop, context)``, where ``loop``
1122 is a reference to the active event loop, and ``context``
1123 is a ``dict`` object containing the details of the exception
1124 (see :meth:`call_exception_handler` documentation for details
1125 about context).
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001126
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001127.. method:: loop.get_exception_handler()
Yury Selivanov950204d2016-05-16 16:23:00 -04001128
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001129 Return the current exception handler, or ``None`` if no custom
1130 exception handler was set.
Yury Selivanov950204d2016-05-16 16:23:00 -04001131
1132 .. versionadded:: 3.5.2
1133
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001134.. method:: loop.default_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001135
1136 Default exception handler.
1137
1138 This is called when an exception occurs and no exception
Carol Willing5b7cbd62018-09-12 17:05:17 -07001139 handler is set. This can be called by a custom exception
1140 handler that wants to defer to the default handler behavior.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001141
1142 *context* parameter has the same meaning as in
1143 :meth:`call_exception_handler`.
1144
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001145.. method:: loop.call_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001146
1147 Call the current event loop exception handler.
1148
1149 *context* is a ``dict`` object containing the following keys
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001150 (new keys may be introduced in future Python versions):
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001151
1152 * 'message': Error message;
1153 * 'exception' (optional): Exception object;
1154 * 'future' (optional): :class:`asyncio.Future` instance;
1155 * 'handle' (optional): :class:`asyncio.Handle` instance;
1156 * 'protocol' (optional): :ref:`Protocol <asyncio-protocol>` instance;
1157 * 'transport' (optional): :ref:`Transport <asyncio-transport>` instance;
1158 * 'socket' (optional): :class:`socket.socket` instance.
1159
1160 .. note::
1161
Carol Willing5b7cbd62018-09-12 17:05:17 -07001162 This method should not be overloaded in subclassed
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001163 event loops. For custom exception handling, use
1164 the :meth:`set_exception_handler()` method.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001165
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001166Enabling debug mode
1167^^^^^^^^^^^^^^^^^^^
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001168
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001169.. method:: loop.get_debug()
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001170
Victor Stinner7b7120e2014-06-23 00:12:14 +02001171 Get the debug mode (:class:`bool`) of the event loop.
1172
1173 The default value is ``True`` if the environment variable
1174 :envvar:`PYTHONASYNCIODEBUG` is set to a non-empty string, ``False``
1175 otherwise.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001176
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001177.. method:: loop.set_debug(enabled: bool)
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001178
1179 Set the debug mode of the event loop.
1180
Yury Selivanov805e27e2018-09-14 16:57:11 -07001181 .. versionchanged:: 3.7
1182
1183 The new ``-X dev`` command line option can now also be used
1184 to enable the debug mode.
1185
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001186.. seealso::
1187
Victor Stinner62511fd2014-06-23 00:36:11 +02001188 The :ref:`debug mode of asyncio <asyncio-debug-mode>`.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001189
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001190
1191Running Subprocesses
1192^^^^^^^^^^^^^^^^^^^^
1193
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001194Methods described in this subsections are low-level. In regular
1195async/await code consider using the high-level
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001196:func:`asyncio.create_subprocess_shell` and
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001197:func:`asyncio.create_subprocess_exec` convenience functions instead.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001198
1199.. note::
1200
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001201 The default asyncio event loop on **Windows** does not support
1202 subprocesses. See :ref:`Subprocess Support on Windows
1203 <asyncio-windows-subprocess>` for details.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001204
1205.. coroutinemethod:: loop.subprocess_exec(protocol_factory, \*args, \
1206 stdin=subprocess.PIPE, stdout=subprocess.PIPE, \
1207 stderr=subprocess.PIPE, \*\*kwargs)
1208
1209 Create a subprocess from one or more string arguments specified by
1210 *args*.
1211
1212 *args* must be a list of strings represented by:
1213
1214 * :class:`str`;
1215 * or :class:`bytes`, encoded to the
1216 :ref:`filesystem encoding <filesystem-encoding>`.
1217
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001218 The first string specifies the program executable,
Carol Willing5b7cbd62018-09-12 17:05:17 -07001219 and the remaining strings specify the arguments. Together, string
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001220 arguments form the ``argv`` of the program.
1221
1222 This is similar to the standard library :class:`subprocess.Popen`
1223 class called with ``shell=False`` and the list of strings passed as
1224 the first argument; however, where :class:`~subprocess.Popen` takes
1225 a single argument which is list of strings, *subprocess_exec*
1226 takes multiple string arguments.
1227
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001228 The *protocol_factory* must be a callable returning a subclass of the
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001229 :class:`asyncio.SubprocessProtocol` class.
1230
1231 Other parameters:
1232
sbstpf0d4c642019-05-27 19:51:19 -04001233 * *stdin* 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 input 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
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001244
sbstpf0d4c642019-05-27 19:51:19 -04001245 * *stdout* can be any of these:
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001246
sbstpf0d4c642019-05-27 19:51:19 -04001247 * a file-like object representing a pipe to be connected to the
1248 subprocess's standard output 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
1257 * *stderr* can be any of these:
1258
1259 * a file-like object representing a pipe to be connected to the
1260 subprocess's standard error stream using
1261 :meth:`~loop.connect_write_pipe`
1262 * the :const:`subprocess.PIPE` constant (default) which will create a new
1263 pipe and connect it,
1264 * the value ``None`` which will make the subprocess inherit the file
1265 descriptor from this process
1266 * the :const:`subprocess.DEVNULL` constant which indicates that the
1267 special :data:`os.devnull` file will be used
1268 * the :const:`subprocess.STDOUT` constant which will connect the standard
1269 error stream to the process' standard output stream
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001270
1271 * All other keyword arguments are passed to :class:`subprocess.Popen`
sbstpf0d4c642019-05-27 19:51:19 -04001272 without interpretation, except for *bufsize*, *universal_newlines*,
1273 *shell*, *text*, *encoding* and *errors*, which should not be specified
1274 at all.
1275
1276 The ``asyncio`` subprocess API does not support decoding the streams
1277 as text. :func:`bytes.decode` can be used to convert the bytes returned
1278 from the stream to text.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001279
1280 See the constructor of the :class:`subprocess.Popen` class
1281 for documentation on other arguments.
1282
1283 Returns a pair of ``(transport, protocol)``, where *transport*
Bumsik Kim5cc583d2018-09-16 19:40:44 -04001284 conforms to the :class:`asyncio.SubprocessTransport` base class and
1285 *protocol* is an object instantiated by the *protocol_factory*.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001286
1287.. coroutinemethod:: loop.subprocess_shell(protocol_factory, cmd, \*, \
1288 stdin=subprocess.PIPE, stdout=subprocess.PIPE, \
1289 stderr=subprocess.PIPE, \*\*kwargs)
1290
1291 Create a subprocess from *cmd*, which can be a :class:`str` or a
1292 :class:`bytes` string encoded to the
1293 :ref:`filesystem encoding <filesystem-encoding>`,
1294 using the platform's "shell" syntax.
1295
1296 This is similar to the standard library :class:`subprocess.Popen`
1297 class called with ``shell=True``.
1298
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001299 The *protocol_factory* must be a callable returning a subclass of the
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001300 :class:`SubprocessProtocol` class.
1301
1302 See :meth:`~loop.subprocess_exec` for more details about
1303 the remaining arguments.
1304
1305 Returns a pair of ``(transport, protocol)``, where *transport*
Bumsik Kim5cc583d2018-09-16 19:40:44 -04001306 conforms to the :class:`SubprocessTransport` base class and
1307 *protocol* is an object instantiated by the *protocol_factory*.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001308
1309.. note::
1310 It is the application's responsibility to ensure that all whitespace
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001311 and special characters are quoted appropriately to avoid `shell injection
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001312 <https://en.wikipedia.org/wiki/Shell_injection#Shell_injection>`_
1313 vulnerabilities. The :func:`shlex.quote` function can be used to
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001314 properly escape whitespace and special characters in strings that
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001315 are going to be used to construct shell commands.
1316
1317
1318Callback Handles
1319================
1320
1321.. class:: Handle
1322
1323 A callback wrapper object returned by :meth:`loop.call_soon`,
1324 :meth:`loop.call_soon_threadsafe`.
1325
1326 .. method:: cancel()
1327
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001328 Cancel the callback. If the callback has already been canceled
1329 or executed, this method has no effect.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001330
1331 .. method:: cancelled()
1332
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001333 Return ``True`` if the callback was cancelled.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001334
1335 .. versionadded:: 3.7
1336
1337.. class:: TimerHandle
1338
1339 A callback wrapper object returned by :meth:`loop.call_later`,
1340 and :meth:`loop.call_at`.
1341
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001342 This class is a subclass of :class:`Handle`.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001343
1344 .. method:: when()
1345
1346 Return a scheduled callback time as :class:`float` seconds.
1347
1348 The time is an absolute timestamp, using the same time
1349 reference as :meth:`loop.time`.
1350
1351 .. versionadded:: 3.7
1352
1353
1354Server Objects
1355==============
1356
1357Server objects are created by :meth:`loop.create_server`,
1358:meth:`loop.create_unix_server`, :func:`start_server`,
1359and :func:`start_unix_server` functions.
1360
1361Do not instantiate the class directly.
Victor Stinner8c462c52014-01-24 18:11:43 +01001362
Victor Stinner8ebeb032014-07-11 23:47:40 +02001363.. class:: Server
Victor Stinner8c462c52014-01-24 18:11:43 +01001364
Yury Selivanovc9070d02018-01-25 18:08:09 -05001365 *Server* objects are asynchronous context managers. When used in an
1366 ``async with`` statement, it's guaranteed that the Server object is
1367 closed and not accepting new connections when the ``async with``
1368 statement is completed::
1369
1370 srv = await loop.create_server(...)
1371
1372 async with srv:
1373 # some code
1374
Carol Willing5b7cbd62018-09-12 17:05:17 -07001375 # At this point, srv is closed and no longer accepts new connections.
Yury Selivanovc9070d02018-01-25 18:08:09 -05001376
1377
1378 .. versionchanged:: 3.7
1379 Server object is an asynchronous context manager since Python 3.7.
Victor Stinner8c462c52014-01-24 18:11:43 +01001380
1381 .. method:: close()
1382
Victor Stinner4bfb14a2014-07-12 03:20:24 +02001383 Stop serving: close listening sockets and set the :attr:`sockets`
1384 attribute to ``None``.
1385
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001386 The sockets that represent existing incoming client connections
1387 are left open.
Victor Stinner8ebeb032014-07-11 23:47:40 +02001388
Berker Peksag49c9edf2016-01-20 07:14:22 +02001389 The server is closed asynchronously, use the :meth:`wait_closed`
1390 coroutine to wait until the server is closed.
Victor Stinner8c462c52014-01-24 18:11:43 +01001391
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)1634fc22017-12-30 20:39:32 +05301392 .. method:: get_loop()
1393
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001394 Return the event loop associated with the server object.
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)1634fc22017-12-30 20:39:32 +05301395
1396 .. versionadded:: 3.7
1397
Yury Selivanovc9070d02018-01-25 18:08:09 -05001398 .. coroutinemethod:: start_serving()
1399
1400 Start accepting connections.
1401
1402 This method is idempotent, so it can be called when
1403 the server is already being serving.
1404
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001405 The *start_serving* keyword-only parameter to
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001406 :meth:`loop.create_server` and
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001407 :meth:`asyncio.start_server` allows creating a Server object
1408 that is not accepting connections initially. In this case
1409 ``Server.start_serving()``, or :meth:`Server.serve_forever` can be used
1410 to make the Server start accepting connections.
Yury Selivanovc9070d02018-01-25 18:08:09 -05001411
1412 .. versionadded:: 3.7
1413
1414 .. coroutinemethod:: serve_forever()
1415
1416 Start accepting connections until the coroutine is cancelled.
1417 Cancellation of ``serve_forever`` task causes the server
1418 to be closed.
1419
1420 This method can be called if the server is already accepting
1421 connections. Only one ``serve_forever`` task can exist per
1422 one *Server* object.
1423
1424 Example::
1425
1426 async def client_connected(reader, writer):
1427 # Communicate with the client with
1428 # reader/writer streams. For example:
1429 await reader.readline()
1430
1431 async def main(host, port):
1432 srv = await asyncio.start_server(
1433 client_connected, host, port)
Andrew Svetlov17ab8f02018-02-17 19:44:35 +02001434 await srv.serve_forever()
Yury Selivanovc9070d02018-01-25 18:08:09 -05001435
1436 asyncio.run(main('127.0.0.1', 0))
1437
1438 .. versionadded:: 3.7
1439
1440 .. method:: is_serving()
1441
1442 Return ``True`` if the server is accepting new connections.
1443
1444 .. versionadded:: 3.7
1445
Victor Stinnerbdd574d2015-02-12 22:49:18 +01001446 .. coroutinemethod:: wait_closed()
Victor Stinner8c462c52014-01-24 18:11:43 +01001447
Victor Stinner8ebeb032014-07-11 23:47:40 +02001448 Wait until the :meth:`close` method completes.
1449
Victor Stinner8ebeb032014-07-11 23:47:40 +02001450 .. attribute:: sockets
1451
Emmanuel Ariasdf5cdc12019-02-22 14:34:41 -03001452 List of :class:`socket.socket` objects the server is listening on.
Victor Stinner8c462c52014-01-24 18:11:43 +01001453
Yury Selivanovc9070d02018-01-25 18:08:09 -05001454 .. versionchanged:: 3.7
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001455 Prior to Python 3.7 ``Server.sockets`` used to return an
1456 internal list of server sockets directly. In 3.7 a copy
Yury Selivanovc9070d02018-01-25 18:08:09 -05001457 of that list is returned.
1458
Victor Stinner8c462c52014-01-24 18:11:43 +01001459
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001460.. _asyncio-event-loops:
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001461
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001462Event Loop Implementations
1463==========================
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001464
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001465asyncio ships with two different event loop implementations:
1466:class:`SelectorEventLoop` and :class:`ProactorEventLoop`.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001467
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001468By default asyncio is configured to use :class:`SelectorEventLoop`
Ben Darnell9ffca672019-06-22 13:38:21 -04001469on Unix and :class:`ProactorEventLoop` on Windows.
Andrew Svetlov3d4dbd82018-02-01 19:59:32 +02001470
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001471
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001472.. class:: SelectorEventLoop
1473
1474 An event loop based on the :mod:`selectors` module.
1475
1476 Uses the most efficient *selector* available for the given
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001477 platform. It is also possible to manually configure the
1478 exact selector implementation to be used::
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001479
1480 import asyncio
1481 import selectors
1482
1483 selector = selectors.SelectSelector()
1484 loop = asyncio.SelectorEventLoop(selector)
1485 asyncio.set_event_loop(loop)
Andrew Svetlov7464e872018-01-19 20:04:29 +02001486
1487
Cheryl Sabella2d6097d2018-10-12 10:55:20 -04001488 .. availability:: Unix, Windows.
Andrew Svetlov7464e872018-01-19 20:04:29 +02001489
1490
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001491.. class:: ProactorEventLoop
1492
1493 An event loop for Windows that uses "I/O Completion Ports" (IOCP).
1494
Cheryl Sabella2d6097d2018-10-12 10:55:20 -04001495 .. availability:: Windows.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001496
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001497 .. seealso::
1498
1499 `MSDN documentation on I/O Completion Ports
1500 <https://docs.microsoft.com/en-ca/windows/desktop/FileIO/i-o-completion-ports>`_.
1501
1502
1503.. class:: AbstractEventLoop
1504
1505 Abstract base class for asyncio-compliant event loops.
1506
1507 The :ref:`Event Loop Methods <asyncio-event-loop>` section lists all
1508 methods that an alternative implementation of ``AbstractEventLoop``
1509 should have defined.
1510
1511
1512Examples
1513========
1514
1515Note that all examples in this section **purposefully** show how
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001516to use the low-level event loop APIs, such as :meth:`loop.run_forever`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001517and :meth:`loop.call_soon`. Modern asyncio applications rarely
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001518need to be written this way; consider using the high-level functions
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001519like :func:`asyncio.run`.
1520
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001521
Yury Selivanov394374e2018-09-17 15:35:24 -04001522.. _asyncio_example_lowlevel_helloworld:
Victor Stinnerea3183f2013-12-03 01:08:00 +01001523
Victor Stinner7f314ed2014-10-15 18:49:16 +02001524Hello World with call_soon()
Victor Stinnera092a612015-01-09 15:58:41 +01001525^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +01001526
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001527An example using the :meth:`loop.call_soon` method to schedule a
1528callback. The callback displays ``"Hello World"`` and then stops the
1529event loop::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001530
1531 import asyncio
1532
Victor Stinner7f314ed2014-10-15 18:49:16 +02001533 def hello_world(loop):
Carol Willing5b7cbd62018-09-12 17:05:17 -07001534 """A callback to print 'Hello World' and stop the event loop"""
Victor Stinnerea3183f2013-12-03 01:08:00 +01001535 print('Hello World')
Victor Stinner7f314ed2014-10-15 18:49:16 +02001536 loop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001537
1538 loop = asyncio.get_event_loop()
Victor Stinner7f314ed2014-10-15 18:49:16 +02001539
1540 # Schedule a call to hello_world()
1541 loop.call_soon(hello_world, loop)
1542
1543 # Blocking call interrupted by loop.stop()
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001544 try:
1545 loop.run_forever()
1546 finally:
1547 loop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001548
Victor Stinner3e09e322013-12-03 01:22:06 +01001549.. seealso::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001550
Yury Selivanov3faaa882018-09-14 13:32:07 -07001551 A similar :ref:`Hello World <coroutine>`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001552 example created with a coroutine and the :func:`run` function.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001553
Victor Stinner8b863482014-01-27 10:07:50 +01001554
Yury Selivanov394374e2018-09-17 15:35:24 -04001555.. _asyncio_example_call_later:
Victor Stinner7f314ed2014-10-15 18:49:16 +02001556
1557Display the current date with call_later()
Victor Stinnera092a612015-01-09 15:58:41 +01001558^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner7f314ed2014-10-15 18:49:16 +02001559
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001560An example of a callback displaying the current date every second. The
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001561callback uses the :meth:`loop.call_later` method to reschedule itself
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001562after 5 seconds, and then stops the event loop::
Victor Stinner7f314ed2014-10-15 18:49:16 +02001563
1564 import asyncio
1565 import datetime
1566
1567 def display_date(end_time, loop):
1568 print(datetime.datetime.now())
1569 if (loop.time() + 1.0) < end_time:
1570 loop.call_later(1, display_date, end_time, loop)
1571 else:
1572 loop.stop()
1573
1574 loop = asyncio.get_event_loop()
1575
1576 # Schedule the first call to display_date()
1577 end_time = loop.time() + 5.0
1578 loop.call_soon(display_date, end_time, loop)
1579
1580 # Blocking call interrupted by loop.stop()
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001581 try:
1582 loop.run_forever()
1583 finally:
1584 loop.close()
Victor Stinner7f314ed2014-10-15 18:49:16 +02001585
1586.. seealso::
1587
Yury Selivanov7372c3b2018-09-14 15:11:24 -07001588 A similar :ref:`current date <asyncio_example_sleep>` example
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001589 created with a coroutine and the :func:`run` function.
Victor Stinner7f314ed2014-10-15 18:49:16 +02001590
1591
Yury Selivanov394374e2018-09-17 15:35:24 -04001592.. _asyncio_example_watch_fd:
Victor Stinner8b863482014-01-27 10:07:50 +01001593
Victor Stinner04e6df32014-10-11 16:16:27 +02001594Watch a file descriptor for read events
Victor Stinnera092a612015-01-09 15:58:41 +01001595^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001596
1597Wait until a file descriptor received some data using the
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001598:meth:`loop.add_reader` method and then close the event loop::
Victor Stinner04e6df32014-10-11 16:16:27 +02001599
1600 import asyncio
Victor Stinnerac577d72017-11-28 21:33:20 +01001601 from socket import socketpair
Victor Stinner04e6df32014-10-11 16:16:27 +02001602
1603 # Create a pair of connected file descriptors
Victor Stinnerccd8e342014-10-11 16:30:02 +02001604 rsock, wsock = socketpair()
Carol Willing5b7cbd62018-09-12 17:05:17 -07001605
Victor Stinner04e6df32014-10-11 16:16:27 +02001606 loop = asyncio.get_event_loop()
1607
1608 def reader():
1609 data = rsock.recv(100)
1610 print("Received:", data.decode())
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001611
Victor Stinner2cef3002014-10-23 22:38:46 +02001612 # We are done: unregister the file descriptor
Victor Stinner04e6df32014-10-11 16:16:27 +02001613 loop.remove_reader(rsock)
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001614
Victor Stinner04e6df32014-10-11 16:16:27 +02001615 # Stop the event loop
1616 loop.stop()
1617
Victor Stinner2cef3002014-10-23 22:38:46 +02001618 # Register the file descriptor for read event
Victor Stinner04e6df32014-10-11 16:16:27 +02001619 loop.add_reader(rsock, reader)
1620
1621 # Simulate the reception of data from the network
1622 loop.call_soon(wsock.send, 'abc'.encode())
1623
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001624 try:
1625 # Run the event loop
1626 loop.run_forever()
1627 finally:
Carol Willing5b7cbd62018-09-12 17:05:17 -07001628 # We are done. Close sockets and the event loop.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001629 rsock.close()
1630 wsock.close()
1631 loop.close()
Victor Stinner04e6df32014-10-11 16:16:27 +02001632
1633.. seealso::
1634
Yury Selivanov394374e2018-09-17 15:35:24 -04001635 * A similar :ref:`example <asyncio_example_create_connection>`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001636 using transports, protocols, and the
1637 :meth:`loop.create_connection` method.
Victor Stinner04e6df32014-10-11 16:16:27 +02001638
Yury Selivanov394374e2018-09-17 15:35:24 -04001639 * Another similar :ref:`example <asyncio_example_create_connection-streams>`
Yury Selivanov6758e6e2019-09-29 21:59:55 -07001640 using the high-level :func:`asyncio.open_connection` function
1641 and streams.
Victor Stinner04e6df32014-10-11 16:16:27 +02001642
1643
Yury Selivanov394374e2018-09-17 15:35:24 -04001644.. _asyncio_example_unix_signals:
1645
Victor Stinner04e6df32014-10-11 16:16:27 +02001646Set signal handlers for SIGINT and SIGTERM
Victor Stinnera092a612015-01-09 15:58:41 +01001647^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001648
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001649(This ``signals`` example only works on Unix.)
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001650
1651Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM`
1652using the :meth:`loop.add_signal_handler` method::
Victor Stinner8b863482014-01-27 10:07:50 +01001653
1654 import asyncio
1655 import functools
1656 import os
1657 import signal
1658
Alexander Vasinceb842e2019-05-03 18:25:36 +03001659 def ask_exit(signame, loop):
Victor Stinner8b863482014-01-27 10:07:50 +01001660 print("got signal %s: exit" % signame)
1661 loop.stop()
1662
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001663 async def main():
1664 loop = asyncio.get_running_loop()
Victor Stinner8b863482014-01-27 10:07:50 +01001665
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001666 for signame in {'SIGINT', 'SIGTERM'}:
1667 loop.add_signal_handler(
1668 getattr(signal, signame),
Alexander Vasinceb842e2019-05-03 18:25:36 +03001669 functools.partial(ask_exit, signame, loop))
Victor Stinner2cef3002014-10-23 22:38:46 +02001670
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001671 await asyncio.sleep(3600)
1672
1673 print("Event loop running for 1 hour, press Ctrl+C to interrupt.")
1674 print(f"pid {os.getpid()}: send SIGINT or SIGTERM to exit.")
1675
1676 asyncio.run(main())