blob: 4ea8521eb0bf3614bb73512749390a5f57421e25 [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
Miss Islington (bot)6bfeb812019-10-10 16:25:28 -07008**Source code:** :source:`Lib/asyncio/events.py`,
9:source:`Lib/asyncio/base_events.py`
10
11------------------------------------
Yury Selivanov7c7605f2018-09-11 09:54:40 -070012
13.. rubric:: Preface
14
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040015The event loop is the core of every asyncio application.
Yury Selivanov7c7605f2018-09-11 09:54:40 -070016Event loops run asynchronous tasks and callbacks, perform network
Carol Willing5b7cbd62018-09-12 17:05:17 -070017IO operations, and run subprocesses.
Yury Selivanov7c7605f2018-09-11 09:54:40 -070018
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040019Application developers should typically use the high-level asyncio functions,
20such as :func:`asyncio.run`, and should rarely need to reference the loop
21object or call its methods. This section is intended mostly for authors
22of lower-level code, libraries, and frameworks, who need finer control over
23the event loop behavior.
Yury Selivanov7c7605f2018-09-11 09:54:40 -070024
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040025.. rubric:: Obtaining the Event Loop
Yury Selivanov7c7605f2018-09-11 09:54:40 -070026
27The following low-level functions can be used to get, set, or create
28an event loop:
29
30.. function:: get_running_loop()
31
32 Return the running event loop in the current OS thread.
33
34 If there is no running event loop a :exc:`RuntimeError` is raised.
35 This function can only be called from a coroutine or a callback.
36
37 .. versionadded:: 3.7
38
39.. function:: get_event_loop()
40
Miss Islington (bot)6aeed012020-01-20 14:52:35 -080041 Get the current event loop.
42
43 If there is no current event loop set in the current OS thread,
44 the OS thread is main, and :func:`set_event_loop` has not yet
Yury Selivanov7c7605f2018-09-11 09:54:40 -070045 been called, asyncio will create a new event loop and set it as the
46 current one.
47
Carol Willing5b7cbd62018-09-12 17:05:17 -070048 Because this function has rather complex behavior (especially
49 when custom event loop policies are in use), using the
50 :func:`get_running_loop` function is preferred to :func:`get_event_loop`
51 in coroutines and callbacks.
Yury Selivanov7c7605f2018-09-11 09:54:40 -070052
Carol Willing5b7cbd62018-09-12 17:05:17 -070053 Consider also using the :func:`asyncio.run` function instead of using
54 lower level functions to manually create and close an event loop.
Yury Selivanov7c7605f2018-09-11 09:54:40 -070055
56.. function:: set_event_loop(loop)
57
58 Set *loop* as a current event loop for the current OS thread.
59
60.. function:: new_event_loop()
61
62 Create a new event loop object.
63
64Note that the behaviour of :func:`get_event_loop`, :func:`set_event_loop`,
65and :func:`new_event_loop` functions can be altered by
66:ref:`setting a custom event loop policy <asyncio-policies>`.
67
68
69.. rubric:: Contents
70
71This documentation page contains the following sections:
72
Carol Willing5b7cbd62018-09-12 17:05:17 -070073* The `Event Loop Methods`_ section is the reference documentation of
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040074 the event loop APIs;
Yury Selivanov7c7605f2018-09-11 09:54:40 -070075
76* The `Callback Handles`_ section documents the :class:`Handle` and
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040077 :class:`TimerHandle` instances which are returned from scheduling
78 methods such as :meth:`loop.call_soon` and :meth:`loop.call_later`;
Yury Selivanov7c7605f2018-09-11 09:54:40 -070079
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040080* The `Server Objects`_ section documents types returned from
Yury Selivanov7c7605f2018-09-11 09:54:40 -070081 event loop methods like :meth:`loop.create_server`;
82
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040083* The `Event Loop Implementations`_ section documents the
Yury Selivanov7c7605f2018-09-11 09:54:40 -070084 :class:`SelectorEventLoop` and :class:`ProactorEventLoop` classes;
85
86* The `Examples`_ section showcases how to work with some event
87 loop APIs.
88
89
Victor Stinner9592edb2014-02-02 15:03:02 +010090.. _asyncio-event-loop:
Victor Stinnerea3183f2013-12-03 01:08:00 +010091
Yury Selivanov7c7605f2018-09-11 09:54:40 -070092Event Loop Methods
93==================
Victor Stinnerea3183f2013-12-03 01:08:00 +010094
Carol Willing5b7cbd62018-09-12 17:05:17 -070095Event loops have **low-level** APIs for the following:
lf627d2c82017-07-25 17:03:51 -060096
Yury Selivanov7c7605f2018-09-11 09:54:40 -070097.. contents::
98 :depth: 1
99 :local:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100100
Victor Stinnerea3183f2013-12-03 01:08:00 +0100101
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700102Running and stopping the loop
103^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100104
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700105.. method:: loop.run_until_complete(future)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100106
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400107 Run until the *future* (an instance of :class:`Future`) has
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700108 completed.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100109
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700110 If the argument is a :ref:`coroutine object <coroutine>` it
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400111 is implicitly scheduled to run as a :class:`asyncio.Task`.
Eli Bendersky136fea22014-02-09 06:55:58 -0800112
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700113 Return the Future's result or raise its exception.
Guido van Rossumf68afd82016-08-08 09:41:21 -0700114
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700115.. method:: loop.run_forever()
Guido van Rossumf68afd82016-08-08 09:41:21 -0700116
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700117 Run the event loop until :meth:`stop` is called.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100118
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700119 If :meth:`stop` is called before :meth:`run_forever()` is called,
120 the loop will poll the I/O selector once with a timeout of zero,
121 run all callbacks scheduled in response to I/O events (and
122 those that were already scheduled), and then exit.
Victor Stinner83704962015-02-25 14:24:15 +0100123
Guido van Rossum41f69f42015-11-19 13:28:47 -0800124 If :meth:`stop` is called while :meth:`run_forever` is running,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700125 the loop will run the current batch of callbacks and then exit.
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400126 Note that new callbacks scheduled by callbacks will not run in this
Carol Willing5b7cbd62018-09-12 17:05:17 -0700127 case; instead, they will run the next time :meth:`run_forever` or
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700128 :meth:`run_until_complete` is called.
Guido van Rossum41f69f42015-11-19 13:28:47 -0800129
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700130.. method:: loop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100131
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700132 Stop the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100133
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700134.. method:: loop.is_running()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100135
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700136 Return ``True`` if the event loop is currently running.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100137
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700138.. method:: loop.is_closed()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100139
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700140 Return ``True`` if the event loop was closed.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100141
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700142.. method:: loop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100143
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700144 Close the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100145
Andriy Maletskyb83d9172018-10-29 23:39:21 +0200146 The loop must not be running when this function is called.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700147 Any pending callbacks will be discarded.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100148
Łukasz Langa7f9a2ae2019-06-04 13:03:20 +0200149 This method clears all queues and shuts down the executor, but does
150 not wait for the executor to finish.
Guido van Rossum41f69f42015-11-19 13:28:47 -0800151
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700152 This method is idempotent and irreversible. No other methods
153 should be called after the event loop is closed.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100154
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700155.. coroutinemethod:: loop.shutdown_asyncgens()
Yury Selivanov03660042016-12-15 17:36:05 -0500156
157 Schedule all currently open :term:`asynchronous generator` objects to
158 close with an :meth:`~agen.aclose()` call. After calling this method,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700159 the event loop will issue a warning if a new asynchronous generator
Carol Willing5b7cbd62018-09-12 17:05:17 -0700160 is iterated. This should be used to reliably finalize all scheduled
Yury Selivanovac94e382018-09-17 23:58:00 -0400161 asynchronous generators.
Carol Willing5b7cbd62018-09-12 17:05:17 -0700162
Yury Selivanovac94e382018-09-17 23:58:00 -0400163 Note that there is no need to call this function when
164 :func:`asyncio.run` is used.
165
166 Example::
Yury Selivanov03660042016-12-15 17:36:05 -0500167
168 try:
169 loop.run_forever()
170 finally:
171 loop.run_until_complete(loop.shutdown_asyncgens())
172 loop.close()
173
174 .. versionadded:: 3.6
175
176
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700177Scheduling callbacks
178^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100179
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700180.. method:: loop.call_soon(callback, *args, context=None)
Victor Stinner8464c242014-11-28 13:15:41 +0100181
Carol Willing5b7cbd62018-09-12 17:05:17 -0700182 Schedule a *callback* to be called with *args* arguments at
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700183 the next iteration of the event loop.
Victor Stinner8464c242014-11-28 13:15:41 +0100184
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700185 Callbacks are called in the order in which they are registered.
186 Each callback will be called exactly once.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100187
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700188 An optional keyword-only *context* argument allows specifying a
189 custom :class:`contextvars.Context` for the *callback* to run in.
190 The current context is used when no *context* is provided.
Yury Selivanov28b91782018-05-23 13:35:04 -0400191
Yury Selivanov1096f762015-06-25 13:49:52 -0400192 An instance of :class:`asyncio.Handle` is returned, which can be
Carol Willing5b7cbd62018-09-12 17:05:17 -0700193 used later to cancel the callback.
194
195 This method is not thread-safe.
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500196
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700197.. method:: loop.call_soon_threadsafe(callback, *args, context=None)
Victor Stinner8464c242014-11-28 13:15:41 +0100198
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700199 A thread-safe variant of :meth:`call_soon`. Must be used to
200 schedule callbacks *from another thread*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100201
Victor Stinner83704962015-02-25 14:24:15 +0100202 See the :ref:`concurrency and multithreading <asyncio-multithreading>`
203 section of the documentation.
204
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700205.. versionchanged:: 3.7
206 The *context* keyword-only parameter was added. See :pep:`567`
207 for more details.
208
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400209.. _asyncio-pass-keywords:
210
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700211.. note::
212
Carol Willing5b7cbd62018-09-12 17:05:17 -0700213 Most :mod:`asyncio` scheduling functions don't allow passing
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400214 keyword arguments. To do that, use :func:`functools.partial`::
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700215
Carol Willing5b7cbd62018-09-12 17:05:17 -0700216 # will schedule "print("Hello", flush=True)"
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700217 loop.call_soon(
218 functools.partial(print, "Hello", flush=True))
219
220 Using partial objects is usually more convenient than using lambdas,
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400221 as asyncio can render partial objects better in debug and error
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700222 messages.
Yury Selivanov28b91782018-05-23 13:35:04 -0400223
Victor Stinnerea3183f2013-12-03 01:08:00 +0100224
Victor Stinner45b27ed2014-02-01 02:36:43 +0100225.. _asyncio-delayed-calls:
226
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700227Scheduling delayed callbacks
228^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100229
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700230Event loop provides mechanisms to schedule callback functions
231to be called at some point in the future. Event loop uses monotonic
232clocks to track time.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100233
Victor Stinner45b27ed2014-02-01 02:36:43 +0100234
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700235.. method:: loop.call_later(delay, callback, *args, context=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100236
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700237 Schedule *callback* to be called after the given *delay*
238 number of seconds (can be either an int or a float).
Victor Stinnerea3183f2013-12-03 01:08:00 +0100239
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700240 An instance of :class:`asyncio.TimerHandle` is returned which can
241 be used to cancel the callback.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100242
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700243 *callback* will be called exactly once. If two callbacks are
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400244 scheduled for exactly the same time, the order in which they
245 are called is undefined.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100246
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700247 The optional positional *args* will be passed to the callback when
248 it is called. If you want the callback to be called with keyword
249 arguments use :func:`functools.partial`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100250
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700251 An optional keyword-only *context* argument allows specifying a
252 custom :class:`contextvars.Context` for the *callback* to run in.
253 The current context is used when no *context* is provided.
Victor Stinner8464c242014-11-28 13:15:41 +0100254
Yury Selivanov28b91782018-05-23 13:35:04 -0400255 .. versionchanged:: 3.7
256 The *context* keyword-only parameter was added. See :pep:`567`
257 for more details.
258
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400259 .. versionchanged:: 3.8
260 In Python 3.7 and earlier with the default event loop implementation,
261 the *delay* could not exceed one day.
262 This has been fixed in Python 3.8.
263
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700264.. method:: loop.call_at(when, callback, *args, context=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100265
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700266 Schedule *callback* to be called at the given absolute timestamp
267 *when* (an int or a float), using the same time reference as
268 :meth:`loop.time`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100269
270 This method's behavior is the same as :meth:`call_later`.
271
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700272 An instance of :class:`asyncio.TimerHandle` is returned which can
273 be used to cancel the callback.
Victor Stinner8464c242014-11-28 13:15:41 +0100274
Yury Selivanov28b91782018-05-23 13:35:04 -0400275 .. versionchanged:: 3.7
276 The *context* keyword-only parameter was added. See :pep:`567`
277 for more details.
278
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400279 .. versionchanged:: 3.8
280 In Python 3.7 and earlier with the default event loop implementation,
281 the difference between *when* and the current time could not exceed
282 one day. This has been fixed in Python 3.8.
283
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700284.. method:: loop.time()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100285
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700286 Return the current time, as a :class:`float` value, according to
287 the event loop's internal monotonic clock.
288
289.. note::
Enrico Alarico Carbognani7e954e72019-04-18 14:43:14 +0200290 .. versionchanged:: 3.8
291 In Python 3.7 and earlier timeouts (relative *delay* or absolute *when*)
292 should not exceed one day. This has been fixed in Python 3.8.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100293
Victor Stinner3e09e322013-12-03 01:22:06 +0100294.. seealso::
295
296 The :func:`asyncio.sleep` function.
297
Victor Stinnerea3183f2013-12-03 01:08:00 +0100298
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700299Creating Futures and Tasks
300^^^^^^^^^^^^^^^^^^^^^^^^^^
Yury Selivanov950204d2016-05-16 16:23:00 -0400301
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700302.. method:: loop.create_future()
Yury Selivanov950204d2016-05-16 16:23:00 -0400303
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700304 Create an :class:`asyncio.Future` object attached to the event loop.
Yury Selivanov950204d2016-05-16 16:23:00 -0400305
Carol Willing5b7cbd62018-09-12 17:05:17 -0700306 This is the preferred way to create Futures in asyncio. This lets
307 third-party event loops provide alternative implementations of
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700308 the Future object (with better performance or instrumentation).
Yury Selivanov950204d2016-05-16 16:23:00 -0400309
310 .. versionadded:: 3.5.2
311
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700312.. method:: loop.create_task(coro, \*, name=None)
Yury Selivanov950204d2016-05-16 16:23:00 -0400313
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700314 Schedule the execution of a :ref:`coroutine`.
315 Return a :class:`Task` object.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200316
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700317 Third-party event loops can use their own subclass of :class:`Task`
318 for interoperability. In this case, the result type is a subclass
319 of :class:`Task`.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200320
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700321 If the *name* argument is provided and not ``None``, it is set as
322 the name of the task using :meth:`Task.set_name`.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200323
Alex Grönholmcca4eec2018-08-09 00:06:47 +0300324 .. versionchanged:: 3.8
325 Added the ``name`` parameter.
326
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700327.. method:: loop.set_task_factory(factory)
Yury Selivanov71854612015-05-11 16:28:27 -0400328
329 Set a task factory that will be used by
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700330 :meth:`loop.create_task`.
Yury Selivanov71854612015-05-11 16:28:27 -0400331
332 If *factory* is ``None`` the default task factory will be set.
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400333 Otherwise, *factory* must be a *callable* with the signature matching
334 ``(loop, coro)``, where *loop* is a reference to the active
335 event loop, and *coro* is a coroutine object. The callable
336 must return a :class:`asyncio.Future`-compatible object.
Yury Selivanov71854612015-05-11 16:28:27 -0400337
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700338.. method:: loop.get_task_factory()
Yury Selivanov71854612015-05-11 16:28:27 -0400339
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700340 Return a task factory or ``None`` if the default one is in use.
Yury Selivanov71854612015-05-11 16:28:27 -0400341
Victor Stinner530ef2f2014-07-08 12:39:10 +0200342
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700343Opening network connections
344^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100345
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700346.. coroutinemethod:: loop.create_connection(protocol_factory, \
347 host=None, port=None, \*, ssl=None, \
348 family=0, proto=0, flags=0, sock=None, \
349 local_addr=None, server_hostname=None, \
Miss Islington (bot)af95d792020-02-10 01:54:38 -0800350 ssl_handshake_timeout=None, \
351 happy_eyeballs_delay=None, interleave=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100352
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700353 Open a streaming transport connection to a given
354 address specified by *host* and *port*.
355
356 The socket family can be either :py:data:`~socket.AF_INET` or
357 :py:data:`~socket.AF_INET6` depending on *host* (or the *family*
358 argument, if provided).
359
360 The socket type will be :py:data:`~socket.SOCK_STREAM`.
361
362 *protocol_factory* must be a callable returning an
363 :ref:`asyncio protocol <asyncio-protocol>` implementation.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100364
Yury Selivanov19a44f62017-12-14 20:53:26 -0500365 This method will try to establish the connection in the background.
366 When successful, it returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100367
368 The chronological synopsis of the underlying operation is as follows:
369
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700370 #. The connection is established and a :ref:`transport <asyncio-transport>`
371 is created for it.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100372
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700373 #. *protocol_factory* is called without arguments and is expected to
374 return a :ref:`protocol <asyncio-protocol>` instance.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100375
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700376 #. The protocol instance is coupled with the transport by calling its
377 :meth:`~BaseProtocol.connection_made` method.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100378
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700379 #. A ``(transport, protocol)`` tuple is returned on success.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100380
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700381 The created transport is an implementation-dependent bidirectional
382 stream.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100383
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700384 Other arguments:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100385
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400386 * *ssl*: if given and not false, a SSL/TLS transport is created
Victor Stinnerea3183f2013-12-03 01:08:00 +0100387 (by default a plain TCP transport is created). If *ssl* is
388 a :class:`ssl.SSLContext` object, this context is used to create
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400389 the transport; if *ssl* is :const:`True`, a default context returned
390 from :func:`ssl.create_default_context` is used.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100391
Berker Peksag9c1dba22014-09-28 00:00:58 +0300392 .. seealso:: :ref:`SSL/TLS security considerations <ssl-security>`
Antoine Pitrouc5e075f2014-03-22 18:19:11 +0100393
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400394 * *server_hostname* sets or overrides the hostname that the target
395 server's certificate will be matched against. Should only be passed
396 if *ssl* is not ``None``. By default the value of the *host* argument
Victor Stinnerea3183f2013-12-03 01:08:00 +0100397 is used. If *host* is empty, there is no default and you must pass a
398 value for *server_hostname*. If *server_hostname* is an empty
399 string, hostname matching is disabled (which is a serious security
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400400 risk, allowing for potential man-in-the-middle attacks).
Victor Stinnerea3183f2013-12-03 01:08:00 +0100401
402 * *family*, *proto*, *flags* are the optional address family, protocol
403 and flags to be passed through to getaddrinfo() for *host* resolution.
404 If given, these should all be integers from the corresponding
405 :mod:`socket` module constants.
406
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800407 * *happy_eyeballs_delay*, if given, enables Happy Eyeballs for this
408 connection. It should
409 be a floating-point number representing the amount of time in seconds
410 to wait for a connection attempt to complete, before starting the next
411 attempt in parallel. This is the "Connection Attempt Delay" as defined
412 in :rfc:`8305`. A sensible default value recommended by the RFC is ``0.25``
413 (250 milliseconds).
414
415 * *interleave* controls address reordering when a host name resolves to
416 multiple IP addresses.
417 If ``0`` or unspecified, no reordering is done, and addresses are
418 tried in the order returned by :meth:`getaddrinfo`. If a positive integer
419 is specified, the addresses are interleaved by address family, and the
420 given integer is interpreted as "First Address Family Count" as defined
421 in :rfc:`8305`. The default is ``0`` if *happy_eyeballs_delay* is not
422 specified, and ``1`` if it is.
423
Victor Stinnerea3183f2013-12-03 01:08:00 +0100424 * *sock*, if given, should be an existing, already connected
425 :class:`socket.socket` object to be used by the transport.
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800426 If *sock* is given, none of *host*, *port*, *family*, *proto*, *flags*,
427 *happy_eyeballs_delay*, *interleave*
Victor Stinnerea3183f2013-12-03 01:08:00 +0100428 and *local_addr* should be specified.
429
430 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
431 to bind the socket to locally. The *local_host* and *local_port*
Carol Willing5b7cbd62018-09-12 17:05:17 -0700432 are looked up using ``getaddrinfo()``, similarly to *host* and *port*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100433
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400434 * *ssl_handshake_timeout* is (for a TLS connection) the time in seconds
435 to wait for the TLS handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400436 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000437
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800438 .. versionadded:: 3.8
439
Miss Islington (bot)af95d792020-02-10 01:54:38 -0800440 Added the *happy_eyeballs_delay* and *interleave* parameters.
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800441
Neil Aspinallf7686c12017-12-19 19:45:42 +0000442 .. versionadded:: 3.7
443
444 The *ssl_handshake_timeout* parameter.
445
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700446 .. versionchanged:: 3.6
447
448 The socket option :py:data:`~socket.TCP_NODELAY` is set by default
449 for all TCP connections.
450
Victor Stinner60208a12015-09-15 22:41:52 +0200451 .. versionchanged:: 3.5
452
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400453 Added support for SSL/TLS in :class:`ProactorEventLoop`.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200454
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100455 .. seealso::
456
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700457 The :func:`open_connection` function is a high-level alternative
458 API. It returns a pair of (:class:`StreamReader`, :class:`StreamWriter`)
459 that can be used directly in async/await code.
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100460
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700461.. coroutinemethod:: loop.create_datagram_endpoint(protocol_factory, \
462 local_addr=None, remote_addr=None, \*, \
463 family=0, proto=0, flags=0, \
464 reuse_address=None, reuse_port=None, \
465 allow_broadcast=None, sock=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100466
Miss Islington (bot)79c29742019-12-09 06:39:54 -0800467 .. note::
468 The parameter *reuse_address* is no longer supported, as using
469 :py:data:`~sockets.SO_REUSEADDR` poses a significant security concern for
470 UDP. Explicitly passing ``reuse_address=True`` will raise an exception.
471
472 When multiple processes with differing UIDs assign sockets to an
Miss Islington (bot)7eb8c6d2019-12-23 07:52:29 -0800473 identical UDP socket address with ``SO_REUSEADDR``, incoming packets can
Miss Islington (bot)79c29742019-12-09 06:39:54 -0800474 become randomly distributed among the sockets.
475
476 For supported platforms, *reuse_port* can be used as a replacement for
477 similar functionality. With *reuse_port*,
478 :py:data:`~sockets.SO_REUSEPORT` is used instead, which specifically
479 prevents processes with differing UIDs from assigning sockets to the same
480 socket address.
481
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700482 Create a datagram connection.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100483
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700484 The socket family can be either :py:data:`~socket.AF_INET`,
485 :py:data:`~socket.AF_INET6`, or :py:data:`~socket.AF_UNIX`,
486 depending on *host* (or the *family* argument, if provided).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100487
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700488 The socket type will be :py:data:`~socket.SOCK_DGRAM`.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100489
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700490 *protocol_factory* must be a callable returning a
491 :ref:`protocol <asyncio-protocol>` implementation.
492
493 A tuple of ``(transport, protocol)`` is returned on success.
494
495 Other arguments:
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700496
497 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
498 to bind the socket to locally. The *local_host* and *local_port*
499 are looked up using :meth:`getaddrinfo`.
500
501 * *remote_addr*, if given, is a ``(remote_host, remote_port)`` tuple used
502 to connect the socket to a remote address. The *remote_host* and
503 *remote_port* are looked up using :meth:`getaddrinfo`.
504
505 * *family*, *proto*, *flags* are the optional address family, protocol
506 and flags to be passed through to :meth:`getaddrinfo` for *host*
507 resolution. If given, these should all be integers from the
508 corresponding :mod:`socket` module constants.
509
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700510 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
511 same port as other existing endpoints are bound to, so long as they all
512 set this flag when being created. This option is not supported on Windows
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400513 and some Unixes. If the :py:data:`~socket.SO_REUSEPORT` constant is not
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700514 defined then this capability is unsupported.
515
516 * *allow_broadcast* tells the kernel to allow this endpoint to send
517 messages to the broadcast address.
518
519 * *sock* can optionally be specified in order to use a preexisting,
520 already connected, :class:`socket.socket` object to be used by the
521 transport. If specified, *local_addr* and *remote_addr* should be omitted
522 (must be :const:`None`).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100523
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200524 See :ref:`UDP echo client protocol <asyncio-udp-echo-client-protocol>` and
525 :ref:`UDP echo server protocol <asyncio-udp-echo-server-protocol>` examples.
526
Romuald Brunet0ded5802018-05-14 18:22:00 +0200527 .. versionchanged:: 3.4.4
528 The *family*, *proto*, *flags*, *reuse_address*, *reuse_port,
529 *allow_broadcast*, and *sock* parameters were added.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100530
Miss Islington (bot)79c29742019-12-09 06:39:54 -0800531 .. versionchanged:: 3.8.1
532 The *reuse_address* parameter is no longer supported due to security
533 concerns.
534
Andrew Svetlovbafd4b52019-05-28 12:52:15 +0300535 .. versionchanged:: 3.8
536 Added support for Windows.
537
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700538.. coroutinemethod:: loop.create_unix_connection(protocol_factory, \
539 path=None, \*, ssl=None, sock=None, \
540 server_hostname=None, ssl_handshake_timeout=None)
Victor Stinnera6919aa2014-02-19 13:32:34 +0100541
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400542 Create a Unix connection.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100543
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700544 The socket family will be :py:data:`~socket.AF_UNIX`; socket
545 type will be :py:data:`~socket.SOCK_STREAM`.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100546
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700547 A tuple of ``(transport, protocol)`` is returned on success.
Guido van Rossum9e80eeb2016-11-03 14:17:25 -0700548
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400549 *path* is the name of a Unix domain socket and is required,
550 unless a *sock* parameter is specified. Abstract Unix sockets,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700551 :class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths are
552 supported.
553
554 See the documentation of the :meth:`loop.create_connection` method
555 for information about arguments to this method.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100556
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400557 .. availability:: Unix.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100558
Neil Aspinallf7686c12017-12-19 19:45:42 +0000559 .. versionadded:: 3.7
560
561 The *ssl_handshake_timeout* parameter.
562
Yury Selivanov423fd362017-11-20 17:26:28 -0500563 .. versionchanged:: 3.7
564
Elvis Pranskevichusc0d062f2018-06-08 11:36:00 -0400565 The *path* parameter can now be a :term:`path-like object`.
Yury Selivanov423fd362017-11-20 17:26:28 -0500566
Victor Stinnera6919aa2014-02-19 13:32:34 +0100567
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700568Creating network servers
569^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100570
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700571.. coroutinemethod:: loop.create_server(protocol_factory, \
572 host=None, port=None, \*, \
573 family=socket.AF_UNSPEC, \
574 flags=socket.AI_PASSIVE, \
575 sock=None, backlog=100, ssl=None, \
576 reuse_address=None, reuse_port=None, \
577 ssl_handshake_timeout=None, start_serving=True)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100578
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700579 Create a TCP server (socket type :data:`~socket.SOCK_STREAM`) listening
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400580 on *port* of the *host* address.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200581
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700582 Returns a :class:`Server` object.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200583
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700584 Arguments:
Victor Stinner33f6abe2014-10-12 20:36:04 +0200585
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400586 * *protocol_factory* must be a callable returning a
587 :ref:`protocol <asyncio-protocol>` implementation.
588
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400589 * The *host* parameter can be set to several types which determine where
590 the server would be listening:
591
592 - If *host* is a string, the TCP server is bound to a single network
593 interface specified by *host*.
594
595 - If *host* is a sequence of strings, the TCP server is bound to all
596 network interfaces specified by the sequence.
597
598 - If *host* is an empty string or ``None``, all interfaces are
599 assumed and a list of multiple sockets will be returned (most likely
600 one for IPv4 and another one for IPv6).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200601
602 * *family* can be set to either :data:`socket.AF_INET` or
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700603 :data:`~socket.AF_INET6` to force the socket to use IPv4 or IPv6.
Carol Willing5b7cbd62018-09-12 17:05:17 -0700604 If not set, the *family* will be determined from host name
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700605 (defaults to :data:`~socket.AF_UNSPEC`).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200606
607 * *flags* is a bitmask for :meth:`getaddrinfo`.
608
609 * *sock* can optionally be specified in order to use a preexisting
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400610 socket object. If specified, *host* and *port* must not be specified.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200611
612 * *backlog* is the maximum number of queued connections passed to
613 :meth:`~socket.socket.listen` (defaults to 100).
614
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400615 * *ssl* can be set to an :class:`~ssl.SSLContext` instance to enable
616 TLS over the accepted connections.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200617
618 * *reuse_address* tells the kernel to reuse a local socket in
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700619 ``TIME_WAIT`` state, without waiting for its natural timeout to
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300620 expire. If not specified will automatically be set to ``True`` on
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400621 Unix.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100622
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700623 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
624 same port as other existing endpoints are bound to, so long as they all
625 set this flag when being created. This option is not supported on
626 Windows.
627
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400628 * *ssl_handshake_timeout* is (for a TLS server) the time in seconds to wait
629 for the TLS handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400630 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000631
Yury Selivanovc9070d02018-01-25 18:08:09 -0500632 * *start_serving* set to ``True`` (the default) causes the created server
633 to start accepting connections immediately. When set to ``False``,
634 the user should await on :meth:`Server.start_serving` or
635 :meth:`Server.serve_forever` to make the server to start accepting
636 connections.
637
Neil Aspinallf7686c12017-12-19 19:45:42 +0000638 .. versionadded:: 3.7
639
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700640 Added *ssl_handshake_timeout* and *start_serving* parameters.
641
642 .. versionchanged:: 3.6
643
644 The socket option :py:data:`~socket.TCP_NODELAY` is set by default
645 for all TCP connections.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000646
Victor Stinner60208a12015-09-15 22:41:52 +0200647 .. versionchanged:: 3.5
648
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400649 Added support for SSL/TLS in :class:`ProactorEventLoop`.
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100650
Victor Stinner7b58a2b2015-09-21 18:41:05 +0200651 .. versionchanged:: 3.5.1
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200652
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700653 The *host* parameter can be a sequence of strings.
654
655 .. seealso::
656
657 The :func:`start_server` function is a higher-level alternative API
658 that returns a pair of :class:`StreamReader` and :class:`StreamWriter`
659 that can be used in an async/await code.
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200660
Victor Stinnerea3183f2013-12-03 01:08:00 +0100661
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700662.. coroutinemethod:: loop.create_unix_server(protocol_factory, path=None, \
663 \*, sock=None, backlog=100, ssl=None, \
664 ssl_handshake_timeout=None, start_serving=True)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100665
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700666 Similar to :meth:`loop.create_server` but works with the
667 :py:data:`~socket.AF_UNIX` socket family.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100668
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400669 *path* is the name of a Unix domain socket, and is required,
670 unless a *sock* argument is provided. Abstract Unix sockets,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700671 :class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths
672 are supported.
Yury Selivanov423fd362017-11-20 17:26:28 -0500673
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400674 See the documentation of the :meth:`loop.create_server` method
675 for information about arguments to this method.
676
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400677 .. availability:: Unix.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100678
Neil Aspinallf7686c12017-12-19 19:45:42 +0000679 .. versionadded:: 3.7
680
Elvis Pranskevichusc0d062f2018-06-08 11:36:00 -0400681 The *ssl_handshake_timeout* and *start_serving* parameters.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000682
Yury Selivanov423fd362017-11-20 17:26:28 -0500683 .. versionchanged:: 3.7
684
685 The *path* parameter can now be a :class:`~pathlib.Path` object.
686
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700687.. coroutinemethod:: loop.connect_accepted_socket(protocol_factory, \
688 sock, \*, ssl=None, ssl_handshake_timeout=None)
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500689
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700690 Wrap an already accepted connection into a transport/protocol pair.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500691
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700692 This method can be used by servers that accept connections outside
693 of asyncio but that use asyncio to handle them.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500694
695 Parameters:
696
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400697 * *protocol_factory* must be a callable returning a
698 :ref:`protocol <asyncio-protocol>` implementation.
699
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700700 * *sock* is a preexisting socket object returned from
701 :meth:`socket.accept <socket.socket.accept>`.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500702
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700703 * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over
704 the accepted connections.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500705
Neil Aspinallf7686c12017-12-19 19:45:42 +0000706 * *ssl_handshake_timeout* is (for an SSL connection) the time in seconds to
707 wait for the SSL handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400708 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000709
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700710 Returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100711
Neil Aspinallf7686c12017-12-19 19:45:42 +0000712 .. versionadded:: 3.7
713
714 The *ssl_handshake_timeout* parameter.
715
AraHaan431665b2017-11-21 11:06:26 -0500716 .. versionadded:: 3.5.3
717
718
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700719Transferring files
720^^^^^^^^^^^^^^^^^^
Andrew Svetlov7c684072018-01-27 21:22:47 +0200721
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700722.. coroutinemethod:: loop.sendfile(transport, file, \
723 offset=0, count=None, *, fallback=True)
Andrew Svetlov7c684072018-01-27 21:22:47 +0200724
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700725 Send a *file* over a *transport*. Return the total number of bytes
726 sent.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200727
728 The method uses high-performance :meth:`os.sendfile` if available.
729
730 *file* must be a regular file object opened in binary mode.
731
732 *offset* tells from where to start reading the file. If specified,
733 *count* is the total number of bytes to transmit as opposed to
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400734 sending the file until EOF is reached. File position is always updated,
735 even when this method raises an error, and
736 :meth:`file.tell() <io.IOBase.tell>` can be used to obtain the actual
737 number of bytes sent.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200738
739 *fallback* set to ``True`` makes asyncio to manually read and send
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700740 the file when the platform does not support the sendfile system call
Andrew Svetlov7c684072018-01-27 21:22:47 +0200741 (e.g. Windows or SSL socket on Unix).
742
743 Raise :exc:`SendfileNotAvailableError` if the system does not support
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400744 the *sendfile* syscall and *fallback* is ``False``.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200745
746 .. versionadded:: 3.7
747
748
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500749TLS Upgrade
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700750^^^^^^^^^^^
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500751
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700752.. coroutinemethod:: loop.start_tls(transport, protocol, \
753 sslcontext, \*, server_side=False, \
754 server_hostname=None, ssl_handshake_timeout=None)
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500755
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700756 Upgrade an existing transport-based connection to TLS.
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500757
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700758 Return a new transport instance, that the *protocol* must start using
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500759 immediately after the *await*. The *transport* instance passed to
760 the *start_tls* method should never be used again.
761
762 Parameters:
763
764 * *transport* and *protocol* instances that methods like
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700765 :meth:`~loop.create_server` and
766 :meth:`~loop.create_connection` return.
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500767
768 * *sslcontext*: a configured instance of :class:`~ssl.SSLContext`.
769
770 * *server_side* pass ``True`` when a server-side connection is being
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700771 upgraded (like the one created by :meth:`~loop.create_server`).
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500772
773 * *server_hostname*: sets or overrides the host name that the target
774 server's certificate will be matched against.
775
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400776 * *ssl_handshake_timeout* is (for a TLS connection) the time in seconds to
777 wait for the TLS handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400778 ``60.0`` seconds if ``None`` (default).
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500779
780 .. versionadded:: 3.7
781
782
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700783Watching file descriptors
784^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerc1567df2014-02-08 23:22:58 +0100785
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700786.. method:: loop.add_reader(fd, callback, \*args)
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200787
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400788 Start monitoring the *fd* file descriptor for read availability and
789 invoke *callback* with the specified arguments once *fd* is available for
790 reading.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200791
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700792.. method:: loop.remove_reader(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100793
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400794 Stop monitoring the *fd* file descriptor for read availability.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100795
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700796.. method:: loop.add_writer(fd, callback, \*args)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100797
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400798 Start monitoring the *fd* file descriptor for write availability and
799 invoke *callback* with the specified arguments once *fd* is available for
800 writing.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100801
Yury Selivanove247b462018-09-20 12:43:59 -0400802 Use :func:`functools.partial` :ref:`to pass keyword arguments
Naglis17473342018-12-04 09:31:15 +0200803 <asyncio-pass-keywords>` to *callback*.
Victor Stinner8464c242014-11-28 13:15:41 +0100804
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700805.. method:: loop.remove_writer(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100806
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400807 Stop monitoring the *fd* file descriptor for write availability.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100808
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700809See also :ref:`Platform Support <asyncio-platform-support>` section
810for some limitations of these methods.
Victor Stinner04e6df32014-10-11 16:16:27 +0200811
Victor Stinnerc1567df2014-02-08 23:22:58 +0100812
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700813Working with socket objects directly
814^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerc1567df2014-02-08 23:22:58 +0100815
Carol Willing5b7cbd62018-09-12 17:05:17 -0700816In general, protocol implementations that use transport-based APIs
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700817such as :meth:`loop.create_connection` and :meth:`loop.create_server`
818are faster than implementations that work with sockets directly.
Carol Willing5b7cbd62018-09-12 17:05:17 -0700819However, there are some use cases when performance is not critical, and
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700820working with :class:`~socket.socket` objects directly is more
821convenient.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100822
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700823.. coroutinemethod:: loop.sock_recv(sock, nbytes)
Yury Selivanov55c50842016-06-08 12:48:15 -0400824
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400825 Receive up to *nbytes* from *sock*. Asynchronous version of
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700826 :meth:`socket.recv() <socket.socket.recv>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100827
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400828 Return the received data as a bytes object.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700829
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400830 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200831
Yury Selivanov19a44f62017-12-14 20:53:26 -0500832 .. versionchanged:: 3.7
Carol Willing5b7cbd62018-09-12 17:05:17 -0700833 Even though this method was always documented as a coroutine
834 method, releases before Python 3.7 returned a :class:`Future`.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700835 Since Python 3.7 this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100836
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700837.. coroutinemethod:: loop.sock_recv_into(sock, buf)
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200838
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400839 Receive data from *sock* into the *buf* buffer. Modeled after the blocking
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700840 :meth:`socket.recv_into() <socket.socket.recv_into>` method.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200841
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700842 Return the number of bytes written to the buffer.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200843
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400844 *sock* must be a non-blocking socket.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200845
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200846 .. versionadded:: 3.7
847
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700848.. coroutinemethod:: loop.sock_sendall(sock, data)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100849
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400850 Send *data* to the *sock* socket. Asynchronous version of
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700851 :meth:`socket.sendall() <socket.socket.sendall>`.
Yury Selivanov55c50842016-06-08 12:48:15 -0400852
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400853 This method continues to send to the socket until either all data
854 in *data* has been sent or an error occurs. ``None`` is returned
Carol Willing5b7cbd62018-09-12 17:05:17 -0700855 on success. On error, an exception is raised. Additionally, there is no way
856 to determine how much data, if any, was successfully processed by the
857 receiving end of the connection.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100858
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400859 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200860
Yury Selivanov19a44f62017-12-14 20:53:26 -0500861 .. versionchanged:: 3.7
862 Even though the method was always documented as a coroutine
863 method, before Python 3.7 it returned an :class:`Future`.
864 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100865
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700866.. coroutinemethod:: loop.sock_connect(sock, address)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100867
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400868 Connect *sock* to a remote socket at *address*.
Victor Stinner1b0580b2014-02-13 09:24:37 +0100869
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700870 Asynchronous version of :meth:`socket.connect() <socket.socket.connect>`.
871
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400872 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200873
Yury Selivanov55c50842016-06-08 12:48:15 -0400874 .. versionchanged:: 3.5.2
875 ``address`` no longer needs to be resolved. ``sock_connect``
876 will try to check if the *address* is already resolved by calling
877 :func:`socket.inet_pton`. If not,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700878 :meth:`loop.getaddrinfo` will be used to resolve the
Yury Selivanov55c50842016-06-08 12:48:15 -0400879 *address*.
880
Victor Stinnerc1567df2014-02-08 23:22:58 +0100881 .. seealso::
882
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700883 :meth:`loop.create_connection`
Yury Selivanov55c50842016-06-08 12:48:15 -0400884 and :func:`asyncio.open_connection() <open_connection>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100885
886
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700887.. coroutinemethod:: loop.sock_accept(sock)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100888
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700889 Accept a connection. Modeled after the blocking
890 :meth:`socket.accept() <socket.socket.accept>` method.
Yury Selivanov55c50842016-06-08 12:48:15 -0400891
892 The socket must be bound to an address and listening
Victor Stinnerc1567df2014-02-08 23:22:58 +0100893 for connections. The return value is a pair ``(conn, address)`` where *conn*
894 is a *new* socket object usable to send and receive data on the connection,
895 and *address* is the address bound to the socket on the other end of the
896 connection.
897
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400898 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200899
Yury Selivanov19a44f62017-12-14 20:53:26 -0500900 .. versionchanged:: 3.7
901 Even though the method was always documented as a coroutine
902 method, before Python 3.7 it returned a :class:`Future`.
903 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100904
905 .. seealso::
906
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700907 :meth:`loop.create_server` and :func:`start_server`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100908
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700909.. coroutinemethod:: loop.sock_sendfile(sock, file, offset=0, count=None, \
910 \*, fallback=True)
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200911
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700912 Send a file using high-performance :mod:`os.sendfile` if possible.
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400913 Return the total number of bytes sent.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200914
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700915 Asynchronous version of :meth:`socket.sendfile() <socket.socket.sendfile>`.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200916
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400917 *sock* must be a non-blocking :const:`socket.SOCK_STREAM`
918 :class:`~socket.socket`.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200919
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400920 *file* must be a regular file object open in binary mode.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200921
922 *offset* tells from where to start reading the file. If specified,
923 *count* is the total number of bytes to transmit as opposed to
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400924 sending the file until EOF is reached. File position is always updated,
925 even when this method raises an error, and
926 :meth:`file.tell() <io.IOBase.tell>` can be used to obtain the actual
927 number of bytes sent.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200928
Carol Willing5b7cbd62018-09-12 17:05:17 -0700929 *fallback*, when set to ``True``, makes asyncio manually read and send
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200930 the file when the platform does not support the sendfile syscall
931 (e.g. Windows or SSL socket on Unix).
932
Andrew Svetlov7464e872018-01-19 20:04:29 +0200933 Raise :exc:`SendfileNotAvailableError` if the system does not support
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200934 *sendfile* syscall and *fallback* is ``False``.
935
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400936 *sock* must be a non-blocking socket.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700937
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200938 .. versionadded:: 3.7
939
Victor Stinnerc1567df2014-02-08 23:22:58 +0100940
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700941DNS
942^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100943
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700944.. coroutinemethod:: loop.getaddrinfo(host, port, \*, family=0, \
945 type=0, proto=0, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100946
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700947 Asynchronous version of :meth:`socket.getaddrinfo`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100948
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700949.. coroutinemethod:: loop.getnameinfo(sockaddr, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100950
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700951 Asynchronous version of :meth:`socket.getnameinfo`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100952
Yury Selivanovbec23722018-01-28 14:09:40 -0500953.. versionchanged:: 3.7
954 Both *getaddrinfo* and *getnameinfo* methods were always documented
955 to return a coroutine, but prior to Python 3.7 they were, in fact,
956 returning :class:`asyncio.Future` objects. Starting with Python 3.7
957 both methods are coroutines.
958
Victor Stinnerea3183f2013-12-03 01:08:00 +0100959
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700960Working with pipes
961^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100962
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700963.. coroutinemethod:: loop.connect_read_pipe(protocol_factory, pipe)
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200964
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400965 Register the read end of *pipe* in the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100966
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700967 *protocol_factory* must be a callable returning an
968 :ref:`asyncio protocol <asyncio-protocol>` implementation.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100969
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700970 *pipe* is a :term:`file-like object <file object>`.
971
972 Return pair ``(transport, protocol)``, where *transport* supports
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400973 the :class:`ReadTransport` interface and *protocol* is an object
974 instantiated by the *protocol_factory*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100975
Victor Stinnerd84fd732014-08-26 01:01:59 +0200976 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
977 non-blocking mode.
978
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700979.. coroutinemethod:: loop.connect_write_pipe(protocol_factory, pipe)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100980
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400981 Register the write end of *pipe* in the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100982
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700983 *protocol_factory* must be a callable returning an
984 :ref:`asyncio protocol <asyncio-protocol>` implementation.
985
986 *pipe* is :term:`file-like object <file object>`.
987
Victor Stinner2cef3002014-10-23 22:38:46 +0200988 Return pair ``(transport, protocol)``, where *transport* supports
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400989 :class:`WriteTransport` interface and *protocol* is an object
990 instantiated by the *protocol_factory*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100991
Victor Stinnerd84fd732014-08-26 01:01:59 +0200992 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
993 non-blocking mode.
994
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700995.. note::
996
997 :class:`SelectorEventLoop` does not support the above methods on
Carol Willing5b7cbd62018-09-12 17:05:17 -0700998 Windows. Use :class:`ProactorEventLoop` instead for Windows.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700999
Victor Stinner08444382014-02-02 22:43:39 +01001000.. seealso::
1001
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001002 The :meth:`loop.subprocess_exec` and
1003 :meth:`loop.subprocess_shell` methods.
Victor Stinner08444382014-02-02 22:43:39 +01001004
Victor Stinnerea3183f2013-12-03 01:08:00 +01001005
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001006Unix signals
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001007^^^^^^^^^^^^
Victor Stinner8b863482014-01-27 10:07:50 +01001008
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001009.. method:: loop.add_signal_handler(signum, callback, \*args)
Victor Stinner8b863482014-01-27 10:07:50 +01001010
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001011 Set *callback* as the handler for the *signum* signal.
Victor Stinner8b863482014-01-27 10:07:50 +01001012
Hrvoje Nikšiće3666fc2018-12-18 22:31:29 +01001013 The callback will be invoked by *loop*, along with other queued callbacks
1014 and runnable coroutines of that event loop. Unlike signal handlers
1015 registered using :func:`signal.signal`, a callback registered with this
1016 function is allowed to interact with the event loop.
1017
Victor Stinner8b863482014-01-27 10:07:50 +01001018 Raise :exc:`ValueError` if the signal number is invalid or uncatchable.
1019 Raise :exc:`RuntimeError` if there is a problem setting up the handler.
1020
Yury Selivanove247b462018-09-20 12:43:59 -04001021 Use :func:`functools.partial` :ref:`to pass keyword arguments
Naglis17473342018-12-04 09:31:15 +02001022 <asyncio-pass-keywords>` to *callback*.
Victor Stinner8464c242014-11-28 13:15:41 +01001023
Hrvoje Nikšiće3666fc2018-12-18 22:31:29 +01001024 Like :func:`signal.signal`, this function must be invoked in the main
1025 thread.
1026
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001027.. method:: loop.remove_signal_handler(sig)
Victor Stinner8b863482014-01-27 10:07:50 +01001028
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001029 Remove the handler for the *sig* signal.
Victor Stinner8b863482014-01-27 10:07:50 +01001030
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001031 Return ``True`` if the signal handler was removed, or ``False`` if
1032 no handler was set for the given signal.
Victor Stinner8b863482014-01-27 10:07:50 +01001033
Cheryl Sabella2d6097d2018-10-12 10:55:20 -04001034 .. availability:: Unix.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001035
Victor Stinner8b863482014-01-27 10:07:50 +01001036.. seealso::
1037
1038 The :mod:`signal` module.
1039
1040
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001041Executing code in thread or process pools
1042^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +01001043
Yury Selivanov47150392018-09-18 17:55:44 -04001044.. awaitablemethod:: loop.run_in_executor(executor, func, \*args)
Victor Stinnerea3183f2013-12-03 01:08:00 +01001045
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001046 Arrange for *func* to be called in the specified executor.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001047
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001048 The *executor* argument should be an :class:`concurrent.futures.Executor`
Larry Hastings3732ed22014-03-15 21:13:56 -07001049 instance. The default executor is used if *executor* is ``None``.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001050
Yury Selivanove247b462018-09-20 12:43:59 -04001051 Example::
1052
1053 import asyncio
1054 import concurrent.futures
1055
1056 def blocking_io():
1057 # File operations (such as logging) can block the
1058 # event loop: run them in a thread pool.
1059 with open('/dev/urandom', 'rb') as f:
1060 return f.read(100)
1061
1062 def cpu_bound():
1063 # CPU-bound operations will block the event loop:
1064 # in general it is preferable to run them in a
1065 # process pool.
1066 return sum(i * i for i in range(10 ** 7))
1067
1068 async def main():
1069 loop = asyncio.get_running_loop()
1070
1071 ## Options:
1072
1073 # 1. Run in the default loop's executor:
1074 result = await loop.run_in_executor(
1075 None, blocking_io)
1076 print('default thread pool', result)
1077
1078 # 2. Run in a custom thread pool:
1079 with concurrent.futures.ThreadPoolExecutor() as pool:
1080 result = await loop.run_in_executor(
1081 pool, blocking_io)
1082 print('custom thread pool', result)
1083
1084 # 3. Run in a custom process pool:
1085 with concurrent.futures.ProcessPoolExecutor() as pool:
1086 result = await loop.run_in_executor(
1087 pool, cpu_bound)
1088 print('custom process pool', result)
1089
1090 asyncio.run(main())
Victor Stinner8464c242014-11-28 13:15:41 +01001091
Yury Selivanovbec23722018-01-28 14:09:40 -05001092 This method returns a :class:`asyncio.Future` object.
1093
Yury Selivanove247b462018-09-20 12:43:59 -04001094 Use :func:`functools.partial` :ref:`to pass keyword arguments
1095 <asyncio-pass-keywords>` to *func*.
1096
Yury Selivanove8a60452016-10-21 17:40:42 -04001097 .. versionchanged:: 3.5.3
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001098 :meth:`loop.run_in_executor` no longer configures the
Yury Selivanove8a60452016-10-21 17:40:42 -04001099 ``max_workers`` of the thread pool executor it creates, instead
1100 leaving it up to the thread pool executor
1101 (:class:`~concurrent.futures.ThreadPoolExecutor`) to set the
1102 default.
1103
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001104.. method:: loop.set_default_executor(executor)
Victor Stinnerea3183f2013-12-03 01:08:00 +01001105
Elvis Pranskevichus22d25082018-07-30 11:42:43 +01001106 Set *executor* as the default executor used by :meth:`run_in_executor`.
1107 *executor* should be an instance of
1108 :class:`~concurrent.futures.ThreadPoolExecutor`.
1109
1110 .. deprecated:: 3.8
1111 Using an executor that is not an instance of
1112 :class:`~concurrent.futures.ThreadPoolExecutor` is deprecated and
1113 will trigger an error in Python 3.9.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001114
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001115 *executor* must be an instance of
1116 :class:`concurrent.futures.ThreadPoolExecutor`.
1117
Victor Stinnerea3183f2013-12-03 01:08:00 +01001118
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001119Error Handling API
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001120^^^^^^^^^^^^^^^^^^
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001121
Martin Panterc04fb562016-02-10 05:44:01 +00001122Allows customizing how exceptions are handled in the event loop.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001123
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001124.. method:: loop.set_exception_handler(handler)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001125
1126 Set *handler* as the new event loop exception handler.
1127
1128 If *handler* is ``None``, the default exception handler will
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001129 be set. Otherwise, *handler* must be a callable with the signature
1130 matching ``(loop, context)``, where ``loop``
1131 is a reference to the active event loop, and ``context``
1132 is a ``dict`` object containing the details of the exception
1133 (see :meth:`call_exception_handler` documentation for details
1134 about context).
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001135
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001136.. method:: loop.get_exception_handler()
Yury Selivanov950204d2016-05-16 16:23:00 -04001137
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001138 Return the current exception handler, or ``None`` if no custom
1139 exception handler was set.
Yury Selivanov950204d2016-05-16 16:23:00 -04001140
1141 .. versionadded:: 3.5.2
1142
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001143.. method:: loop.default_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001144
1145 Default exception handler.
1146
1147 This is called when an exception occurs and no exception
Carol Willing5b7cbd62018-09-12 17:05:17 -07001148 handler is set. This can be called by a custom exception
1149 handler that wants to defer to the default handler behavior.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001150
1151 *context* parameter has the same meaning as in
1152 :meth:`call_exception_handler`.
1153
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001154.. method:: loop.call_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001155
1156 Call the current event loop exception handler.
1157
1158 *context* is a ``dict`` object containing the following keys
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001159 (new keys may be introduced in future Python versions):
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001160
1161 * 'message': Error message;
1162 * 'exception' (optional): Exception object;
1163 * 'future' (optional): :class:`asyncio.Future` instance;
1164 * 'handle' (optional): :class:`asyncio.Handle` instance;
1165 * 'protocol' (optional): :ref:`Protocol <asyncio-protocol>` instance;
1166 * 'transport' (optional): :ref:`Transport <asyncio-transport>` instance;
1167 * 'socket' (optional): :class:`socket.socket` instance.
1168
1169 .. note::
1170
Carol Willing5b7cbd62018-09-12 17:05:17 -07001171 This method should not be overloaded in subclassed
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001172 event loops. For custom exception handling, use
1173 the :meth:`set_exception_handler()` method.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001174
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001175Enabling debug mode
1176^^^^^^^^^^^^^^^^^^^
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001177
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001178.. method:: loop.get_debug()
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001179
Victor Stinner7b7120e2014-06-23 00:12:14 +02001180 Get the debug mode (:class:`bool`) of the event loop.
1181
1182 The default value is ``True`` if the environment variable
1183 :envvar:`PYTHONASYNCIODEBUG` is set to a non-empty string, ``False``
1184 otherwise.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001185
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001186.. method:: loop.set_debug(enabled: bool)
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001187
1188 Set the debug mode of the event loop.
1189
Yury Selivanov805e27e2018-09-14 16:57:11 -07001190 .. versionchanged:: 3.7
1191
1192 The new ``-X dev`` command line option can now also be used
1193 to enable the debug mode.
1194
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001195.. seealso::
1196
Victor Stinner62511fd2014-06-23 00:36:11 +02001197 The :ref:`debug mode of asyncio <asyncio-debug-mode>`.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001198
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001199
1200Running Subprocesses
1201^^^^^^^^^^^^^^^^^^^^
1202
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001203Methods described in this subsections are low-level. In regular
1204async/await code consider using the high-level
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001205:func:`asyncio.create_subprocess_shell` and
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001206:func:`asyncio.create_subprocess_exec` convenience functions instead.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001207
1208.. note::
1209
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001210 The default asyncio event loop on **Windows** does not support
1211 subprocesses. See :ref:`Subprocess Support on Windows
1212 <asyncio-windows-subprocess>` for details.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001213
1214.. coroutinemethod:: loop.subprocess_exec(protocol_factory, \*args, \
1215 stdin=subprocess.PIPE, stdout=subprocess.PIPE, \
1216 stderr=subprocess.PIPE, \*\*kwargs)
1217
1218 Create a subprocess from one or more string arguments specified by
1219 *args*.
1220
1221 *args* must be a list of strings represented by:
1222
1223 * :class:`str`;
1224 * or :class:`bytes`, encoded to the
1225 :ref:`filesystem encoding <filesystem-encoding>`.
1226
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001227 The first string specifies the program executable,
Carol Willing5b7cbd62018-09-12 17:05:17 -07001228 and the remaining strings specify the arguments. Together, string
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001229 arguments form the ``argv`` of the program.
1230
1231 This is similar to the standard library :class:`subprocess.Popen`
1232 class called with ``shell=False`` and the list of strings passed as
1233 the first argument; however, where :class:`~subprocess.Popen` takes
1234 a single argument which is list of strings, *subprocess_exec*
1235 takes multiple string arguments.
1236
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001237 The *protocol_factory* must be a callable returning a subclass of the
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001238 :class:`asyncio.SubprocessProtocol` class.
1239
1240 Other parameters:
1241
sbstpf0d4c642019-05-27 19:51:19 -04001242 * *stdin* can be any of these:
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001243
sbstpf0d4c642019-05-27 19:51:19 -04001244 * a file-like object representing a pipe to be connected to the
1245 subprocess's standard input stream using
1246 :meth:`~loop.connect_write_pipe`
1247 * the :const:`subprocess.PIPE` constant (default) which will create a new
1248 pipe and connect it,
1249 * the value ``None`` which will make the subprocess inherit the file
1250 descriptor from this process
1251 * the :const:`subprocess.DEVNULL` constant which indicates that the
1252 special :data:`os.devnull` file will be used
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001253
sbstpf0d4c642019-05-27 19:51:19 -04001254 * *stdout* can be any of these:
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001255
sbstpf0d4c642019-05-27 19:51:19 -04001256 * a file-like object representing a pipe to be connected to the
1257 subprocess's standard output stream using
1258 :meth:`~loop.connect_write_pipe`
1259 * the :const:`subprocess.PIPE` constant (default) which will create a new
1260 pipe and connect it,
1261 * the value ``None`` which will make the subprocess inherit the file
1262 descriptor from this process
1263 * the :const:`subprocess.DEVNULL` constant which indicates that the
1264 special :data:`os.devnull` file will be used
1265
1266 * *stderr* can be any of these:
1267
1268 * a file-like object representing a pipe to be connected to the
1269 subprocess's standard error stream using
1270 :meth:`~loop.connect_write_pipe`
1271 * the :const:`subprocess.PIPE` constant (default) which will create a new
1272 pipe and connect it,
1273 * the value ``None`` which will make the subprocess inherit the file
1274 descriptor from this process
1275 * the :const:`subprocess.DEVNULL` constant which indicates that the
1276 special :data:`os.devnull` file will be used
1277 * the :const:`subprocess.STDOUT` constant which will connect the standard
1278 error stream to the process' standard output stream
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001279
1280 * All other keyword arguments are passed to :class:`subprocess.Popen`
sbstpf0d4c642019-05-27 19:51:19 -04001281 without interpretation, except for *bufsize*, *universal_newlines*,
1282 *shell*, *text*, *encoding* and *errors*, which should not be specified
1283 at all.
1284
1285 The ``asyncio`` subprocess API does not support decoding the streams
1286 as text. :func:`bytes.decode` can be used to convert the bytes returned
1287 from the stream to text.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001288
1289 See the constructor of the :class:`subprocess.Popen` class
1290 for documentation on other arguments.
1291
1292 Returns a pair of ``(transport, protocol)``, where *transport*
Bumsik Kim5cc583d2018-09-16 19:40:44 -04001293 conforms to the :class:`asyncio.SubprocessTransport` base class and
1294 *protocol* is an object instantiated by the *protocol_factory*.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001295
1296.. coroutinemethod:: loop.subprocess_shell(protocol_factory, cmd, \*, \
1297 stdin=subprocess.PIPE, stdout=subprocess.PIPE, \
1298 stderr=subprocess.PIPE, \*\*kwargs)
1299
1300 Create a subprocess from *cmd*, which can be a :class:`str` or a
1301 :class:`bytes` string encoded to the
1302 :ref:`filesystem encoding <filesystem-encoding>`,
1303 using the platform's "shell" syntax.
1304
1305 This is similar to the standard library :class:`subprocess.Popen`
1306 class called with ``shell=True``.
1307
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001308 The *protocol_factory* must be a callable returning a subclass of the
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001309 :class:`SubprocessProtocol` class.
1310
1311 See :meth:`~loop.subprocess_exec` for more details about
1312 the remaining arguments.
1313
1314 Returns a pair of ``(transport, protocol)``, where *transport*
Bumsik Kim5cc583d2018-09-16 19:40:44 -04001315 conforms to the :class:`SubprocessTransport` base class and
1316 *protocol* is an object instantiated by the *protocol_factory*.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001317
1318.. note::
1319 It is the application's responsibility to ensure that all whitespace
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001320 and special characters are quoted appropriately to avoid `shell injection
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001321 <https://en.wikipedia.org/wiki/Shell_injection#Shell_injection>`_
1322 vulnerabilities. The :func:`shlex.quote` function can be used to
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001323 properly escape whitespace and special characters in strings that
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001324 are going to be used to construct shell commands.
1325
1326
1327Callback Handles
1328================
1329
1330.. class:: Handle
1331
1332 A callback wrapper object returned by :meth:`loop.call_soon`,
1333 :meth:`loop.call_soon_threadsafe`.
1334
1335 .. method:: cancel()
1336
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001337 Cancel the callback. If the callback has already been canceled
1338 or executed, this method has no effect.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001339
1340 .. method:: cancelled()
1341
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001342 Return ``True`` if the callback was cancelled.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001343
1344 .. versionadded:: 3.7
1345
1346.. class:: TimerHandle
1347
1348 A callback wrapper object returned by :meth:`loop.call_later`,
1349 and :meth:`loop.call_at`.
1350
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001351 This class is a subclass of :class:`Handle`.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001352
1353 .. method:: when()
1354
1355 Return a scheduled callback time as :class:`float` seconds.
1356
1357 The time is an absolute timestamp, using the same time
1358 reference as :meth:`loop.time`.
1359
1360 .. versionadded:: 3.7
1361
1362
1363Server Objects
1364==============
1365
1366Server objects are created by :meth:`loop.create_server`,
1367:meth:`loop.create_unix_server`, :func:`start_server`,
1368and :func:`start_unix_server` functions.
1369
1370Do not instantiate the class directly.
Victor Stinner8c462c52014-01-24 18:11:43 +01001371
Victor Stinner8ebeb032014-07-11 23:47:40 +02001372.. class:: Server
Victor Stinner8c462c52014-01-24 18:11:43 +01001373
Yury Selivanovc9070d02018-01-25 18:08:09 -05001374 *Server* objects are asynchronous context managers. When used in an
1375 ``async with`` statement, it's guaranteed that the Server object is
1376 closed and not accepting new connections when the ``async with``
1377 statement is completed::
1378
1379 srv = await loop.create_server(...)
1380
1381 async with srv:
1382 # some code
1383
Carol Willing5b7cbd62018-09-12 17:05:17 -07001384 # At this point, srv is closed and no longer accepts new connections.
Yury Selivanovc9070d02018-01-25 18:08:09 -05001385
1386
1387 .. versionchanged:: 3.7
1388 Server object is an asynchronous context manager since Python 3.7.
Victor Stinner8c462c52014-01-24 18:11:43 +01001389
1390 .. method:: close()
1391
Victor Stinner4bfb14a2014-07-12 03:20:24 +02001392 Stop serving: close listening sockets and set the :attr:`sockets`
1393 attribute to ``None``.
1394
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001395 The sockets that represent existing incoming client connections
1396 are left open.
Victor Stinner8ebeb032014-07-11 23:47:40 +02001397
Berker Peksag49c9edf2016-01-20 07:14:22 +02001398 The server is closed asynchronously, use the :meth:`wait_closed`
1399 coroutine to wait until the server is closed.
Victor Stinner8c462c52014-01-24 18:11:43 +01001400
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)1634fc22017-12-30 20:39:32 +05301401 .. method:: get_loop()
1402
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001403 Return the event loop associated with the server object.
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)1634fc22017-12-30 20:39:32 +05301404
1405 .. versionadded:: 3.7
1406
Yury Selivanovc9070d02018-01-25 18:08:09 -05001407 .. coroutinemethod:: start_serving()
1408
1409 Start accepting connections.
1410
1411 This method is idempotent, so it can be called when
1412 the server is already being serving.
1413
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001414 The *start_serving* keyword-only parameter to
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001415 :meth:`loop.create_server` and
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001416 :meth:`asyncio.start_server` allows creating a Server object
1417 that is not accepting connections initially. In this case
1418 ``Server.start_serving()``, or :meth:`Server.serve_forever` can be used
1419 to make the Server start accepting connections.
Yury Selivanovc9070d02018-01-25 18:08:09 -05001420
1421 .. versionadded:: 3.7
1422
1423 .. coroutinemethod:: serve_forever()
1424
1425 Start accepting connections until the coroutine is cancelled.
1426 Cancellation of ``serve_forever`` task causes the server
1427 to be closed.
1428
1429 This method can be called if the server is already accepting
1430 connections. Only one ``serve_forever`` task can exist per
1431 one *Server* object.
1432
1433 Example::
1434
1435 async def client_connected(reader, writer):
1436 # Communicate with the client with
1437 # reader/writer streams. For example:
1438 await reader.readline()
1439
1440 async def main(host, port):
1441 srv = await asyncio.start_server(
1442 client_connected, host, port)
Andrew Svetlov17ab8f02018-02-17 19:44:35 +02001443 await srv.serve_forever()
Yury Selivanovc9070d02018-01-25 18:08:09 -05001444
1445 asyncio.run(main('127.0.0.1', 0))
1446
1447 .. versionadded:: 3.7
1448
1449 .. method:: is_serving()
1450
1451 Return ``True`` if the server is accepting new connections.
1452
1453 .. versionadded:: 3.7
1454
Victor Stinnerbdd574d2015-02-12 22:49:18 +01001455 .. coroutinemethod:: wait_closed()
Victor Stinner8c462c52014-01-24 18:11:43 +01001456
Victor Stinner8ebeb032014-07-11 23:47:40 +02001457 Wait until the :meth:`close` method completes.
1458
Victor Stinner8ebeb032014-07-11 23:47:40 +02001459 .. attribute:: sockets
1460
Emmanuel Ariasdf5cdc12019-02-22 14:34:41 -03001461 List of :class:`socket.socket` objects the server is listening on.
Victor Stinner8c462c52014-01-24 18:11:43 +01001462
Yury Selivanovc9070d02018-01-25 18:08:09 -05001463 .. versionchanged:: 3.7
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001464 Prior to Python 3.7 ``Server.sockets`` used to return an
1465 internal list of server sockets directly. In 3.7 a copy
Yury Selivanovc9070d02018-01-25 18:08:09 -05001466 of that list is returned.
1467
Victor Stinner8c462c52014-01-24 18:11:43 +01001468
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001469.. _asyncio-event-loops:
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001470
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001471Event Loop Implementations
1472==========================
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001473
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001474asyncio ships with two different event loop implementations:
1475:class:`SelectorEventLoop` and :class:`ProactorEventLoop`.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001476
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001477By default asyncio is configured to use :class:`SelectorEventLoop`
Miss Islington (bot)2e0ec5b2019-06-23 17:33:00 -07001478on Unix and :class:`ProactorEventLoop` on Windows.
Andrew Svetlov3d4dbd82018-02-01 19:59:32 +02001479
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001480
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001481.. class:: SelectorEventLoop
1482
1483 An event loop based on the :mod:`selectors` module.
1484
1485 Uses the most efficient *selector* available for the given
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001486 platform. It is also possible to manually configure the
1487 exact selector implementation to be used::
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001488
1489 import asyncio
1490 import selectors
1491
1492 selector = selectors.SelectSelector()
1493 loop = asyncio.SelectorEventLoop(selector)
1494 asyncio.set_event_loop(loop)
Andrew Svetlov7464e872018-01-19 20:04:29 +02001495
1496
Cheryl Sabella2d6097d2018-10-12 10:55:20 -04001497 .. availability:: Unix, Windows.
Andrew Svetlov7464e872018-01-19 20:04:29 +02001498
1499
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001500.. class:: ProactorEventLoop
1501
1502 An event loop for Windows that uses "I/O Completion Ports" (IOCP).
1503
Cheryl Sabella2d6097d2018-10-12 10:55:20 -04001504 .. availability:: Windows.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001505
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001506 .. seealso::
1507
1508 `MSDN documentation on I/O Completion Ports
1509 <https://docs.microsoft.com/en-ca/windows/desktop/FileIO/i-o-completion-ports>`_.
1510
1511
1512.. class:: AbstractEventLoop
1513
1514 Abstract base class for asyncio-compliant event loops.
1515
1516 The :ref:`Event Loop Methods <asyncio-event-loop>` section lists all
1517 methods that an alternative implementation of ``AbstractEventLoop``
1518 should have defined.
1519
1520
1521Examples
1522========
1523
1524Note that all examples in this section **purposefully** show how
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001525to use the low-level event loop APIs, such as :meth:`loop.run_forever`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001526and :meth:`loop.call_soon`. Modern asyncio applications rarely
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001527need to be written this way; consider using the high-level functions
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001528like :func:`asyncio.run`.
1529
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001530
Yury Selivanov394374e2018-09-17 15:35:24 -04001531.. _asyncio_example_lowlevel_helloworld:
Victor Stinnerea3183f2013-12-03 01:08:00 +01001532
Victor Stinner7f314ed2014-10-15 18:49:16 +02001533Hello World with call_soon()
Victor Stinnera092a612015-01-09 15:58:41 +01001534^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +01001535
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001536An example using the :meth:`loop.call_soon` method to schedule a
1537callback. The callback displays ``"Hello World"`` and then stops the
1538event loop::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001539
1540 import asyncio
1541
Victor Stinner7f314ed2014-10-15 18:49:16 +02001542 def hello_world(loop):
Carol Willing5b7cbd62018-09-12 17:05:17 -07001543 """A callback to print 'Hello World' and stop the event loop"""
Victor Stinnerea3183f2013-12-03 01:08:00 +01001544 print('Hello World')
Victor Stinner7f314ed2014-10-15 18:49:16 +02001545 loop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001546
1547 loop = asyncio.get_event_loop()
Victor Stinner7f314ed2014-10-15 18:49:16 +02001548
1549 # Schedule a call to hello_world()
1550 loop.call_soon(hello_world, loop)
1551
1552 # Blocking call interrupted by loop.stop()
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001553 try:
1554 loop.run_forever()
1555 finally:
1556 loop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001557
Victor Stinner3e09e322013-12-03 01:22:06 +01001558.. seealso::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001559
Yury Selivanov3faaa882018-09-14 13:32:07 -07001560 A similar :ref:`Hello World <coroutine>`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001561 example created with a coroutine and the :func:`run` function.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001562
Victor Stinner8b863482014-01-27 10:07:50 +01001563
Yury Selivanov394374e2018-09-17 15:35:24 -04001564.. _asyncio_example_call_later:
Victor Stinner7f314ed2014-10-15 18:49:16 +02001565
1566Display the current date with call_later()
Victor Stinnera092a612015-01-09 15:58:41 +01001567^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner7f314ed2014-10-15 18:49:16 +02001568
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001569An example of a callback displaying the current date every second. The
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001570callback uses the :meth:`loop.call_later` method to reschedule itself
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001571after 5 seconds, and then stops the event loop::
Victor Stinner7f314ed2014-10-15 18:49:16 +02001572
1573 import asyncio
1574 import datetime
1575
1576 def display_date(end_time, loop):
1577 print(datetime.datetime.now())
1578 if (loop.time() + 1.0) < end_time:
1579 loop.call_later(1, display_date, end_time, loop)
1580 else:
1581 loop.stop()
1582
1583 loop = asyncio.get_event_loop()
1584
1585 # Schedule the first call to display_date()
1586 end_time = loop.time() + 5.0
1587 loop.call_soon(display_date, end_time, loop)
1588
1589 # Blocking call interrupted by loop.stop()
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001590 try:
1591 loop.run_forever()
1592 finally:
1593 loop.close()
Victor Stinner7f314ed2014-10-15 18:49:16 +02001594
1595.. seealso::
1596
Yury Selivanov7372c3b2018-09-14 15:11:24 -07001597 A similar :ref:`current date <asyncio_example_sleep>` example
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001598 created with a coroutine and the :func:`run` function.
Victor Stinner7f314ed2014-10-15 18:49:16 +02001599
1600
Yury Selivanov394374e2018-09-17 15:35:24 -04001601.. _asyncio_example_watch_fd:
Victor Stinner8b863482014-01-27 10:07:50 +01001602
Victor Stinner04e6df32014-10-11 16:16:27 +02001603Watch a file descriptor for read events
Victor Stinnera092a612015-01-09 15:58:41 +01001604^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001605
1606Wait until a file descriptor received some data using the
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001607:meth:`loop.add_reader` method and then close the event loop::
Victor Stinner04e6df32014-10-11 16:16:27 +02001608
1609 import asyncio
Victor Stinnerac577d72017-11-28 21:33:20 +01001610 from socket import socketpair
Victor Stinner04e6df32014-10-11 16:16:27 +02001611
1612 # Create a pair of connected file descriptors
Victor Stinnerccd8e342014-10-11 16:30:02 +02001613 rsock, wsock = socketpair()
Carol Willing5b7cbd62018-09-12 17:05:17 -07001614
Victor Stinner04e6df32014-10-11 16:16:27 +02001615 loop = asyncio.get_event_loop()
1616
1617 def reader():
1618 data = rsock.recv(100)
1619 print("Received:", data.decode())
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001620
Victor Stinner2cef3002014-10-23 22:38:46 +02001621 # We are done: unregister the file descriptor
Victor Stinner04e6df32014-10-11 16:16:27 +02001622 loop.remove_reader(rsock)
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001623
Victor Stinner04e6df32014-10-11 16:16:27 +02001624 # Stop the event loop
1625 loop.stop()
1626
Victor Stinner2cef3002014-10-23 22:38:46 +02001627 # Register the file descriptor for read event
Victor Stinner04e6df32014-10-11 16:16:27 +02001628 loop.add_reader(rsock, reader)
1629
1630 # Simulate the reception of data from the network
1631 loop.call_soon(wsock.send, 'abc'.encode())
1632
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001633 try:
1634 # Run the event loop
1635 loop.run_forever()
1636 finally:
Carol Willing5b7cbd62018-09-12 17:05:17 -07001637 # We are done. Close sockets and the event loop.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001638 rsock.close()
1639 wsock.close()
1640 loop.close()
Victor Stinner04e6df32014-10-11 16:16:27 +02001641
1642.. seealso::
1643
Yury Selivanov394374e2018-09-17 15:35:24 -04001644 * A similar :ref:`example <asyncio_example_create_connection>`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001645 using transports, protocols, and the
1646 :meth:`loop.create_connection` method.
Victor Stinner04e6df32014-10-11 16:16:27 +02001647
Yury Selivanov394374e2018-09-17 15:35:24 -04001648 * Another similar :ref:`example <asyncio_example_create_connection-streams>`
Yury Selivanov1c19d652019-09-29 22:30:17 -07001649 using the high-level :func:`asyncio.open_connection` function
1650 and streams.
Victor Stinner04e6df32014-10-11 16:16:27 +02001651
1652
Yury Selivanov394374e2018-09-17 15:35:24 -04001653.. _asyncio_example_unix_signals:
1654
Victor Stinner04e6df32014-10-11 16:16:27 +02001655Set signal handlers for SIGINT and SIGTERM
Victor Stinnera092a612015-01-09 15:58:41 +01001656^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001657
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001658(This ``signals`` example only works on Unix.)
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001659
1660Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM`
1661using the :meth:`loop.add_signal_handler` method::
Victor Stinner8b863482014-01-27 10:07:50 +01001662
1663 import asyncio
1664 import functools
1665 import os
1666 import signal
1667
Alexander Vasinceb842e2019-05-03 18:25:36 +03001668 def ask_exit(signame, loop):
Victor Stinner8b863482014-01-27 10:07:50 +01001669 print("got signal %s: exit" % signame)
1670 loop.stop()
1671
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001672 async def main():
1673 loop = asyncio.get_running_loop()
Victor Stinner8b863482014-01-27 10:07:50 +01001674
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001675 for signame in {'SIGINT', 'SIGTERM'}:
1676 loop.add_signal_handler(
1677 getattr(signal, signame),
Alexander Vasinceb842e2019-05-03 18:25:36 +03001678 functools.partial(ask_exit, signame, loop))
Victor Stinner2cef3002014-10-23 22:38:46 +02001679
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001680 await asyncio.sleep(3600)
1681
1682 print("Event loop running for 1 hour, press Ctrl+C to interrupt.")
1683 print(f"pid {os.getpid()}: send SIGINT or SIGTERM to exit.")
1684
1685 asyncio.run(main())