blob: b1e73189a7a4cfc07f4a7f78fdb9b3a353d8c02b [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
Kyle Stanleyf9000642019-10-10 19:18:46 -04008**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
Andrew Svetlov2c49bec2020-01-21 00:46:38 +020041 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
Kyle Stanley9fdc64c2019-09-19 08:47:22 -0400176.. coroutinemethod:: loop.shutdown_default_executor()
177
178 Schedule the closure of the default executor and wait for it to join all of
179 the threads in the :class:`ThreadPoolExecutor`. After calling this method, a
180 :exc:`RuntimeError` will be raised if :meth:`loop.run_in_executor` is called
181 while using the default executor.
182
183 Note that there is no need to call this function when
184 :func:`asyncio.run` is used.
185
186 .. versionadded:: 3.9
187
Yury Selivanov03660042016-12-15 17:36:05 -0500188
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700189Scheduling callbacks
190^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100191
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700192.. method:: loop.call_soon(callback, *args, context=None)
Victor Stinner8464c242014-11-28 13:15:41 +0100193
Miss Islington (bot)ddd3c462020-06-22 19:22:45 -0700194 Schedule the *callback* :term:`callback` to be called with
195 *args* arguments at the next iteration of the event loop.
Victor Stinner8464c242014-11-28 13:15:41 +0100196
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700197 Callbacks are called in the order in which they are registered.
198 Each callback will be called exactly once.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100199
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700200 An optional keyword-only *context* argument allows specifying a
201 custom :class:`contextvars.Context` for the *callback* to run in.
202 The current context is used when no *context* is provided.
Yury Selivanov28b91782018-05-23 13:35:04 -0400203
Yury Selivanov1096f762015-06-25 13:49:52 -0400204 An instance of :class:`asyncio.Handle` is returned, which can be
Carol Willing5b7cbd62018-09-12 17:05:17 -0700205 used later to cancel the callback.
206
207 This method is not thread-safe.
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500208
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700209.. method:: loop.call_soon_threadsafe(callback, *args, context=None)
Victor Stinner8464c242014-11-28 13:15:41 +0100210
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700211 A thread-safe variant of :meth:`call_soon`. Must be used to
212 schedule callbacks *from another thread*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100213
Victor Stinner83704962015-02-25 14:24:15 +0100214 See the :ref:`concurrency and multithreading <asyncio-multithreading>`
215 section of the documentation.
216
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700217.. versionchanged:: 3.7
218 The *context* keyword-only parameter was added. See :pep:`567`
219 for more details.
220
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400221.. _asyncio-pass-keywords:
222
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700223.. note::
224
Carol Willing5b7cbd62018-09-12 17:05:17 -0700225 Most :mod:`asyncio` scheduling functions don't allow passing
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400226 keyword arguments. To do that, use :func:`functools.partial`::
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700227
Carol Willing5b7cbd62018-09-12 17:05:17 -0700228 # will schedule "print("Hello", flush=True)"
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700229 loop.call_soon(
230 functools.partial(print, "Hello", flush=True))
231
232 Using partial objects is usually more convenient than using lambdas,
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400233 as asyncio can render partial objects better in debug and error
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700234 messages.
Yury Selivanov28b91782018-05-23 13:35:04 -0400235
Victor Stinnerea3183f2013-12-03 01:08:00 +0100236
Victor Stinner45b27ed2014-02-01 02:36:43 +0100237.. _asyncio-delayed-calls:
238
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700239Scheduling delayed callbacks
240^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100241
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700242Event loop provides mechanisms to schedule callback functions
243to be called at some point in the future. Event loop uses monotonic
244clocks to track time.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100245
Victor Stinner45b27ed2014-02-01 02:36:43 +0100246
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700247.. method:: loop.call_later(delay, callback, *args, context=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100248
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700249 Schedule *callback* to be called after the given *delay*
250 number of seconds (can be either an int or a float).
Victor Stinnerea3183f2013-12-03 01:08:00 +0100251
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700252 An instance of :class:`asyncio.TimerHandle` is returned which can
253 be used to cancel the callback.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100254
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700255 *callback* will be called exactly once. If two callbacks are
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400256 scheduled for exactly the same time, the order in which they
257 are called is undefined.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100258
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700259 The optional positional *args* will be passed to the callback when
260 it is called. If you want the callback to be called with keyword
261 arguments use :func:`functools.partial`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100262
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700263 An optional keyword-only *context* argument allows specifying a
264 custom :class:`contextvars.Context` for the *callback* to run in.
265 The current context is used when no *context* is provided.
Victor Stinner8464c242014-11-28 13:15:41 +0100266
Yury Selivanov28b91782018-05-23 13:35:04 -0400267 .. versionchanged:: 3.7
268 The *context* keyword-only parameter was added. See :pep:`567`
269 for more details.
270
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400271 .. versionchanged:: 3.8
272 In Python 3.7 and earlier with the default event loop implementation,
273 the *delay* could not exceed one day.
274 This has been fixed in Python 3.8.
275
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700276.. method:: loop.call_at(when, callback, *args, context=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100277
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700278 Schedule *callback* to be called at the given absolute timestamp
279 *when* (an int or a float), using the same time reference as
280 :meth:`loop.time`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100281
282 This method's behavior is the same as :meth:`call_later`.
283
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700284 An instance of :class:`asyncio.TimerHandle` is returned which can
285 be used to cancel the callback.
Victor Stinner8464c242014-11-28 13:15:41 +0100286
Yury Selivanov28b91782018-05-23 13:35:04 -0400287 .. versionchanged:: 3.7
288 The *context* keyword-only parameter was added. See :pep:`567`
289 for more details.
290
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400291 .. versionchanged:: 3.8
292 In Python 3.7 and earlier with the default event loop implementation,
293 the difference between *when* and the current time could not exceed
294 one day. This has been fixed in Python 3.8.
295
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700296.. method:: loop.time()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100297
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700298 Return the current time, as a :class:`float` value, according to
299 the event loop's internal monotonic clock.
300
301.. note::
Enrico Alarico Carbognani7e954e72019-04-18 14:43:14 +0200302 .. versionchanged:: 3.8
303 In Python 3.7 and earlier timeouts (relative *delay* or absolute *when*)
304 should not exceed one day. This has been fixed in Python 3.8.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100305
Victor Stinner3e09e322013-12-03 01:22:06 +0100306.. seealso::
307
308 The :func:`asyncio.sleep` function.
309
Victor Stinnerea3183f2013-12-03 01:08:00 +0100310
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700311Creating Futures and Tasks
312^^^^^^^^^^^^^^^^^^^^^^^^^^
Yury Selivanov950204d2016-05-16 16:23:00 -0400313
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700314.. method:: loop.create_future()
Yury Selivanov950204d2016-05-16 16:23:00 -0400315
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700316 Create an :class:`asyncio.Future` object attached to the event loop.
Yury Selivanov950204d2016-05-16 16:23:00 -0400317
Carol Willing5b7cbd62018-09-12 17:05:17 -0700318 This is the preferred way to create Futures in asyncio. This lets
319 third-party event loops provide alternative implementations of
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700320 the Future object (with better performance or instrumentation).
Yury Selivanov950204d2016-05-16 16:23:00 -0400321
322 .. versionadded:: 3.5.2
323
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700324.. method:: loop.create_task(coro, \*, name=None)
Yury Selivanov950204d2016-05-16 16:23:00 -0400325
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700326 Schedule the execution of a :ref:`coroutine`.
327 Return a :class:`Task` object.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200328
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700329 Third-party event loops can use their own subclass of :class:`Task`
330 for interoperability. In this case, the result type is a subclass
331 of :class:`Task`.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200332
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700333 If the *name* argument is provided and not ``None``, it is set as
334 the name of the task using :meth:`Task.set_name`.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200335
Alex Grönholmcca4eec2018-08-09 00:06:47 +0300336 .. versionchanged:: 3.8
337 Added the ``name`` parameter.
338
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700339.. method:: loop.set_task_factory(factory)
Yury Selivanov71854612015-05-11 16:28:27 -0400340
341 Set a task factory that will be used by
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700342 :meth:`loop.create_task`.
Yury Selivanov71854612015-05-11 16:28:27 -0400343
344 If *factory* is ``None`` the default task factory will be set.
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400345 Otherwise, *factory* must be a *callable* with the signature matching
346 ``(loop, coro)``, where *loop* is a reference to the active
347 event loop, and *coro* is a coroutine object. The callable
348 must return a :class:`asyncio.Future`-compatible object.
Yury Selivanov71854612015-05-11 16:28:27 -0400349
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700350.. method:: loop.get_task_factory()
Yury Selivanov71854612015-05-11 16:28:27 -0400351
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700352 Return a task factory or ``None`` if the default one is in use.
Yury Selivanov71854612015-05-11 16:28:27 -0400353
Victor Stinner530ef2f2014-07-08 12:39:10 +0200354
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700355Opening network connections
356^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100357
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700358.. coroutinemethod:: loop.create_connection(protocol_factory, \
359 host=None, port=None, \*, ssl=None, \
360 family=0, proto=0, flags=0, sock=None, \
361 local_addr=None, server_hostname=None, \
idomic5305cc92020-02-10 04:48:40 -0500362 ssl_handshake_timeout=None, \
363 happy_eyeballs_delay=None, interleave=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100364
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700365 Open a streaming transport connection to a given
366 address specified by *host* and *port*.
367
368 The socket family can be either :py:data:`~socket.AF_INET` or
369 :py:data:`~socket.AF_INET6` depending on *host* (or the *family*
370 argument, if provided).
371
372 The socket type will be :py:data:`~socket.SOCK_STREAM`.
373
374 *protocol_factory* must be a callable returning an
375 :ref:`asyncio protocol <asyncio-protocol>` implementation.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100376
Yury Selivanov19a44f62017-12-14 20:53:26 -0500377 This method will try to establish the connection in the background.
378 When successful, it returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100379
380 The chronological synopsis of the underlying operation is as follows:
381
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700382 #. The connection is established and a :ref:`transport <asyncio-transport>`
383 is created for it.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100384
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700385 #. *protocol_factory* is called without arguments and is expected to
386 return a :ref:`protocol <asyncio-protocol>` instance.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100387
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700388 #. The protocol instance is coupled with the transport by calling its
389 :meth:`~BaseProtocol.connection_made` method.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100390
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700391 #. A ``(transport, protocol)`` tuple is returned on success.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100392
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700393 The created transport is an implementation-dependent bidirectional
394 stream.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100395
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700396 Other arguments:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100397
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400398 * *ssl*: if given and not false, a SSL/TLS transport is created
Victor Stinnerea3183f2013-12-03 01:08:00 +0100399 (by default a plain TCP transport is created). If *ssl* is
400 a :class:`ssl.SSLContext` object, this context is used to create
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400401 the transport; if *ssl* is :const:`True`, a default context returned
402 from :func:`ssl.create_default_context` is used.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100403
Berker Peksag9c1dba22014-09-28 00:00:58 +0300404 .. seealso:: :ref:`SSL/TLS security considerations <ssl-security>`
Antoine Pitrouc5e075f2014-03-22 18:19:11 +0100405
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400406 * *server_hostname* sets or overrides the hostname that the target
407 server's certificate will be matched against. Should only be passed
408 if *ssl* is not ``None``. By default the value of the *host* argument
Victor Stinnerea3183f2013-12-03 01:08:00 +0100409 is used. If *host* is empty, there is no default and you must pass a
410 value for *server_hostname*. If *server_hostname* is an empty
411 string, hostname matching is disabled (which is a serious security
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400412 risk, allowing for potential man-in-the-middle attacks).
Victor Stinnerea3183f2013-12-03 01:08:00 +0100413
414 * *family*, *proto*, *flags* are the optional address family, protocol
415 and flags to be passed through to getaddrinfo() for *host* resolution.
416 If given, these should all be integers from the corresponding
417 :mod:`socket` module constants.
418
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800419 * *happy_eyeballs_delay*, if given, enables Happy Eyeballs for this
420 connection. It should
421 be a floating-point number representing the amount of time in seconds
422 to wait for a connection attempt to complete, before starting the next
423 attempt in parallel. This is the "Connection Attempt Delay" as defined
424 in :rfc:`8305`. A sensible default value recommended by the RFC is ``0.25``
425 (250 milliseconds).
426
427 * *interleave* controls address reordering when a host name resolves to
428 multiple IP addresses.
429 If ``0`` or unspecified, no reordering is done, and addresses are
430 tried in the order returned by :meth:`getaddrinfo`. If a positive integer
431 is specified, the addresses are interleaved by address family, and the
432 given integer is interpreted as "First Address Family Count" as defined
433 in :rfc:`8305`. The default is ``0`` if *happy_eyeballs_delay* is not
434 specified, and ``1`` if it is.
435
Victor Stinnerea3183f2013-12-03 01:08:00 +0100436 * *sock*, if given, should be an existing, already connected
437 :class:`socket.socket` object to be used by the transport.
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800438 If *sock* is given, none of *host*, *port*, *family*, *proto*, *flags*,
439 *happy_eyeballs_delay*, *interleave*
Victor Stinnerea3183f2013-12-03 01:08:00 +0100440 and *local_addr* should be specified.
441
442 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
443 to bind the socket to locally. The *local_host* and *local_port*
Carol Willing5b7cbd62018-09-12 17:05:17 -0700444 are looked up using ``getaddrinfo()``, similarly to *host* and *port*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100445
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400446 * *ssl_handshake_timeout* is (for a TLS connection) the time in seconds
447 to wait for the TLS handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400448 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000449
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800450 .. versionadded:: 3.8
451
idomic5305cc92020-02-10 04:48:40 -0500452 Added the *happy_eyeballs_delay* and *interleave* parameters.
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800453
idomic8af47122020-02-24 09:59:40 -0500454 Happy Eyeballs Algorithm: Success with Dual-Stack Hosts.
455 When a server's IPv4 path and protocol are working, but the server's
456 IPv6 path and protocol are not working, a dual-stack client
457 application experiences significant connection delay compared to an
458 IPv4-only client. This is undesirable because it causes the dual-
459 stack client to have a worse user experience. This document
460 specifies requirements for algorithms that reduce this user-visible
461 delay and provides an algorithm.
462
463 For more information: https://tools.ietf.org/html/rfc6555
464
Neil Aspinallf7686c12017-12-19 19:45:42 +0000465 .. versionadded:: 3.7
466
467 The *ssl_handshake_timeout* parameter.
468
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700469 .. versionchanged:: 3.6
470
471 The socket option :py:data:`~socket.TCP_NODELAY` is set by default
472 for all TCP connections.
473
Victor Stinner60208a12015-09-15 22:41:52 +0200474 .. versionchanged:: 3.5
475
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400476 Added support for SSL/TLS in :class:`ProactorEventLoop`.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200477
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100478 .. seealso::
479
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700480 The :func:`open_connection` function is a high-level alternative
481 API. It returns a pair of (:class:`StreamReader`, :class:`StreamWriter`)
482 that can be used directly in async/await code.
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100483
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700484.. coroutinemethod:: loop.create_datagram_endpoint(protocol_factory, \
485 local_addr=None, remote_addr=None, \*, \
486 family=0, proto=0, flags=0, \
487 reuse_address=None, reuse_port=None, \
488 allow_broadcast=None, sock=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100489
Kyle Stanleyab513a32019-12-09 09:21:10 -0500490 .. note::
491 The parameter *reuse_address* is no longer supported, as using
492 :py:data:`~sockets.SO_REUSEADDR` poses a significant security concern for
493 UDP. Explicitly passing ``reuse_address=True`` will raise an exception.
494
495 When multiple processes with differing UIDs assign sockets to an
Jesús Ceab0d49492019-12-20 03:21:03 +0100496 identical UDP socket address with ``SO_REUSEADDR``, incoming packets can
Kyle Stanleyab513a32019-12-09 09:21:10 -0500497 become randomly distributed among the sockets.
498
499 For supported platforms, *reuse_port* can be used as a replacement for
500 similar functionality. With *reuse_port*,
501 :py:data:`~sockets.SO_REUSEPORT` is used instead, which specifically
502 prevents processes with differing UIDs from assigning sockets to the same
503 socket address.
504
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700505 Create a datagram connection.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100506
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700507 The socket family can be either :py:data:`~socket.AF_INET`,
508 :py:data:`~socket.AF_INET6`, or :py:data:`~socket.AF_UNIX`,
509 depending on *host* (or the *family* argument, if provided).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100510
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700511 The socket type will be :py:data:`~socket.SOCK_DGRAM`.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100512
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700513 *protocol_factory* must be a callable returning a
514 :ref:`protocol <asyncio-protocol>` implementation.
515
516 A tuple of ``(transport, protocol)`` is returned on success.
517
518 Other arguments:
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700519
520 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
521 to bind the socket to locally. The *local_host* and *local_port*
522 are looked up using :meth:`getaddrinfo`.
523
524 * *remote_addr*, if given, is a ``(remote_host, remote_port)`` tuple used
525 to connect the socket to a remote address. The *remote_host* and
526 *remote_port* are looked up using :meth:`getaddrinfo`.
527
528 * *family*, *proto*, *flags* are the optional address family, protocol
529 and flags to be passed through to :meth:`getaddrinfo` for *host*
530 resolution. If given, these should all be integers from the
531 corresponding :mod:`socket` module constants.
532
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700533 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
534 same port as other existing endpoints are bound to, so long as they all
535 set this flag when being created. This option is not supported on Windows
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400536 and some Unixes. If the :py:data:`~socket.SO_REUSEPORT` constant is not
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700537 defined then this capability is unsupported.
538
539 * *allow_broadcast* tells the kernel to allow this endpoint to send
540 messages to the broadcast address.
541
542 * *sock* can optionally be specified in order to use a preexisting,
543 already connected, :class:`socket.socket` object to be used by the
544 transport. If specified, *local_addr* and *remote_addr* should be omitted
545 (must be :const:`None`).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100546
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200547 See :ref:`UDP echo client protocol <asyncio-udp-echo-client-protocol>` and
548 :ref:`UDP echo server protocol <asyncio-udp-echo-server-protocol>` examples.
549
Romuald Brunet0ded5802018-05-14 18:22:00 +0200550 .. versionchanged:: 3.4.4
551 The *family*, *proto*, *flags*, *reuse_address*, *reuse_port,
552 *allow_broadcast*, and *sock* parameters were added.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100553
Kyle Stanleyab513a32019-12-09 09:21:10 -0500554 .. versionchanged:: 3.8.1
555 The *reuse_address* parameter is no longer supported due to security
556 concerns.
557
Andrew Svetlovbafd4b52019-05-28 12:52:15 +0300558 .. versionchanged:: 3.8
559 Added support for Windows.
560
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700561.. coroutinemethod:: loop.create_unix_connection(protocol_factory, \
562 path=None, \*, ssl=None, sock=None, \
563 server_hostname=None, ssl_handshake_timeout=None)
Victor Stinnera6919aa2014-02-19 13:32:34 +0100564
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400565 Create a Unix connection.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100566
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700567 The socket family will be :py:data:`~socket.AF_UNIX`; socket
568 type will be :py:data:`~socket.SOCK_STREAM`.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100569
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700570 A tuple of ``(transport, protocol)`` is returned on success.
Guido van Rossum9e80eeb2016-11-03 14:17:25 -0700571
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400572 *path* is the name of a Unix domain socket and is required,
573 unless a *sock* parameter is specified. Abstract Unix sockets,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700574 :class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths are
575 supported.
576
577 See the documentation of the :meth:`loop.create_connection` method
578 for information about arguments to this method.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100579
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400580 .. availability:: Unix.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100581
Neil Aspinallf7686c12017-12-19 19:45:42 +0000582 .. versionadded:: 3.7
583
584 The *ssl_handshake_timeout* parameter.
585
Yury Selivanov423fd362017-11-20 17:26:28 -0500586 .. versionchanged:: 3.7
587
Elvis Pranskevichusc0d062f2018-06-08 11:36:00 -0400588 The *path* parameter can now be a :term:`path-like object`.
Yury Selivanov423fd362017-11-20 17:26:28 -0500589
Victor Stinnera6919aa2014-02-19 13:32:34 +0100590
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700591Creating network servers
592^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100593
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700594.. coroutinemethod:: loop.create_server(protocol_factory, \
595 host=None, port=None, \*, \
596 family=socket.AF_UNSPEC, \
597 flags=socket.AI_PASSIVE, \
598 sock=None, backlog=100, ssl=None, \
599 reuse_address=None, reuse_port=None, \
600 ssl_handshake_timeout=None, start_serving=True)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100601
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700602 Create a TCP server (socket type :data:`~socket.SOCK_STREAM`) listening
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400603 on *port* of the *host* address.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200604
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700605 Returns a :class:`Server` object.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200606
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700607 Arguments:
Victor Stinner33f6abe2014-10-12 20:36:04 +0200608
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400609 * *protocol_factory* must be a callable returning a
610 :ref:`protocol <asyncio-protocol>` implementation.
611
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400612 * The *host* parameter can be set to several types which determine where
613 the server would be listening:
614
615 - If *host* is a string, the TCP server is bound to a single network
616 interface specified by *host*.
617
618 - If *host* is a sequence of strings, the TCP server is bound to all
619 network interfaces specified by the sequence.
620
621 - If *host* is an empty string or ``None``, all interfaces are
622 assumed and a list of multiple sockets will be returned (most likely
623 one for IPv4 and another one for IPv6).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200624
625 * *family* can be set to either :data:`socket.AF_INET` or
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700626 :data:`~socket.AF_INET6` to force the socket to use IPv4 or IPv6.
Carol Willing5b7cbd62018-09-12 17:05:17 -0700627 If not set, the *family* will be determined from host name
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700628 (defaults to :data:`~socket.AF_UNSPEC`).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200629
630 * *flags* is a bitmask for :meth:`getaddrinfo`.
631
632 * *sock* can optionally be specified in order to use a preexisting
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400633 socket object. If specified, *host* and *port* must not be specified.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200634
635 * *backlog* is the maximum number of queued connections passed to
636 :meth:`~socket.socket.listen` (defaults to 100).
637
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400638 * *ssl* can be set to an :class:`~ssl.SSLContext` instance to enable
639 TLS over the accepted connections.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200640
641 * *reuse_address* tells the kernel to reuse a local socket in
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700642 ``TIME_WAIT`` state, without waiting for its natural timeout to
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300643 expire. If not specified will automatically be set to ``True`` on
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400644 Unix.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100645
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700646 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
647 same port as other existing endpoints are bound to, so long as they all
648 set this flag when being created. This option is not supported on
649 Windows.
650
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400651 * *ssl_handshake_timeout* is (for a TLS server) the time in seconds to wait
652 for the TLS handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400653 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000654
Yury Selivanovc9070d02018-01-25 18:08:09 -0500655 * *start_serving* set to ``True`` (the default) causes the created server
656 to start accepting connections immediately. When set to ``False``,
657 the user should await on :meth:`Server.start_serving` or
658 :meth:`Server.serve_forever` to make the server to start accepting
659 connections.
660
Neil Aspinallf7686c12017-12-19 19:45:42 +0000661 .. versionadded:: 3.7
662
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700663 Added *ssl_handshake_timeout* and *start_serving* parameters.
664
665 .. versionchanged:: 3.6
666
667 The socket option :py:data:`~socket.TCP_NODELAY` is set by default
668 for all TCP connections.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000669
Victor Stinner60208a12015-09-15 22:41:52 +0200670 .. versionchanged:: 3.5
671
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400672 Added support for SSL/TLS in :class:`ProactorEventLoop`.
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100673
Victor Stinner7b58a2b2015-09-21 18:41:05 +0200674 .. versionchanged:: 3.5.1
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200675
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700676 The *host* parameter can be a sequence of strings.
677
678 .. seealso::
679
680 The :func:`start_server` function is a higher-level alternative API
681 that returns a pair of :class:`StreamReader` and :class:`StreamWriter`
682 that can be used in an async/await code.
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200683
Victor Stinnerea3183f2013-12-03 01:08:00 +0100684
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700685.. coroutinemethod:: loop.create_unix_server(protocol_factory, path=None, \
686 \*, sock=None, backlog=100, ssl=None, \
687 ssl_handshake_timeout=None, start_serving=True)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100688
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700689 Similar to :meth:`loop.create_server` but works with the
690 :py:data:`~socket.AF_UNIX` socket family.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100691
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400692 *path* is the name of a Unix domain socket, and is required,
693 unless a *sock* argument is provided. Abstract Unix sockets,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700694 :class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths
695 are supported.
Yury Selivanov423fd362017-11-20 17:26:28 -0500696
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400697 See the documentation of the :meth:`loop.create_server` method
698 for information about arguments to this method.
699
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400700 .. availability:: Unix.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100701
Neil Aspinallf7686c12017-12-19 19:45:42 +0000702 .. versionadded:: 3.7
703
Elvis Pranskevichusc0d062f2018-06-08 11:36:00 -0400704 The *ssl_handshake_timeout* and *start_serving* parameters.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000705
Yury Selivanov423fd362017-11-20 17:26:28 -0500706 .. versionchanged:: 3.7
707
708 The *path* parameter can now be a :class:`~pathlib.Path` object.
709
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700710.. coroutinemethod:: loop.connect_accepted_socket(protocol_factory, \
711 sock, \*, ssl=None, ssl_handshake_timeout=None)
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500712
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700713 Wrap an already accepted connection into a transport/protocol pair.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500714
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700715 This method can be used by servers that accept connections outside
716 of asyncio but that use asyncio to handle them.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500717
718 Parameters:
719
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400720 * *protocol_factory* must be a callable returning a
721 :ref:`protocol <asyncio-protocol>` implementation.
722
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700723 * *sock* is a preexisting socket object returned from
724 :meth:`socket.accept <socket.socket.accept>`.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500725
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700726 * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over
727 the accepted connections.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500728
Neil Aspinallf7686c12017-12-19 19:45:42 +0000729 * *ssl_handshake_timeout* is (for an SSL connection) the time in seconds to
730 wait for the SSL handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400731 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000732
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700733 Returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100734
Neil Aspinallf7686c12017-12-19 19:45:42 +0000735 .. versionadded:: 3.7
736
737 The *ssl_handshake_timeout* parameter.
738
AraHaan431665b2017-11-21 11:06:26 -0500739 .. versionadded:: 3.5.3
740
741
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700742Transferring files
743^^^^^^^^^^^^^^^^^^
Andrew Svetlov7c684072018-01-27 21:22:47 +0200744
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700745.. coroutinemethod:: loop.sendfile(transport, file, \
746 offset=0, count=None, *, fallback=True)
Andrew Svetlov7c684072018-01-27 21:22:47 +0200747
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700748 Send a *file* over a *transport*. Return the total number of bytes
749 sent.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200750
751 The method uses high-performance :meth:`os.sendfile` if available.
752
753 *file* must be a regular file object opened in binary mode.
754
755 *offset* tells from where to start reading the file. If specified,
756 *count* is the total number of bytes to transmit as opposed to
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400757 sending the file until EOF is reached. File position is always updated,
758 even when this method raises an error, and
759 :meth:`file.tell() <io.IOBase.tell>` can be used to obtain the actual
760 number of bytes sent.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200761
762 *fallback* set to ``True`` makes asyncio to manually read and send
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700763 the file when the platform does not support the sendfile system call
Andrew Svetlov7c684072018-01-27 21:22:47 +0200764 (e.g. Windows or SSL socket on Unix).
765
766 Raise :exc:`SendfileNotAvailableError` if the system does not support
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400767 the *sendfile* syscall and *fallback* is ``False``.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200768
769 .. versionadded:: 3.7
770
771
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500772TLS Upgrade
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700773^^^^^^^^^^^
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500774
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700775.. coroutinemethod:: loop.start_tls(transport, protocol, \
776 sslcontext, \*, server_side=False, \
777 server_hostname=None, ssl_handshake_timeout=None)
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500778
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700779 Upgrade an existing transport-based connection to TLS.
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500780
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700781 Return a new transport instance, that the *protocol* must start using
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500782 immediately after the *await*. The *transport* instance passed to
783 the *start_tls* method should never be used again.
784
785 Parameters:
786
787 * *transport* and *protocol* instances that methods like
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700788 :meth:`~loop.create_server` and
789 :meth:`~loop.create_connection` return.
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500790
791 * *sslcontext*: a configured instance of :class:`~ssl.SSLContext`.
792
793 * *server_side* pass ``True`` when a server-side connection is being
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700794 upgraded (like the one created by :meth:`~loop.create_server`).
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500795
796 * *server_hostname*: sets or overrides the host name that the target
797 server's certificate will be matched against.
798
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400799 * *ssl_handshake_timeout* is (for a TLS connection) the time in seconds to
800 wait for the TLS handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400801 ``60.0`` seconds if ``None`` (default).
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500802
803 .. versionadded:: 3.7
804
805
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700806Watching file descriptors
807^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerc1567df2014-02-08 23:22:58 +0100808
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700809.. method:: loop.add_reader(fd, callback, \*args)
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200810
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400811 Start monitoring the *fd* file descriptor for read availability and
812 invoke *callback* with the specified arguments once *fd* is available for
813 reading.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200814
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700815.. method:: loop.remove_reader(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100816
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400817 Stop monitoring the *fd* file descriptor for read availability.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100818
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700819.. method:: loop.add_writer(fd, callback, \*args)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100820
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400821 Start monitoring the *fd* file descriptor for write availability and
822 invoke *callback* with the specified arguments once *fd* is available for
823 writing.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100824
Yury Selivanove247b462018-09-20 12:43:59 -0400825 Use :func:`functools.partial` :ref:`to pass keyword arguments
Naglis17473342018-12-04 09:31:15 +0200826 <asyncio-pass-keywords>` to *callback*.
Victor Stinner8464c242014-11-28 13:15:41 +0100827
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700828.. method:: loop.remove_writer(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100829
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400830 Stop monitoring the *fd* file descriptor for write availability.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100831
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700832See also :ref:`Platform Support <asyncio-platform-support>` section
833for some limitations of these methods.
Victor Stinner04e6df32014-10-11 16:16:27 +0200834
Victor Stinnerc1567df2014-02-08 23:22:58 +0100835
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700836Working with socket objects directly
837^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerc1567df2014-02-08 23:22:58 +0100838
Carol Willing5b7cbd62018-09-12 17:05:17 -0700839In general, protocol implementations that use transport-based APIs
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700840such as :meth:`loop.create_connection` and :meth:`loop.create_server`
841are faster than implementations that work with sockets directly.
Carol Willing5b7cbd62018-09-12 17:05:17 -0700842However, there are some use cases when performance is not critical, and
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700843working with :class:`~socket.socket` objects directly is more
844convenient.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100845
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700846.. coroutinemethod:: loop.sock_recv(sock, nbytes)
Yury Selivanov55c50842016-06-08 12:48:15 -0400847
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400848 Receive up to *nbytes* from *sock*. Asynchronous version of
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700849 :meth:`socket.recv() <socket.socket.recv>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100850
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400851 Return the received data as a bytes object.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700852
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400853 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200854
Yury Selivanov19a44f62017-12-14 20:53:26 -0500855 .. versionchanged:: 3.7
Carol Willing5b7cbd62018-09-12 17:05:17 -0700856 Even though this method was always documented as a coroutine
857 method, releases before Python 3.7 returned a :class:`Future`.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700858 Since Python 3.7 this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100859
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700860.. coroutinemethod:: loop.sock_recv_into(sock, buf)
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200861
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400862 Receive data from *sock* into the *buf* buffer. Modeled after the blocking
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700863 :meth:`socket.recv_into() <socket.socket.recv_into>` method.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200864
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700865 Return the number of bytes written to the buffer.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200866
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400867 *sock* must be a non-blocking socket.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200868
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200869 .. versionadded:: 3.7
870
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700871.. coroutinemethod:: loop.sock_sendall(sock, data)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100872
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400873 Send *data* to the *sock* socket. Asynchronous version of
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700874 :meth:`socket.sendall() <socket.socket.sendall>`.
Yury Selivanov55c50842016-06-08 12:48:15 -0400875
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400876 This method continues to send to the socket until either all data
877 in *data* has been sent or an error occurs. ``None`` is returned
Carol Willing5b7cbd62018-09-12 17:05:17 -0700878 on success. On error, an exception is raised. Additionally, there is no way
879 to determine how much data, if any, was successfully processed by the
880 receiving end of the connection.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100881
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400882 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200883
Yury Selivanov19a44f62017-12-14 20:53:26 -0500884 .. versionchanged:: 3.7
885 Even though the method was always documented as a coroutine
886 method, before Python 3.7 it returned an :class:`Future`.
887 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100888
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700889.. coroutinemethod:: loop.sock_connect(sock, address)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100890
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400891 Connect *sock* to a remote socket at *address*.
Victor Stinner1b0580b2014-02-13 09:24:37 +0100892
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700893 Asynchronous version of :meth:`socket.connect() <socket.socket.connect>`.
894
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400895 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200896
Yury Selivanov55c50842016-06-08 12:48:15 -0400897 .. versionchanged:: 3.5.2
898 ``address`` no longer needs to be resolved. ``sock_connect``
899 will try to check if the *address* is already resolved by calling
900 :func:`socket.inet_pton`. If not,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700901 :meth:`loop.getaddrinfo` will be used to resolve the
Yury Selivanov55c50842016-06-08 12:48:15 -0400902 *address*.
903
Victor Stinnerc1567df2014-02-08 23:22:58 +0100904 .. seealso::
905
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700906 :meth:`loop.create_connection`
Yury Selivanov55c50842016-06-08 12:48:15 -0400907 and :func:`asyncio.open_connection() <open_connection>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100908
909
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700910.. coroutinemethod:: loop.sock_accept(sock)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100911
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700912 Accept a connection. Modeled after the blocking
913 :meth:`socket.accept() <socket.socket.accept>` method.
Yury Selivanov55c50842016-06-08 12:48:15 -0400914
915 The socket must be bound to an address and listening
Victor Stinnerc1567df2014-02-08 23:22:58 +0100916 for connections. The return value is a pair ``(conn, address)`` where *conn*
917 is a *new* socket object usable to send and receive data on the connection,
918 and *address* is the address bound to the socket on the other end of the
919 connection.
920
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400921 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200922
Yury Selivanov19a44f62017-12-14 20:53:26 -0500923 .. versionchanged:: 3.7
924 Even though the method was always documented as a coroutine
925 method, before Python 3.7 it returned a :class:`Future`.
926 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100927
928 .. seealso::
929
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700930 :meth:`loop.create_server` and :func:`start_server`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100931
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700932.. coroutinemethod:: loop.sock_sendfile(sock, file, offset=0, count=None, \
933 \*, fallback=True)
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200934
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700935 Send a file using high-performance :mod:`os.sendfile` if possible.
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400936 Return the total number of bytes sent.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200937
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700938 Asynchronous version of :meth:`socket.sendfile() <socket.socket.sendfile>`.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200939
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400940 *sock* must be a non-blocking :const:`socket.SOCK_STREAM`
941 :class:`~socket.socket`.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200942
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400943 *file* must be a regular file object open in binary mode.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200944
945 *offset* tells from where to start reading the file. If specified,
946 *count* is the total number of bytes to transmit as opposed to
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400947 sending the file until EOF is reached. File position is always updated,
948 even when this method raises an error, and
949 :meth:`file.tell() <io.IOBase.tell>` can be used to obtain the actual
950 number of bytes sent.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200951
Carol Willing5b7cbd62018-09-12 17:05:17 -0700952 *fallback*, when set to ``True``, makes asyncio manually read and send
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200953 the file when the platform does not support the sendfile syscall
954 (e.g. Windows or SSL socket on Unix).
955
Andrew Svetlov7464e872018-01-19 20:04:29 +0200956 Raise :exc:`SendfileNotAvailableError` if the system does not support
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200957 *sendfile* syscall and *fallback* is ``False``.
958
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400959 *sock* must be a non-blocking socket.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700960
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200961 .. versionadded:: 3.7
962
Victor Stinnerc1567df2014-02-08 23:22:58 +0100963
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700964DNS
965^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100966
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700967.. coroutinemethod:: loop.getaddrinfo(host, port, \*, family=0, \
968 type=0, proto=0, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100969
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700970 Asynchronous version of :meth:`socket.getaddrinfo`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100971
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700972.. coroutinemethod:: loop.getnameinfo(sockaddr, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100973
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700974 Asynchronous version of :meth:`socket.getnameinfo`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100975
Yury Selivanovbec23722018-01-28 14:09:40 -0500976.. versionchanged:: 3.7
977 Both *getaddrinfo* and *getnameinfo* methods were always documented
978 to return a coroutine, but prior to Python 3.7 they were, in fact,
979 returning :class:`asyncio.Future` objects. Starting with Python 3.7
980 both methods are coroutines.
981
Victor Stinnerea3183f2013-12-03 01:08:00 +0100982
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700983Working with pipes
984^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100985
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700986.. coroutinemethod:: loop.connect_read_pipe(protocol_factory, pipe)
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200987
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400988 Register the read end of *pipe* in the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100989
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700990 *protocol_factory* must be a callable returning an
991 :ref:`asyncio protocol <asyncio-protocol>` implementation.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100992
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700993 *pipe* is a :term:`file-like object <file object>`.
994
995 Return pair ``(transport, protocol)``, where *transport* supports
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400996 the :class:`ReadTransport` interface and *protocol* is an object
997 instantiated by the *protocol_factory*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100998
Victor Stinnerd84fd732014-08-26 01:01:59 +0200999 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
1000 non-blocking mode.
1001
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001002.. coroutinemethod:: loop.connect_write_pipe(protocol_factory, pipe)
Victor Stinnerea3183f2013-12-03 01:08:00 +01001003
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001004 Register the write end of *pipe* in the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001005
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001006 *protocol_factory* must be a callable returning an
1007 :ref:`asyncio protocol <asyncio-protocol>` implementation.
1008
1009 *pipe* is :term:`file-like object <file object>`.
1010
Victor Stinner2cef3002014-10-23 22:38:46 +02001011 Return pair ``(transport, protocol)``, where *transport* supports
Bumsik Kim5cc583d2018-09-16 19:40:44 -04001012 :class:`WriteTransport` interface and *protocol* is an object
1013 instantiated by the *protocol_factory*.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001014
Victor Stinnerd84fd732014-08-26 01:01:59 +02001015 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
1016 non-blocking mode.
1017
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001018.. note::
1019
1020 :class:`SelectorEventLoop` does not support the above methods on
Carol Willing5b7cbd62018-09-12 17:05:17 -07001021 Windows. Use :class:`ProactorEventLoop` instead for Windows.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001022
Victor Stinner08444382014-02-02 22:43:39 +01001023.. seealso::
1024
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001025 The :meth:`loop.subprocess_exec` and
1026 :meth:`loop.subprocess_shell` methods.
Victor Stinner08444382014-02-02 22:43:39 +01001027
Victor Stinnerea3183f2013-12-03 01:08:00 +01001028
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001029Unix signals
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001030^^^^^^^^^^^^
Victor Stinner8b863482014-01-27 10:07:50 +01001031
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001032.. method:: loop.add_signal_handler(signum, callback, \*args)
Victor Stinner8b863482014-01-27 10:07:50 +01001033
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001034 Set *callback* as the handler for the *signum* signal.
Victor Stinner8b863482014-01-27 10:07:50 +01001035
Hrvoje Nikšiće3666fc2018-12-18 22:31:29 +01001036 The callback will be invoked by *loop*, along with other queued callbacks
1037 and runnable coroutines of that event loop. Unlike signal handlers
1038 registered using :func:`signal.signal`, a callback registered with this
1039 function is allowed to interact with the event loop.
1040
Victor Stinner8b863482014-01-27 10:07:50 +01001041 Raise :exc:`ValueError` if the signal number is invalid or uncatchable.
1042 Raise :exc:`RuntimeError` if there is a problem setting up the handler.
1043
Yury Selivanove247b462018-09-20 12:43:59 -04001044 Use :func:`functools.partial` :ref:`to pass keyword arguments
Naglis17473342018-12-04 09:31:15 +02001045 <asyncio-pass-keywords>` to *callback*.
Victor Stinner8464c242014-11-28 13:15:41 +01001046
Hrvoje Nikšiće3666fc2018-12-18 22:31:29 +01001047 Like :func:`signal.signal`, this function must be invoked in the main
1048 thread.
1049
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001050.. method:: loop.remove_signal_handler(sig)
Victor Stinner8b863482014-01-27 10:07:50 +01001051
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001052 Remove the handler for the *sig* signal.
Victor Stinner8b863482014-01-27 10:07:50 +01001053
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001054 Return ``True`` if the signal handler was removed, or ``False`` if
1055 no handler was set for the given signal.
Victor Stinner8b863482014-01-27 10:07:50 +01001056
Cheryl Sabella2d6097d2018-10-12 10:55:20 -04001057 .. availability:: Unix.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001058
Victor Stinner8b863482014-01-27 10:07:50 +01001059.. seealso::
1060
1061 The :mod:`signal` module.
1062
1063
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001064Executing code in thread or process pools
1065^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +01001066
Yury Selivanov47150392018-09-18 17:55:44 -04001067.. awaitablemethod:: loop.run_in_executor(executor, func, \*args)
Victor Stinnerea3183f2013-12-03 01:08:00 +01001068
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001069 Arrange for *func* to be called in the specified executor.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001070
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001071 The *executor* argument should be an :class:`concurrent.futures.Executor`
Larry Hastings3732ed22014-03-15 21:13:56 -07001072 instance. The default executor is used if *executor* is ``None``.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001073
Yury Selivanove247b462018-09-20 12:43:59 -04001074 Example::
1075
1076 import asyncio
1077 import concurrent.futures
1078
1079 def blocking_io():
1080 # File operations (such as logging) can block the
1081 # event loop: run them in a thread pool.
1082 with open('/dev/urandom', 'rb') as f:
1083 return f.read(100)
1084
1085 def cpu_bound():
1086 # CPU-bound operations will block the event loop:
1087 # in general it is preferable to run them in a
1088 # process pool.
1089 return sum(i * i for i in range(10 ** 7))
1090
1091 async def main():
1092 loop = asyncio.get_running_loop()
1093
1094 ## Options:
1095
1096 # 1. Run in the default loop's executor:
1097 result = await loop.run_in_executor(
1098 None, blocking_io)
1099 print('default thread pool', result)
1100
1101 # 2. Run in a custom thread pool:
1102 with concurrent.futures.ThreadPoolExecutor() as pool:
1103 result = await loop.run_in_executor(
1104 pool, blocking_io)
1105 print('custom thread pool', result)
1106
1107 # 3. Run in a custom process pool:
1108 with concurrent.futures.ProcessPoolExecutor() as pool:
1109 result = await loop.run_in_executor(
1110 pool, cpu_bound)
1111 print('custom process pool', result)
1112
1113 asyncio.run(main())
Victor Stinner8464c242014-11-28 13:15:41 +01001114
Yury Selivanovbec23722018-01-28 14:09:40 -05001115 This method returns a :class:`asyncio.Future` object.
1116
Yury Selivanove247b462018-09-20 12:43:59 -04001117 Use :func:`functools.partial` :ref:`to pass keyword arguments
1118 <asyncio-pass-keywords>` to *func*.
1119
Yury Selivanove8a60452016-10-21 17:40:42 -04001120 .. versionchanged:: 3.5.3
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001121 :meth:`loop.run_in_executor` no longer configures the
Yury Selivanove8a60452016-10-21 17:40:42 -04001122 ``max_workers`` of the thread pool executor it creates, instead
1123 leaving it up to the thread pool executor
1124 (:class:`~concurrent.futures.ThreadPoolExecutor`) to set the
1125 default.
1126
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001127.. method:: loop.set_default_executor(executor)
Victor Stinnerea3183f2013-12-03 01:08:00 +01001128
Elvis Pranskevichus22d25082018-07-30 11:42:43 +01001129 Set *executor* as the default executor used by :meth:`run_in_executor`.
1130 *executor* should be an instance of
1131 :class:`~concurrent.futures.ThreadPoolExecutor`.
1132
1133 .. deprecated:: 3.8
1134 Using an executor that is not an instance of
1135 :class:`~concurrent.futures.ThreadPoolExecutor` is deprecated and
1136 will trigger an error in Python 3.9.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001137
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001138 *executor* must be an instance of
1139 :class:`concurrent.futures.ThreadPoolExecutor`.
1140
Victor Stinnerea3183f2013-12-03 01:08:00 +01001141
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001142Error Handling API
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001143^^^^^^^^^^^^^^^^^^
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001144
Martin Panterc04fb562016-02-10 05:44:01 +00001145Allows customizing how exceptions are handled in the event loop.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001146
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001147.. method:: loop.set_exception_handler(handler)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001148
1149 Set *handler* as the new event loop exception handler.
1150
1151 If *handler* is ``None``, the default exception handler will
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001152 be set. Otherwise, *handler* must be a callable with the signature
1153 matching ``(loop, context)``, where ``loop``
1154 is a reference to the active event loop, and ``context``
1155 is a ``dict`` object containing the details of the exception
1156 (see :meth:`call_exception_handler` documentation for details
1157 about context).
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001158
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001159.. method:: loop.get_exception_handler()
Yury Selivanov950204d2016-05-16 16:23:00 -04001160
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001161 Return the current exception handler, or ``None`` if no custom
1162 exception handler was set.
Yury Selivanov950204d2016-05-16 16:23:00 -04001163
1164 .. versionadded:: 3.5.2
1165
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001166.. method:: loop.default_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001167
1168 Default exception handler.
1169
1170 This is called when an exception occurs and no exception
Carol Willing5b7cbd62018-09-12 17:05:17 -07001171 handler is set. This can be called by a custom exception
1172 handler that wants to defer to the default handler behavior.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001173
1174 *context* parameter has the same meaning as in
1175 :meth:`call_exception_handler`.
1176
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001177.. method:: loop.call_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001178
1179 Call the current event loop exception handler.
1180
1181 *context* is a ``dict`` object containing the following keys
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001182 (new keys may be introduced in future Python versions):
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001183
1184 * 'message': Error message;
1185 * 'exception' (optional): Exception object;
1186 * 'future' (optional): :class:`asyncio.Future` instance;
1187 * 'handle' (optional): :class:`asyncio.Handle` instance;
1188 * 'protocol' (optional): :ref:`Protocol <asyncio-protocol>` instance;
1189 * 'transport' (optional): :ref:`Transport <asyncio-transport>` instance;
1190 * 'socket' (optional): :class:`socket.socket` instance.
1191
1192 .. note::
1193
Carol Willing5b7cbd62018-09-12 17:05:17 -07001194 This method should not be overloaded in subclassed
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001195 event loops. For custom exception handling, use
1196 the :meth:`set_exception_handler()` method.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001197
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001198Enabling debug mode
1199^^^^^^^^^^^^^^^^^^^
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001200
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001201.. method:: loop.get_debug()
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001202
Victor Stinner7b7120e2014-06-23 00:12:14 +02001203 Get the debug mode (:class:`bool`) of the event loop.
1204
1205 The default value is ``True`` if the environment variable
1206 :envvar:`PYTHONASYNCIODEBUG` is set to a non-empty string, ``False``
1207 otherwise.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001208
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001209.. method:: loop.set_debug(enabled: bool)
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001210
1211 Set the debug mode of the event loop.
1212
Yury Selivanov805e27e2018-09-14 16:57:11 -07001213 .. versionchanged:: 3.7
1214
Victor Stinnerb9783d22020-01-24 10:22:18 +01001215 The new :ref:`Python Development Mode <devmode>` can now also be used
Yury Selivanov805e27e2018-09-14 16:57:11 -07001216 to enable the debug mode.
1217
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001218.. seealso::
1219
Victor Stinner62511fd2014-06-23 00:36:11 +02001220 The :ref:`debug mode of asyncio <asyncio-debug-mode>`.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001221
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001222
1223Running Subprocesses
1224^^^^^^^^^^^^^^^^^^^^
1225
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001226Methods described in this subsections are low-level. In regular
1227async/await code consider using the high-level
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001228:func:`asyncio.create_subprocess_shell` and
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001229:func:`asyncio.create_subprocess_exec` convenience functions instead.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001230
1231.. note::
1232
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001233 The default asyncio event loop on **Windows** does not support
1234 subprocesses. See :ref:`Subprocess Support on Windows
1235 <asyncio-windows-subprocess>` for details.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001236
1237.. coroutinemethod:: loop.subprocess_exec(protocol_factory, \*args, \
1238 stdin=subprocess.PIPE, stdout=subprocess.PIPE, \
1239 stderr=subprocess.PIPE, \*\*kwargs)
1240
1241 Create a subprocess from one or more string arguments specified by
1242 *args*.
1243
1244 *args* must be a list of strings represented by:
1245
1246 * :class:`str`;
1247 * or :class:`bytes`, encoded to the
1248 :ref:`filesystem encoding <filesystem-encoding>`.
1249
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001250 The first string specifies the program executable,
Carol Willing5b7cbd62018-09-12 17:05:17 -07001251 and the remaining strings specify the arguments. Together, string
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001252 arguments form the ``argv`` of the program.
1253
1254 This is similar to the standard library :class:`subprocess.Popen`
1255 class called with ``shell=False`` and the list of strings passed as
1256 the first argument; however, where :class:`~subprocess.Popen` takes
1257 a single argument which is list of strings, *subprocess_exec*
1258 takes multiple string arguments.
1259
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001260 The *protocol_factory* must be a callable returning a subclass of the
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001261 :class:`asyncio.SubprocessProtocol` class.
1262
1263 Other parameters:
1264
sbstpf0d4c642019-05-27 19:51:19 -04001265 * *stdin* can be any of these:
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001266
sbstpf0d4c642019-05-27 19:51:19 -04001267 * a file-like object representing a pipe to be connected to the
1268 subprocess's standard input stream using
1269 :meth:`~loop.connect_write_pipe`
1270 * the :const:`subprocess.PIPE` constant (default) which will create a new
1271 pipe and connect it,
1272 * the value ``None`` which will make the subprocess inherit the file
1273 descriptor from this process
1274 * the :const:`subprocess.DEVNULL` constant which indicates that the
1275 special :data:`os.devnull` file will be used
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001276
sbstpf0d4c642019-05-27 19:51:19 -04001277 * *stdout* can be any of these:
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001278
sbstpf0d4c642019-05-27 19:51:19 -04001279 * a file-like object representing a pipe to be connected to the
1280 subprocess's standard output stream using
1281 :meth:`~loop.connect_write_pipe`
1282 * the :const:`subprocess.PIPE` constant (default) which will create a new
1283 pipe and connect it,
1284 * the value ``None`` which will make the subprocess inherit the file
1285 descriptor from this process
1286 * the :const:`subprocess.DEVNULL` constant which indicates that the
1287 special :data:`os.devnull` file will be used
1288
1289 * *stderr* can be any of these:
1290
1291 * a file-like object representing a pipe to be connected to the
1292 subprocess's standard error stream using
1293 :meth:`~loop.connect_write_pipe`
1294 * the :const:`subprocess.PIPE` constant (default) which will create a new
1295 pipe and connect it,
1296 * the value ``None`` which will make the subprocess inherit the file
1297 descriptor from this process
1298 * the :const:`subprocess.DEVNULL` constant which indicates that the
1299 special :data:`os.devnull` file will be used
1300 * the :const:`subprocess.STDOUT` constant which will connect the standard
1301 error stream to the process' standard output stream
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001302
1303 * All other keyword arguments are passed to :class:`subprocess.Popen`
sbstpf0d4c642019-05-27 19:51:19 -04001304 without interpretation, except for *bufsize*, *universal_newlines*,
1305 *shell*, *text*, *encoding* and *errors*, which should not be specified
1306 at all.
1307
1308 The ``asyncio`` subprocess API does not support decoding the streams
1309 as text. :func:`bytes.decode` can be used to convert the bytes returned
1310 from the stream to text.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001311
1312 See the constructor of the :class:`subprocess.Popen` class
1313 for documentation on other arguments.
1314
1315 Returns a pair of ``(transport, protocol)``, where *transport*
Bumsik Kim5cc583d2018-09-16 19:40:44 -04001316 conforms to the :class:`asyncio.SubprocessTransport` base class and
1317 *protocol* is an object instantiated by the *protocol_factory*.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001318
1319.. coroutinemethod:: loop.subprocess_shell(protocol_factory, cmd, \*, \
1320 stdin=subprocess.PIPE, stdout=subprocess.PIPE, \
1321 stderr=subprocess.PIPE, \*\*kwargs)
1322
1323 Create a subprocess from *cmd*, which can be a :class:`str` or a
1324 :class:`bytes` string encoded to the
1325 :ref:`filesystem encoding <filesystem-encoding>`,
1326 using the platform's "shell" syntax.
1327
1328 This is similar to the standard library :class:`subprocess.Popen`
1329 class called with ``shell=True``.
1330
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001331 The *protocol_factory* must be a callable returning a subclass of the
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001332 :class:`SubprocessProtocol` class.
1333
1334 See :meth:`~loop.subprocess_exec` for more details about
1335 the remaining arguments.
1336
1337 Returns a pair of ``(transport, protocol)``, where *transport*
Bumsik Kim5cc583d2018-09-16 19:40:44 -04001338 conforms to the :class:`SubprocessTransport` base class and
1339 *protocol* is an object instantiated by the *protocol_factory*.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001340
1341.. note::
1342 It is the application's responsibility to ensure that all whitespace
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001343 and special characters are quoted appropriately to avoid `shell injection
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001344 <https://en.wikipedia.org/wiki/Shell_injection#Shell_injection>`_
1345 vulnerabilities. The :func:`shlex.quote` function can be used to
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001346 properly escape whitespace and special characters in strings that
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001347 are going to be used to construct shell commands.
1348
1349
1350Callback Handles
1351================
1352
1353.. class:: Handle
1354
1355 A callback wrapper object returned by :meth:`loop.call_soon`,
1356 :meth:`loop.call_soon_threadsafe`.
1357
1358 .. method:: cancel()
1359
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001360 Cancel the callback. If the callback has already been canceled
1361 or executed, this method has no effect.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001362
1363 .. method:: cancelled()
1364
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001365 Return ``True`` if the callback was cancelled.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001366
1367 .. versionadded:: 3.7
1368
1369.. class:: TimerHandle
1370
1371 A callback wrapper object returned by :meth:`loop.call_later`,
1372 and :meth:`loop.call_at`.
1373
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001374 This class is a subclass of :class:`Handle`.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001375
1376 .. method:: when()
1377
1378 Return a scheduled callback time as :class:`float` seconds.
1379
1380 The time is an absolute timestamp, using the same time
1381 reference as :meth:`loop.time`.
1382
1383 .. versionadded:: 3.7
1384
1385
1386Server Objects
1387==============
1388
1389Server objects are created by :meth:`loop.create_server`,
1390:meth:`loop.create_unix_server`, :func:`start_server`,
1391and :func:`start_unix_server` functions.
1392
1393Do not instantiate the class directly.
Victor Stinner8c462c52014-01-24 18:11:43 +01001394
Victor Stinner8ebeb032014-07-11 23:47:40 +02001395.. class:: Server
Victor Stinner8c462c52014-01-24 18:11:43 +01001396
Yury Selivanovc9070d02018-01-25 18:08:09 -05001397 *Server* objects are asynchronous context managers. When used in an
1398 ``async with`` statement, it's guaranteed that the Server object is
1399 closed and not accepting new connections when the ``async with``
1400 statement is completed::
1401
1402 srv = await loop.create_server(...)
1403
1404 async with srv:
1405 # some code
1406
Carol Willing5b7cbd62018-09-12 17:05:17 -07001407 # At this point, srv is closed and no longer accepts new connections.
Yury Selivanovc9070d02018-01-25 18:08:09 -05001408
1409
1410 .. versionchanged:: 3.7
1411 Server object is an asynchronous context manager since Python 3.7.
Victor Stinner8c462c52014-01-24 18:11:43 +01001412
1413 .. method:: close()
1414
Victor Stinner4bfb14a2014-07-12 03:20:24 +02001415 Stop serving: close listening sockets and set the :attr:`sockets`
1416 attribute to ``None``.
1417
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001418 The sockets that represent existing incoming client connections
1419 are left open.
Victor Stinner8ebeb032014-07-11 23:47:40 +02001420
Berker Peksag49c9edf2016-01-20 07:14:22 +02001421 The server is closed asynchronously, use the :meth:`wait_closed`
1422 coroutine to wait until the server is closed.
Victor Stinner8c462c52014-01-24 18:11:43 +01001423
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)1634fc22017-12-30 20:39:32 +05301424 .. method:: get_loop()
1425
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001426 Return the event loop associated with the server object.
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)1634fc22017-12-30 20:39:32 +05301427
1428 .. versionadded:: 3.7
1429
Yury Selivanovc9070d02018-01-25 18:08:09 -05001430 .. coroutinemethod:: start_serving()
1431
1432 Start accepting connections.
1433
1434 This method is idempotent, so it can be called when
1435 the server is already being serving.
1436
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001437 The *start_serving* keyword-only parameter to
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001438 :meth:`loop.create_server` and
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001439 :meth:`asyncio.start_server` allows creating a Server object
1440 that is not accepting connections initially. In this case
1441 ``Server.start_serving()``, or :meth:`Server.serve_forever` can be used
1442 to make the Server start accepting connections.
Yury Selivanovc9070d02018-01-25 18:08:09 -05001443
1444 .. versionadded:: 3.7
1445
1446 .. coroutinemethod:: serve_forever()
1447
1448 Start accepting connections until the coroutine is cancelled.
1449 Cancellation of ``serve_forever`` task causes the server
1450 to be closed.
1451
1452 This method can be called if the server is already accepting
1453 connections. Only one ``serve_forever`` task can exist per
1454 one *Server* object.
1455
1456 Example::
1457
1458 async def client_connected(reader, writer):
1459 # Communicate with the client with
1460 # reader/writer streams. For example:
1461 await reader.readline()
1462
1463 async def main(host, port):
1464 srv = await asyncio.start_server(
1465 client_connected, host, port)
Andrew Svetlov17ab8f02018-02-17 19:44:35 +02001466 await srv.serve_forever()
Yury Selivanovc9070d02018-01-25 18:08:09 -05001467
1468 asyncio.run(main('127.0.0.1', 0))
1469
1470 .. versionadded:: 3.7
1471
1472 .. method:: is_serving()
1473
1474 Return ``True`` if the server is accepting new connections.
1475
1476 .. versionadded:: 3.7
1477
Victor Stinnerbdd574d2015-02-12 22:49:18 +01001478 .. coroutinemethod:: wait_closed()
Victor Stinner8c462c52014-01-24 18:11:43 +01001479
Victor Stinner8ebeb032014-07-11 23:47:40 +02001480 Wait until the :meth:`close` method completes.
1481
Victor Stinner8ebeb032014-07-11 23:47:40 +02001482 .. attribute:: sockets
1483
Emmanuel Ariasdf5cdc12019-02-22 14:34:41 -03001484 List of :class:`socket.socket` objects the server is listening on.
Victor Stinner8c462c52014-01-24 18:11:43 +01001485
Yury Selivanovc9070d02018-01-25 18:08:09 -05001486 .. versionchanged:: 3.7
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001487 Prior to Python 3.7 ``Server.sockets`` used to return an
1488 internal list of server sockets directly. In 3.7 a copy
Yury Selivanovc9070d02018-01-25 18:08:09 -05001489 of that list is returned.
1490
Victor Stinner8c462c52014-01-24 18:11:43 +01001491
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001492.. _asyncio-event-loops:
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001493
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001494Event Loop Implementations
1495==========================
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001496
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001497asyncio ships with two different event loop implementations:
1498:class:`SelectorEventLoop` and :class:`ProactorEventLoop`.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001499
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001500By default asyncio is configured to use :class:`SelectorEventLoop`
Ben Darnell9ffca672019-06-22 13:38:21 -04001501on Unix and :class:`ProactorEventLoop` on Windows.
Andrew Svetlov3d4dbd82018-02-01 19:59:32 +02001502
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001503
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001504.. class:: SelectorEventLoop
1505
1506 An event loop based on the :mod:`selectors` module.
1507
1508 Uses the most efficient *selector* available for the given
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001509 platform. It is also possible to manually configure the
1510 exact selector implementation to be used::
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001511
1512 import asyncio
1513 import selectors
1514
1515 selector = selectors.SelectSelector()
1516 loop = asyncio.SelectorEventLoop(selector)
1517 asyncio.set_event_loop(loop)
Andrew Svetlov7464e872018-01-19 20:04:29 +02001518
1519
Cheryl Sabella2d6097d2018-10-12 10:55:20 -04001520 .. availability:: Unix, Windows.
Andrew Svetlov7464e872018-01-19 20:04:29 +02001521
1522
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001523.. class:: ProactorEventLoop
1524
1525 An event loop for Windows that uses "I/O Completion Ports" (IOCP).
1526
Cheryl Sabella2d6097d2018-10-12 10:55:20 -04001527 .. availability:: Windows.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001528
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001529 .. seealso::
1530
1531 `MSDN documentation on I/O Completion Ports
1532 <https://docs.microsoft.com/en-ca/windows/desktop/FileIO/i-o-completion-ports>`_.
1533
1534
1535.. class:: AbstractEventLoop
1536
1537 Abstract base class for asyncio-compliant event loops.
1538
1539 The :ref:`Event Loop Methods <asyncio-event-loop>` section lists all
1540 methods that an alternative implementation of ``AbstractEventLoop``
1541 should have defined.
1542
1543
1544Examples
1545========
1546
1547Note that all examples in this section **purposefully** show how
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001548to use the low-level event loop APIs, such as :meth:`loop.run_forever`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001549and :meth:`loop.call_soon`. Modern asyncio applications rarely
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001550need to be written this way; consider using the high-level functions
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001551like :func:`asyncio.run`.
1552
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001553
Yury Selivanov394374e2018-09-17 15:35:24 -04001554.. _asyncio_example_lowlevel_helloworld:
Victor Stinnerea3183f2013-12-03 01:08:00 +01001555
Victor Stinner7f314ed2014-10-15 18:49:16 +02001556Hello World with call_soon()
Victor Stinnera092a612015-01-09 15:58:41 +01001557^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +01001558
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001559An example using the :meth:`loop.call_soon` method to schedule a
1560callback. The callback displays ``"Hello World"`` and then stops the
1561event loop::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001562
1563 import asyncio
1564
Victor Stinner7f314ed2014-10-15 18:49:16 +02001565 def hello_world(loop):
Carol Willing5b7cbd62018-09-12 17:05:17 -07001566 """A callback to print 'Hello World' and stop the event loop"""
Victor Stinnerea3183f2013-12-03 01:08:00 +01001567 print('Hello World')
Victor Stinner7f314ed2014-10-15 18:49:16 +02001568 loop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001569
1570 loop = asyncio.get_event_loop()
Victor Stinner7f314ed2014-10-15 18:49:16 +02001571
1572 # Schedule a call to hello_world()
1573 loop.call_soon(hello_world, loop)
1574
1575 # Blocking call interrupted by loop.stop()
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001576 try:
1577 loop.run_forever()
1578 finally:
1579 loop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001580
Victor Stinner3e09e322013-12-03 01:22:06 +01001581.. seealso::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001582
Yury Selivanov3faaa882018-09-14 13:32:07 -07001583 A similar :ref:`Hello World <coroutine>`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001584 example created with a coroutine and the :func:`run` function.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001585
Victor Stinner8b863482014-01-27 10:07:50 +01001586
Yury Selivanov394374e2018-09-17 15:35:24 -04001587.. _asyncio_example_call_later:
Victor Stinner7f314ed2014-10-15 18:49:16 +02001588
1589Display the current date with call_later()
Victor Stinnera092a612015-01-09 15:58:41 +01001590^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner7f314ed2014-10-15 18:49:16 +02001591
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001592An example of a callback displaying the current date every second. The
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001593callback uses the :meth:`loop.call_later` method to reschedule itself
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001594after 5 seconds, and then stops the event loop::
Victor Stinner7f314ed2014-10-15 18:49:16 +02001595
1596 import asyncio
1597 import datetime
1598
1599 def display_date(end_time, loop):
1600 print(datetime.datetime.now())
1601 if (loop.time() + 1.0) < end_time:
1602 loop.call_later(1, display_date, end_time, loop)
1603 else:
1604 loop.stop()
1605
1606 loop = asyncio.get_event_loop()
1607
1608 # Schedule the first call to display_date()
1609 end_time = loop.time() + 5.0
1610 loop.call_soon(display_date, end_time, loop)
1611
1612 # Blocking call interrupted by loop.stop()
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001613 try:
1614 loop.run_forever()
1615 finally:
1616 loop.close()
Victor Stinner7f314ed2014-10-15 18:49:16 +02001617
1618.. seealso::
1619
Yury Selivanov7372c3b2018-09-14 15:11:24 -07001620 A similar :ref:`current date <asyncio_example_sleep>` example
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001621 created with a coroutine and the :func:`run` function.
Victor Stinner7f314ed2014-10-15 18:49:16 +02001622
1623
Yury Selivanov394374e2018-09-17 15:35:24 -04001624.. _asyncio_example_watch_fd:
Victor Stinner8b863482014-01-27 10:07:50 +01001625
Victor Stinner04e6df32014-10-11 16:16:27 +02001626Watch a file descriptor for read events
Victor Stinnera092a612015-01-09 15:58:41 +01001627^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001628
1629Wait until a file descriptor received some data using the
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001630:meth:`loop.add_reader` method and then close the event loop::
Victor Stinner04e6df32014-10-11 16:16:27 +02001631
1632 import asyncio
Victor Stinnerac577d72017-11-28 21:33:20 +01001633 from socket import socketpair
Victor Stinner04e6df32014-10-11 16:16:27 +02001634
1635 # Create a pair of connected file descriptors
Victor Stinnerccd8e342014-10-11 16:30:02 +02001636 rsock, wsock = socketpair()
Carol Willing5b7cbd62018-09-12 17:05:17 -07001637
Victor Stinner04e6df32014-10-11 16:16:27 +02001638 loop = asyncio.get_event_loop()
1639
1640 def reader():
1641 data = rsock.recv(100)
1642 print("Received:", data.decode())
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001643
Victor Stinner2cef3002014-10-23 22:38:46 +02001644 # We are done: unregister the file descriptor
Victor Stinner04e6df32014-10-11 16:16:27 +02001645 loop.remove_reader(rsock)
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001646
Victor Stinner04e6df32014-10-11 16:16:27 +02001647 # Stop the event loop
1648 loop.stop()
1649
Victor Stinner2cef3002014-10-23 22:38:46 +02001650 # Register the file descriptor for read event
Victor Stinner04e6df32014-10-11 16:16:27 +02001651 loop.add_reader(rsock, reader)
1652
1653 # Simulate the reception of data from the network
1654 loop.call_soon(wsock.send, 'abc'.encode())
1655
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001656 try:
1657 # Run the event loop
1658 loop.run_forever()
1659 finally:
Carol Willing5b7cbd62018-09-12 17:05:17 -07001660 # We are done. Close sockets and the event loop.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001661 rsock.close()
1662 wsock.close()
1663 loop.close()
Victor Stinner04e6df32014-10-11 16:16:27 +02001664
1665.. seealso::
1666
Yury Selivanov394374e2018-09-17 15:35:24 -04001667 * A similar :ref:`example <asyncio_example_create_connection>`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001668 using transports, protocols, and the
1669 :meth:`loop.create_connection` method.
Victor Stinner04e6df32014-10-11 16:16:27 +02001670
Yury Selivanov394374e2018-09-17 15:35:24 -04001671 * Another similar :ref:`example <asyncio_example_create_connection-streams>`
Yury Selivanov6758e6e2019-09-29 21:59:55 -07001672 using the high-level :func:`asyncio.open_connection` function
1673 and streams.
Victor Stinner04e6df32014-10-11 16:16:27 +02001674
1675
Yury Selivanov394374e2018-09-17 15:35:24 -04001676.. _asyncio_example_unix_signals:
1677
Victor Stinner04e6df32014-10-11 16:16:27 +02001678Set signal handlers for SIGINT and SIGTERM
Victor Stinnera092a612015-01-09 15:58:41 +01001679^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001680
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001681(This ``signals`` example only works on Unix.)
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001682
1683Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM`
1684using the :meth:`loop.add_signal_handler` method::
Victor Stinner8b863482014-01-27 10:07:50 +01001685
1686 import asyncio
1687 import functools
1688 import os
1689 import signal
1690
Alexander Vasinceb842e2019-05-03 18:25:36 +03001691 def ask_exit(signame, loop):
Victor Stinner8b863482014-01-27 10:07:50 +01001692 print("got signal %s: exit" % signame)
1693 loop.stop()
1694
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001695 async def main():
1696 loop = asyncio.get_running_loop()
Victor Stinner8b863482014-01-27 10:07:50 +01001697
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001698 for signame in {'SIGINT', 'SIGTERM'}:
1699 loop.add_signal_handler(
1700 getattr(signal, signame),
Alexander Vasinceb842e2019-05-03 18:25:36 +03001701 functools.partial(ask_exit, signame, loop))
Victor Stinner2cef3002014-10-23 22:38:46 +02001702
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001703 await asyncio.sleep(3600)
1704
1705 print("Event loop running for 1 hour, press Ctrl+C to interrupt.")
1706 print(f"pid {os.getpid()}: send SIGINT or SIGTERM to exit.")
1707
1708 asyncio.run(main())