blob: 44798dc3e23cb9b0eb24e356e838d76f5fa73191 [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
Serhiy Storchaka172c0f22021-04-25 13:40:44 +030056 .. deprecated:: 3.10
57 Deprecation warning is emitted if there is no running event loop.
Miss Islington (bot)1557cff2021-07-26 21:07:30 -070058 In future Python releases, this function will be an alias of
Serhiy Storchaka172c0f22021-04-25 13:40:44 +030059 :func:`get_running_loop`.
60
Yury Selivanov7c7605f2018-09-11 09:54:40 -070061.. function:: set_event_loop(loop)
62
63 Set *loop* as a current event loop for the current OS thread.
64
65.. function:: new_event_loop()
66
67 Create a new event loop object.
68
69Note that the behaviour of :func:`get_event_loop`, :func:`set_event_loop`,
70and :func:`new_event_loop` functions can be altered by
71:ref:`setting a custom event loop policy <asyncio-policies>`.
72
73
74.. rubric:: Contents
75
76This documentation page contains the following sections:
77
Carol Willing5b7cbd62018-09-12 17:05:17 -070078* The `Event Loop Methods`_ section is the reference documentation of
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040079 the event loop APIs;
Yury Selivanov7c7605f2018-09-11 09:54:40 -070080
81* The `Callback Handles`_ section documents the :class:`Handle` and
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040082 :class:`TimerHandle` instances which are returned from scheduling
83 methods such as :meth:`loop.call_soon` and :meth:`loop.call_later`;
Yury Selivanov7c7605f2018-09-11 09:54:40 -070084
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040085* The `Server Objects`_ section documents types returned from
Yury Selivanov7c7605f2018-09-11 09:54:40 -070086 event loop methods like :meth:`loop.create_server`;
87
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040088* The `Event Loop Implementations`_ section documents the
Yury Selivanov7c7605f2018-09-11 09:54:40 -070089 :class:`SelectorEventLoop` and :class:`ProactorEventLoop` classes;
90
91* The `Examples`_ section showcases how to work with some event
92 loop APIs.
93
94
Victor Stinner9592edb2014-02-02 15:03:02 +010095.. _asyncio-event-loop:
Victor Stinnerea3183f2013-12-03 01:08:00 +010096
Yury Selivanov7c7605f2018-09-11 09:54:40 -070097Event Loop Methods
98==================
Victor Stinnerea3183f2013-12-03 01:08:00 +010099
Carol Willing5b7cbd62018-09-12 17:05:17 -0700100Event loops have **low-level** APIs for the following:
lf627d2c82017-07-25 17:03:51 -0600101
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700102.. contents::
103 :depth: 1
104 :local:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100105
Victor Stinnerea3183f2013-12-03 01:08:00 +0100106
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700107Running and stopping the loop
108^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100109
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700110.. method:: loop.run_until_complete(future)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100111
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400112 Run until the *future* (an instance of :class:`Future`) has
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700113 completed.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100114
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700115 If the argument is a :ref:`coroutine object <coroutine>` it
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400116 is implicitly scheduled to run as a :class:`asyncio.Task`.
Eli Bendersky136fea22014-02-09 06:55:58 -0800117
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700118 Return the Future's result or raise its exception.
Guido van Rossumf68afd82016-08-08 09:41:21 -0700119
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700120.. method:: loop.run_forever()
Guido van Rossumf68afd82016-08-08 09:41:21 -0700121
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700122 Run the event loop until :meth:`stop` is called.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100123
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700124 If :meth:`stop` is called before :meth:`run_forever()` is called,
125 the loop will poll the I/O selector once with a timeout of zero,
126 run all callbacks scheduled in response to I/O events (and
127 those that were already scheduled), and then exit.
Victor Stinner83704962015-02-25 14:24:15 +0100128
Guido van Rossum41f69f42015-11-19 13:28:47 -0800129 If :meth:`stop` is called while :meth:`run_forever` is running,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700130 the loop will run the current batch of callbacks and then exit.
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400131 Note that new callbacks scheduled by callbacks will not run in this
Carol Willing5b7cbd62018-09-12 17:05:17 -0700132 case; instead, they will run the next time :meth:`run_forever` or
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700133 :meth:`run_until_complete` is called.
Guido van Rossum41f69f42015-11-19 13:28:47 -0800134
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700135.. method:: loop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100136
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700137 Stop the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100138
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700139.. method:: loop.is_running()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100140
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700141 Return ``True`` if the event loop is currently running.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100142
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700143.. method:: loop.is_closed()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100144
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700145 Return ``True`` if the event loop was closed.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100146
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700147.. method:: loop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100148
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700149 Close the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100150
Andriy Maletskyb83d9172018-10-29 23:39:21 +0200151 The loop must not be running when this function is called.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700152 Any pending callbacks will be discarded.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100153
Łukasz Langa7f9a2ae2019-06-04 13:03:20 +0200154 This method clears all queues and shuts down the executor, but does
155 not wait for the executor to finish.
Guido van Rossum41f69f42015-11-19 13:28:47 -0800156
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700157 This method is idempotent and irreversible. No other methods
158 should be called after the event loop is closed.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100159
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700160.. coroutinemethod:: loop.shutdown_asyncgens()
Yury Selivanov03660042016-12-15 17:36:05 -0500161
162 Schedule all currently open :term:`asynchronous generator` objects to
163 close with an :meth:`~agen.aclose()` call. After calling this method,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700164 the event loop will issue a warning if a new asynchronous generator
Carol Willing5b7cbd62018-09-12 17:05:17 -0700165 is iterated. This should be used to reliably finalize all scheduled
Yury Selivanovac94e382018-09-17 23:58:00 -0400166 asynchronous generators.
Carol Willing5b7cbd62018-09-12 17:05:17 -0700167
Yury Selivanovac94e382018-09-17 23:58:00 -0400168 Note that there is no need to call this function when
169 :func:`asyncio.run` is used.
170
171 Example::
Yury Selivanov03660042016-12-15 17:36:05 -0500172
173 try:
174 loop.run_forever()
175 finally:
176 loop.run_until_complete(loop.shutdown_asyncgens())
177 loop.close()
178
179 .. versionadded:: 3.6
180
Kyle Stanley9fdc64c2019-09-19 08:47:22 -0400181.. coroutinemethod:: loop.shutdown_default_executor()
182
183 Schedule the closure of the default executor and wait for it to join all of
184 the threads in the :class:`ThreadPoolExecutor`. After calling this method, a
185 :exc:`RuntimeError` will be raised if :meth:`loop.run_in_executor` is called
186 while using the default executor.
187
188 Note that there is no need to call this function when
189 :func:`asyncio.run` is used.
190
191 .. versionadded:: 3.9
192
Yury Selivanov03660042016-12-15 17:36:05 -0500193
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700194Scheduling callbacks
195^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100196
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700197.. method:: loop.call_soon(callback, *args, context=None)
Victor Stinner8464c242014-11-28 13:15:41 +0100198
Roger Iyengara16d6972020-06-22 22:16:00 -0400199 Schedule the *callback* :term:`callback` to be called with
200 *args* arguments at the next iteration of the event loop.
Victor Stinner8464c242014-11-28 13:15:41 +0100201
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700202 Callbacks are called in the order in which they are registered.
203 Each callback will be called exactly once.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100204
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700205 An optional keyword-only *context* argument allows specifying a
206 custom :class:`contextvars.Context` for the *callback* to run in.
207 The current context is used when no *context* is provided.
Yury Selivanov28b91782018-05-23 13:35:04 -0400208
Yury Selivanov1096f762015-06-25 13:49:52 -0400209 An instance of :class:`asyncio.Handle` is returned, which can be
Carol Willing5b7cbd62018-09-12 17:05:17 -0700210 used later to cancel the callback.
211
212 This method is not thread-safe.
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500213
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700214.. method:: loop.call_soon_threadsafe(callback, *args, context=None)
Victor Stinner8464c242014-11-28 13:15:41 +0100215
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700216 A thread-safe variant of :meth:`call_soon`. Must be used to
217 schedule callbacks *from another thread*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100218
Miss Islington (bot)bbb10762021-08-17 16:17:29 -0700219 Raises :exc:`RuntimeError` if called on a loop that's been closed.
220 This can happen on a secondary thread when the main application is
221 shutting down.
222
Victor Stinner83704962015-02-25 14:24:15 +0100223 See the :ref:`concurrency and multithreading <asyncio-multithreading>`
224 section of the documentation.
225
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700226.. versionchanged:: 3.7
227 The *context* keyword-only parameter was added. See :pep:`567`
228 for more details.
229
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400230.. _asyncio-pass-keywords:
231
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700232.. note::
233
Carol Willing5b7cbd62018-09-12 17:05:17 -0700234 Most :mod:`asyncio` scheduling functions don't allow passing
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400235 keyword arguments. To do that, use :func:`functools.partial`::
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700236
Carol Willing5b7cbd62018-09-12 17:05:17 -0700237 # will schedule "print("Hello", flush=True)"
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700238 loop.call_soon(
239 functools.partial(print, "Hello", flush=True))
240
241 Using partial objects is usually more convenient than using lambdas,
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400242 as asyncio can render partial objects better in debug and error
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700243 messages.
Yury Selivanov28b91782018-05-23 13:35:04 -0400244
Victor Stinnerea3183f2013-12-03 01:08:00 +0100245
Victor Stinner45b27ed2014-02-01 02:36:43 +0100246.. _asyncio-delayed-calls:
247
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700248Scheduling delayed callbacks
249^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100250
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700251Event loop provides mechanisms to schedule callback functions
252to be called at some point in the future. Event loop uses monotonic
253clocks to track time.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100254
Victor Stinner45b27ed2014-02-01 02:36:43 +0100255
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700256.. method:: loop.call_later(delay, callback, *args, context=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100257
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700258 Schedule *callback* to be called after the given *delay*
259 number of seconds (can be either an int or a float).
Victor Stinnerea3183f2013-12-03 01:08:00 +0100260
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700261 An instance of :class:`asyncio.TimerHandle` is returned which can
262 be used to cancel the callback.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100263
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700264 *callback* will be called exactly once. If two callbacks are
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400265 scheduled for exactly the same time, the order in which they
266 are called is undefined.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100267
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700268 The optional positional *args* will be passed to the callback when
269 it is called. If you want the callback to be called with keyword
270 arguments use :func:`functools.partial`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100271
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700272 An optional keyword-only *context* argument allows specifying a
273 custom :class:`contextvars.Context` for the *callback* to run in.
274 The current context is used when no *context* is provided.
Victor Stinner8464c242014-11-28 13:15:41 +0100275
Yury Selivanov28b91782018-05-23 13:35:04 -0400276 .. versionchanged:: 3.7
277 The *context* keyword-only parameter was added. See :pep:`567`
278 for more details.
279
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400280 .. versionchanged:: 3.8
281 In Python 3.7 and earlier with the default event loop implementation,
282 the *delay* could not exceed one day.
283 This has been fixed in Python 3.8.
284
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700285.. method:: loop.call_at(when, callback, *args, context=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100286
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700287 Schedule *callback* to be called at the given absolute timestamp
288 *when* (an int or a float), using the same time reference as
289 :meth:`loop.time`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100290
291 This method's behavior is the same as :meth:`call_later`.
292
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700293 An instance of :class:`asyncio.TimerHandle` is returned which can
294 be used to cancel the callback.
Victor Stinner8464c242014-11-28 13:15:41 +0100295
Yury Selivanov28b91782018-05-23 13:35:04 -0400296 .. versionchanged:: 3.7
297 The *context* keyword-only parameter was added. See :pep:`567`
298 for more details.
299
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400300 .. versionchanged:: 3.8
301 In Python 3.7 and earlier with the default event loop implementation,
302 the difference between *when* and the current time could not exceed
303 one day. This has been fixed in Python 3.8.
304
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700305.. method:: loop.time()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100306
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700307 Return the current time, as a :class:`float` value, according to
308 the event loop's internal monotonic clock.
309
310.. note::
Enrico Alarico Carbognani7e954e72019-04-18 14:43:14 +0200311 .. versionchanged:: 3.8
312 In Python 3.7 and earlier timeouts (relative *delay* or absolute *when*)
313 should not exceed one day. This has been fixed in Python 3.8.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100314
Victor Stinner3e09e322013-12-03 01:22:06 +0100315.. seealso::
316
317 The :func:`asyncio.sleep` function.
318
Victor Stinnerea3183f2013-12-03 01:08:00 +0100319
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700320Creating Futures and Tasks
321^^^^^^^^^^^^^^^^^^^^^^^^^^
Yury Selivanov950204d2016-05-16 16:23:00 -0400322
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700323.. method:: loop.create_future()
Yury Selivanov950204d2016-05-16 16:23:00 -0400324
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700325 Create an :class:`asyncio.Future` object attached to the event loop.
Yury Selivanov950204d2016-05-16 16:23:00 -0400326
Carol Willing5b7cbd62018-09-12 17:05:17 -0700327 This is the preferred way to create Futures in asyncio. This lets
328 third-party event loops provide alternative implementations of
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700329 the Future object (with better performance or instrumentation).
Yury Selivanov950204d2016-05-16 16:23:00 -0400330
331 .. versionadded:: 3.5.2
332
Andre Delfinodcc997c2020-12-16 22:37:28 -0300333.. method:: loop.create_task(coro, *, name=None)
Yury Selivanov950204d2016-05-16 16:23:00 -0400334
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700335 Schedule the execution of a :ref:`coroutine`.
336 Return a :class:`Task` object.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200337
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700338 Third-party event loops can use their own subclass of :class:`Task`
339 for interoperability. In this case, the result type is a subclass
340 of :class:`Task`.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200341
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700342 If the *name* argument is provided and not ``None``, it is set as
343 the name of the task using :meth:`Task.set_name`.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200344
Alex Grönholmcca4eec2018-08-09 00:06:47 +0300345 .. versionchanged:: 3.8
346 Added the ``name`` parameter.
347
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700348.. method:: loop.set_task_factory(factory)
Yury Selivanov71854612015-05-11 16:28:27 -0400349
350 Set a task factory that will be used by
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700351 :meth:`loop.create_task`.
Yury Selivanov71854612015-05-11 16:28:27 -0400352
353 If *factory* is ``None`` the default task factory will be set.
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400354 Otherwise, *factory* must be a *callable* with the signature matching
355 ``(loop, coro)``, where *loop* is a reference to the active
356 event loop, and *coro* is a coroutine object. The callable
357 must return a :class:`asyncio.Future`-compatible object.
Yury Selivanov71854612015-05-11 16:28:27 -0400358
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700359.. method:: loop.get_task_factory()
Yury Selivanov71854612015-05-11 16:28:27 -0400360
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700361 Return a task factory or ``None`` if the default one is in use.
Yury Selivanov71854612015-05-11 16:28:27 -0400362
Victor Stinner530ef2f2014-07-08 12:39:10 +0200363
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700364Opening network connections
365^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100366
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700367.. coroutinemethod:: loop.create_connection(protocol_factory, \
Andre Delfinodcc997c2020-12-16 22:37:28 -0300368 host=None, port=None, *, ssl=None, \
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700369 family=0, proto=0, flags=0, sock=None, \
370 local_addr=None, server_hostname=None, \
idomic5305cc92020-02-10 04:48:40 -0500371 ssl_handshake_timeout=None, \
372 happy_eyeballs_delay=None, interleave=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100373
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700374 Open a streaming transport connection to a given
375 address specified by *host* and *port*.
376
377 The socket family can be either :py:data:`~socket.AF_INET` or
378 :py:data:`~socket.AF_INET6` depending on *host* (or the *family*
379 argument, if provided).
380
381 The socket type will be :py:data:`~socket.SOCK_STREAM`.
382
383 *protocol_factory* must be a callable returning an
384 :ref:`asyncio protocol <asyncio-protocol>` implementation.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100385
Yury Selivanov19a44f62017-12-14 20:53:26 -0500386 This method will try to establish the connection in the background.
387 When successful, it returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100388
389 The chronological synopsis of the underlying operation is as follows:
390
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700391 #. The connection is established and a :ref:`transport <asyncio-transport>`
392 is created for it.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100393
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700394 #. *protocol_factory* is called without arguments and is expected to
395 return a :ref:`protocol <asyncio-protocol>` instance.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100396
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700397 #. The protocol instance is coupled with the transport by calling its
398 :meth:`~BaseProtocol.connection_made` method.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100399
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700400 #. A ``(transport, protocol)`` tuple is returned on success.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100401
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700402 The created transport is an implementation-dependent bidirectional
403 stream.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100404
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700405 Other arguments:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100406
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400407 * *ssl*: if given and not false, a SSL/TLS transport is created
Victor Stinnerea3183f2013-12-03 01:08:00 +0100408 (by default a plain TCP transport is created). If *ssl* is
409 a :class:`ssl.SSLContext` object, this context is used to create
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400410 the transport; if *ssl* is :const:`True`, a default context returned
411 from :func:`ssl.create_default_context` is used.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100412
Berker Peksag9c1dba22014-09-28 00:00:58 +0300413 .. seealso:: :ref:`SSL/TLS security considerations <ssl-security>`
Antoine Pitrouc5e075f2014-03-22 18:19:11 +0100414
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400415 * *server_hostname* sets or overrides the hostname that the target
416 server's certificate will be matched against. Should only be passed
417 if *ssl* is not ``None``. By default the value of the *host* argument
Victor Stinnerea3183f2013-12-03 01:08:00 +0100418 is used. If *host* is empty, there is no default and you must pass a
419 value for *server_hostname*. If *server_hostname* is an empty
420 string, hostname matching is disabled (which is a serious security
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400421 risk, allowing for potential man-in-the-middle attacks).
Victor Stinnerea3183f2013-12-03 01:08:00 +0100422
423 * *family*, *proto*, *flags* are the optional address family, protocol
424 and flags to be passed through to getaddrinfo() for *host* resolution.
425 If given, these should all be integers from the corresponding
426 :mod:`socket` module constants.
427
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800428 * *happy_eyeballs_delay*, if given, enables Happy Eyeballs for this
429 connection. It should
430 be a floating-point number representing the amount of time in seconds
431 to wait for a connection attempt to complete, before starting the next
432 attempt in parallel. This is the "Connection Attempt Delay" as defined
433 in :rfc:`8305`. A sensible default value recommended by the RFC is ``0.25``
434 (250 milliseconds).
435
436 * *interleave* controls address reordering when a host name resolves to
437 multiple IP addresses.
438 If ``0`` or unspecified, no reordering is done, and addresses are
439 tried in the order returned by :meth:`getaddrinfo`. If a positive integer
440 is specified, the addresses are interleaved by address family, and the
441 given integer is interpreted as "First Address Family Count" as defined
442 in :rfc:`8305`. The default is ``0`` if *happy_eyeballs_delay* is not
443 specified, and ``1`` if it is.
444
Victor Stinnerea3183f2013-12-03 01:08:00 +0100445 * *sock*, if given, should be an existing, already connected
446 :class:`socket.socket` object to be used by the transport.
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800447 If *sock* is given, none of *host*, *port*, *family*, *proto*, *flags*,
448 *happy_eyeballs_delay*, *interleave*
Victor Stinnerea3183f2013-12-03 01:08:00 +0100449 and *local_addr* should be specified.
450
451 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
Miss Islington (bot)9d16b1a2021-05-19 14:19:53 -0700452 to bind the socket locally. The *local_host* and *local_port*
Carol Willing5b7cbd62018-09-12 17:05:17 -0700453 are looked up using ``getaddrinfo()``, similarly to *host* and *port*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100454
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400455 * *ssl_handshake_timeout* is (for a TLS connection) the time in seconds
456 to wait for the TLS handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400457 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000458
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800459 .. versionadded:: 3.8
460
idomic5305cc92020-02-10 04:48:40 -0500461 Added the *happy_eyeballs_delay* and *interleave* parameters.
twisteroid ambassador88f07a82019-05-05 19:14:35 +0800462
idomic8af47122020-02-24 09:59:40 -0500463 Happy Eyeballs Algorithm: Success with Dual-Stack Hosts.
464 When a server's IPv4 path and protocol are working, but the server's
465 IPv6 path and protocol are not working, a dual-stack client
466 application experiences significant connection delay compared to an
467 IPv4-only client. This is undesirable because it causes the dual-
468 stack client to have a worse user experience. This document
469 specifies requirements for algorithms that reduce this user-visible
470 delay and provides an algorithm.
471
472 For more information: https://tools.ietf.org/html/rfc6555
473
Neil Aspinallf7686c12017-12-19 19:45:42 +0000474 .. versionadded:: 3.7
475
476 The *ssl_handshake_timeout* parameter.
477
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700478 .. versionchanged:: 3.6
479
480 The socket option :py:data:`~socket.TCP_NODELAY` is set by default
481 for all TCP connections.
482
Victor Stinner60208a12015-09-15 22:41:52 +0200483 .. versionchanged:: 3.5
484
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400485 Added support for SSL/TLS in :class:`ProactorEventLoop`.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200486
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100487 .. seealso::
488
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700489 The :func:`open_connection` function is a high-level alternative
490 API. It returns a pair of (:class:`StreamReader`, :class:`StreamWriter`)
491 that can be used directly in async/await code.
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100492
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700493.. coroutinemethod:: loop.create_datagram_endpoint(protocol_factory, \
Andre Delfinodcc997c2020-12-16 22:37:28 -0300494 local_addr=None, remote_addr=None, *, \
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700495 family=0, proto=0, flags=0, \
496 reuse_address=None, reuse_port=None, \
497 allow_broadcast=None, sock=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100498
Kyle Stanleyab513a32019-12-09 09:21:10 -0500499 .. note::
500 The parameter *reuse_address* is no longer supported, as using
501 :py:data:`~sockets.SO_REUSEADDR` poses a significant security concern for
502 UDP. Explicitly passing ``reuse_address=True`` will raise an exception.
503
504 When multiple processes with differing UIDs assign sockets to an
Jesús Ceab0d49492019-12-20 03:21:03 +0100505 identical UDP socket address with ``SO_REUSEADDR``, incoming packets can
Kyle Stanleyab513a32019-12-09 09:21:10 -0500506 become randomly distributed among the sockets.
507
508 For supported platforms, *reuse_port* can be used as a replacement for
509 similar functionality. With *reuse_port*,
510 :py:data:`~sockets.SO_REUSEPORT` is used instead, which specifically
511 prevents processes with differing UIDs from assigning sockets to the same
512 socket address.
513
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700514 Create a datagram connection.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100515
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700516 The socket family can be either :py:data:`~socket.AF_INET`,
517 :py:data:`~socket.AF_INET6`, or :py:data:`~socket.AF_UNIX`,
518 depending on *host* (or the *family* argument, if provided).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100519
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700520 The socket type will be :py:data:`~socket.SOCK_DGRAM`.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100521
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700522 *protocol_factory* must be a callable returning a
523 :ref:`protocol <asyncio-protocol>` implementation.
524
525 A tuple of ``(transport, protocol)`` is returned on success.
526
527 Other arguments:
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700528
529 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
Miss Islington (bot)9d16b1a2021-05-19 14:19:53 -0700530 to bind the socket locally. The *local_host* and *local_port*
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700531 are looked up using :meth:`getaddrinfo`.
532
533 * *remote_addr*, if given, is a ``(remote_host, remote_port)`` tuple used
534 to connect the socket to a remote address. The *remote_host* and
535 *remote_port* are looked up using :meth:`getaddrinfo`.
536
537 * *family*, *proto*, *flags* are the optional address family, protocol
538 and flags to be passed through to :meth:`getaddrinfo` for *host*
539 resolution. If given, these should all be integers from the
540 corresponding :mod:`socket` module constants.
541
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700542 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
543 same port as other existing endpoints are bound to, so long as they all
544 set this flag when being created. This option is not supported on Windows
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400545 and some Unixes. If the :py:data:`~socket.SO_REUSEPORT` constant is not
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700546 defined then this capability is unsupported.
547
548 * *allow_broadcast* tells the kernel to allow this endpoint to send
549 messages to the broadcast address.
550
551 * *sock* can optionally be specified in order to use a preexisting,
552 already connected, :class:`socket.socket` object to be used by the
553 transport. If specified, *local_addr* and *remote_addr* should be omitted
554 (must be :const:`None`).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100555
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200556 See :ref:`UDP echo client protocol <asyncio-udp-echo-client-protocol>` and
557 :ref:`UDP echo server protocol <asyncio-udp-echo-server-protocol>` examples.
558
Romuald Brunet0ded5802018-05-14 18:22:00 +0200559 .. versionchanged:: 3.4.4
560 The *family*, *proto*, *flags*, *reuse_address*, *reuse_port,
561 *allow_broadcast*, and *sock* parameters were added.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100562
Kyle Stanleyab513a32019-12-09 09:21:10 -0500563 .. versionchanged:: 3.8.1
564 The *reuse_address* parameter is no longer supported due to security
565 concerns.
566
Andrew Svetlovbafd4b52019-05-28 12:52:15 +0300567 .. versionchanged:: 3.8
568 Added support for Windows.
569
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700570.. coroutinemethod:: loop.create_unix_connection(protocol_factory, \
Andre Delfinodcc997c2020-12-16 22:37:28 -0300571 path=None, *, ssl=None, sock=None, \
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700572 server_hostname=None, ssl_handshake_timeout=None)
Victor Stinnera6919aa2014-02-19 13:32:34 +0100573
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400574 Create a Unix connection.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100575
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700576 The socket family will be :py:data:`~socket.AF_UNIX`; socket
577 type will be :py:data:`~socket.SOCK_STREAM`.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100578
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700579 A tuple of ``(transport, protocol)`` is returned on success.
Guido van Rossum9e80eeb2016-11-03 14:17:25 -0700580
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400581 *path* is the name of a Unix domain socket and is required,
582 unless a *sock* parameter is specified. Abstract Unix sockets,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700583 :class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths are
584 supported.
585
586 See the documentation of the :meth:`loop.create_connection` method
587 for information about arguments to this method.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100588
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400589 .. availability:: Unix.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100590
Neil Aspinallf7686c12017-12-19 19:45:42 +0000591 .. versionadded:: 3.7
592
593 The *ssl_handshake_timeout* parameter.
594
Yury Selivanov423fd362017-11-20 17:26:28 -0500595 .. versionchanged:: 3.7
596
Elvis Pranskevichusc0d062f2018-06-08 11:36:00 -0400597 The *path* parameter can now be a :term:`path-like object`.
Yury Selivanov423fd362017-11-20 17:26:28 -0500598
Victor Stinnera6919aa2014-02-19 13:32:34 +0100599
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700600Creating network servers
601^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100602
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700603.. coroutinemethod:: loop.create_server(protocol_factory, \
Andre Delfinodcc997c2020-12-16 22:37:28 -0300604 host=None, port=None, *, \
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700605 family=socket.AF_UNSPEC, \
606 flags=socket.AI_PASSIVE, \
607 sock=None, backlog=100, ssl=None, \
608 reuse_address=None, reuse_port=None, \
609 ssl_handshake_timeout=None, start_serving=True)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100610
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700611 Create a TCP server (socket type :data:`~socket.SOCK_STREAM`) listening
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400612 on *port* of the *host* address.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200613
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700614 Returns a :class:`Server` object.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200615
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700616 Arguments:
Victor Stinner33f6abe2014-10-12 20:36:04 +0200617
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400618 * *protocol_factory* must be a callable returning a
619 :ref:`protocol <asyncio-protocol>` implementation.
620
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400621 * The *host* parameter can be set to several types which determine where
622 the server would be listening:
623
624 - If *host* is a string, the TCP server is bound to a single network
625 interface specified by *host*.
626
627 - If *host* is a sequence of strings, the TCP server is bound to all
628 network interfaces specified by the sequence.
629
630 - If *host* is an empty string or ``None``, all interfaces are
631 assumed and a list of multiple sockets will be returned (most likely
632 one for IPv4 and another one for IPv6).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200633
Miss Islington (bot)8cabcde2021-11-24 12:39:51 -0800634 * The *port* parameter can be set to specify which port the server should
635 listen on. If ``0`` or ``None`` (the default), a random unused port will
636 be selected (note that if *host* resolves to multiple network interfaces,
637 a different random port will be selected for each interface).
638
Victor Stinner33f6abe2014-10-12 20:36:04 +0200639 * *family* can be set to either :data:`socket.AF_INET` or
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700640 :data:`~socket.AF_INET6` to force the socket to use IPv4 or IPv6.
Carol Willing5b7cbd62018-09-12 17:05:17 -0700641 If not set, the *family* will be determined from host name
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700642 (defaults to :data:`~socket.AF_UNSPEC`).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200643
644 * *flags* is a bitmask for :meth:`getaddrinfo`.
645
646 * *sock* can optionally be specified in order to use a preexisting
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400647 socket object. If specified, *host* and *port* must not be specified.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200648
649 * *backlog* is the maximum number of queued connections passed to
650 :meth:`~socket.socket.listen` (defaults to 100).
651
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400652 * *ssl* can be set to an :class:`~ssl.SSLContext` instance to enable
653 TLS over the accepted connections.
Victor Stinner33f6abe2014-10-12 20:36:04 +0200654
655 * *reuse_address* tells the kernel to reuse a local socket in
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700656 ``TIME_WAIT`` state, without waiting for its natural timeout to
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300657 expire. If not specified will automatically be set to ``True`` on
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400658 Unix.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100659
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700660 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
661 same port as other existing endpoints are bound to, so long as they all
662 set this flag when being created. This option is not supported on
663 Windows.
664
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400665 * *ssl_handshake_timeout* is (for a TLS server) the time in seconds to wait
666 for the TLS handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400667 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000668
Yury Selivanovc9070d02018-01-25 18:08:09 -0500669 * *start_serving* set to ``True`` (the default) causes the created server
670 to start accepting connections immediately. When set to ``False``,
671 the user should await on :meth:`Server.start_serving` or
672 :meth:`Server.serve_forever` to make the server to start accepting
673 connections.
674
Neil Aspinallf7686c12017-12-19 19:45:42 +0000675 .. versionadded:: 3.7
676
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700677 Added *ssl_handshake_timeout* and *start_serving* parameters.
678
679 .. versionchanged:: 3.6
680
681 The socket option :py:data:`~socket.TCP_NODELAY` is set by default
682 for all TCP connections.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000683
Victor Stinner60208a12015-09-15 22:41:52 +0200684 .. versionchanged:: 3.5
685
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400686 Added support for SSL/TLS in :class:`ProactorEventLoop`.
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100687
Victor Stinner7b58a2b2015-09-21 18:41:05 +0200688 .. versionchanged:: 3.5.1
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200689
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700690 The *host* parameter can be a sequence of strings.
691
692 .. seealso::
693
694 The :func:`start_server` function is a higher-level alternative API
695 that returns a pair of :class:`StreamReader` and :class:`StreamWriter`
696 that can be used in an async/await code.
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200697
Victor Stinnerea3183f2013-12-03 01:08:00 +0100698
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700699.. coroutinemethod:: loop.create_unix_server(protocol_factory, path=None, \
Andre Delfinodcc997c2020-12-16 22:37:28 -0300700 *, sock=None, backlog=100, ssl=None, \
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700701 ssl_handshake_timeout=None, start_serving=True)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100702
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700703 Similar to :meth:`loop.create_server` but works with the
704 :py:data:`~socket.AF_UNIX` socket family.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100705
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400706 *path* is the name of a Unix domain socket, and is required,
707 unless a *sock* argument is provided. Abstract Unix sockets,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700708 :class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths
709 are supported.
Yury Selivanov423fd362017-11-20 17:26:28 -0500710
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400711 See the documentation of the :meth:`loop.create_server` method
712 for information about arguments to this method.
713
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400714 .. availability:: Unix.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100715
Neil Aspinallf7686c12017-12-19 19:45:42 +0000716 .. versionadded:: 3.7
717
Elvis Pranskevichusc0d062f2018-06-08 11:36:00 -0400718 The *ssl_handshake_timeout* and *start_serving* parameters.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000719
Yury Selivanov423fd362017-11-20 17:26:28 -0500720 .. versionchanged:: 3.7
721
722 The *path* parameter can now be a :class:`~pathlib.Path` object.
723
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700724.. coroutinemethod:: loop.connect_accepted_socket(protocol_factory, \
Andre Delfinodcc997c2020-12-16 22:37:28 -0300725 sock, *, ssl=None, ssl_handshake_timeout=None)
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500726
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700727 Wrap an already accepted connection into a transport/protocol pair.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500728
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700729 This method can be used by servers that accept connections outside
730 of asyncio but that use asyncio to handle them.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500731
732 Parameters:
733
Bumsik Kim5cc583d2018-09-16 19:40:44 -0400734 * *protocol_factory* must be a callable returning a
735 :ref:`protocol <asyncio-protocol>` implementation.
736
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700737 * *sock* is a preexisting socket object returned from
738 :meth:`socket.accept <socket.socket.accept>`.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500739
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700740 * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over
741 the accepted connections.
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500742
Neil Aspinallf7686c12017-12-19 19:45:42 +0000743 * *ssl_handshake_timeout* is (for an SSL connection) the time in seconds to
744 wait for the SSL handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400745 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000746
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700747 Returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100748
Neil Aspinallf7686c12017-12-19 19:45:42 +0000749 .. versionadded:: 3.7
750
751 The *ssl_handshake_timeout* parameter.
752
AraHaan431665b2017-11-21 11:06:26 -0500753 .. versionadded:: 3.5.3
754
755
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700756Transferring files
757^^^^^^^^^^^^^^^^^^
Andrew Svetlov7c684072018-01-27 21:22:47 +0200758
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700759.. coroutinemethod:: loop.sendfile(transport, file, \
760 offset=0, count=None, *, fallback=True)
Andrew Svetlov7c684072018-01-27 21:22:47 +0200761
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700762 Send a *file* over a *transport*. Return the total number of bytes
763 sent.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200764
765 The method uses high-performance :meth:`os.sendfile` if available.
766
767 *file* must be a regular file object opened in binary mode.
768
769 *offset* tells from where to start reading the file. If specified,
770 *count* is the total number of bytes to transmit as opposed to
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400771 sending the file until EOF is reached. File position is always updated,
772 even when this method raises an error, and
773 :meth:`file.tell() <io.IOBase.tell>` can be used to obtain the actual
774 number of bytes sent.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200775
776 *fallback* set to ``True`` makes asyncio to manually read and send
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700777 the file when the platform does not support the sendfile system call
Andrew Svetlov7c684072018-01-27 21:22:47 +0200778 (e.g. Windows or SSL socket on Unix).
779
780 Raise :exc:`SendfileNotAvailableError` if the system does not support
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400781 the *sendfile* syscall and *fallback* is ``False``.
Andrew Svetlov7c684072018-01-27 21:22:47 +0200782
783 .. versionadded:: 3.7
784
785
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500786TLS Upgrade
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700787^^^^^^^^^^^
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500788
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700789.. coroutinemethod:: loop.start_tls(transport, protocol, \
Andre Delfinodcc997c2020-12-16 22:37:28 -0300790 sslcontext, *, server_side=False, \
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700791 server_hostname=None, ssl_handshake_timeout=None)
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500792
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700793 Upgrade an existing transport-based connection to TLS.
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500794
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700795 Return a new transport instance, that the *protocol* must start using
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500796 immediately after the *await*. The *transport* instance passed to
797 the *start_tls* method should never be used again.
798
799 Parameters:
800
801 * *transport* and *protocol* instances that methods like
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700802 :meth:`~loop.create_server` and
803 :meth:`~loop.create_connection` return.
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500804
805 * *sslcontext*: a configured instance of :class:`~ssl.SSLContext`.
806
807 * *server_side* pass ``True`` when a server-side connection is being
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700808 upgraded (like the one created by :meth:`~loop.create_server`).
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500809
810 * *server_hostname*: sets or overrides the host name that the target
811 server's certificate will be matched against.
812
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400813 * *ssl_handshake_timeout* is (for a TLS connection) the time in seconds to
814 wait for the TLS handshake to complete before aborting the connection.
Yury Selivanov96026432018-06-04 11:32:35 -0400815 ``60.0`` seconds if ``None`` (default).
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500816
817 .. versionadded:: 3.7
818
819
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700820Watching file descriptors
821^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerc1567df2014-02-08 23:22:58 +0100822
Andre Delfinodcc997c2020-12-16 22:37:28 -0300823.. method:: loop.add_reader(fd, callback, *args)
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200824
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400825 Start monitoring the *fd* file descriptor for read availability and
826 invoke *callback* with the specified arguments once *fd* is available for
827 reading.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200828
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700829.. method:: loop.remove_reader(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100830
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400831 Stop monitoring the *fd* file descriptor for read availability.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100832
Andre Delfinodcc997c2020-12-16 22:37:28 -0300833.. method:: loop.add_writer(fd, callback, *args)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100834
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400835 Start monitoring the *fd* file descriptor for write availability and
836 invoke *callback* with the specified arguments once *fd* is available for
837 writing.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100838
Yury Selivanove247b462018-09-20 12:43:59 -0400839 Use :func:`functools.partial` :ref:`to pass keyword arguments
Naglis17473342018-12-04 09:31:15 +0200840 <asyncio-pass-keywords>` to *callback*.
Victor Stinner8464c242014-11-28 13:15:41 +0100841
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700842.. method:: loop.remove_writer(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100843
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400844 Stop monitoring the *fd* file descriptor for write availability.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100845
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700846See also :ref:`Platform Support <asyncio-platform-support>` section
847for some limitations of these methods.
Victor Stinner04e6df32014-10-11 16:16:27 +0200848
Victor Stinnerc1567df2014-02-08 23:22:58 +0100849
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700850Working with socket objects directly
851^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerc1567df2014-02-08 23:22:58 +0100852
Carol Willing5b7cbd62018-09-12 17:05:17 -0700853In general, protocol implementations that use transport-based APIs
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700854such as :meth:`loop.create_connection` and :meth:`loop.create_server`
855are faster than implementations that work with sockets directly.
Carol Willing5b7cbd62018-09-12 17:05:17 -0700856However, there are some use cases when performance is not critical, and
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700857working with :class:`~socket.socket` objects directly is more
858convenient.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100859
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700860.. coroutinemethod:: loop.sock_recv(sock, nbytes)
Yury Selivanov55c50842016-06-08 12:48:15 -0400861
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400862 Receive up to *nbytes* from *sock*. Asynchronous version of
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700863 :meth:`socket.recv() <socket.socket.recv>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100864
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400865 Return the received data as a bytes object.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700866
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400867 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200868
Yury Selivanov19a44f62017-12-14 20:53:26 -0500869 .. versionchanged:: 3.7
Carol Willing5b7cbd62018-09-12 17:05:17 -0700870 Even though this method was always documented as a coroutine
871 method, releases before Python 3.7 returned a :class:`Future`.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700872 Since Python 3.7 this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100873
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700874.. coroutinemethod:: loop.sock_recv_into(sock, buf)
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200875
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400876 Receive data from *sock* into the *buf* buffer. Modeled after the blocking
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700877 :meth:`socket.recv_into() <socket.socket.recv_into>` method.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200878
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700879 Return the number of bytes written to the buffer.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200880
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400881 *sock* must be a non-blocking socket.
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200882
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200883 .. versionadded:: 3.7
884
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700885.. coroutinemethod:: loop.sock_sendall(sock, data)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100886
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400887 Send *data* to the *sock* socket. Asynchronous version of
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700888 :meth:`socket.sendall() <socket.socket.sendall>`.
Yury Selivanov55c50842016-06-08 12:48:15 -0400889
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400890 This method continues to send to the socket until either all data
891 in *data* has been sent or an error occurs. ``None`` is returned
Carol Willing5b7cbd62018-09-12 17:05:17 -0700892 on success. On error, an exception is raised. Additionally, there is no way
893 to determine how much data, if any, was successfully processed by the
894 receiving end of the connection.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100895
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400896 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200897
Yury Selivanov19a44f62017-12-14 20:53:26 -0500898 .. versionchanged:: 3.7
899 Even though the method was always documented as a coroutine
900 method, before Python 3.7 it returned an :class:`Future`.
901 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100902
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700903.. coroutinemethod:: loop.sock_connect(sock, address)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100904
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400905 Connect *sock* to a remote socket at *address*.
Victor Stinner1b0580b2014-02-13 09:24:37 +0100906
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700907 Asynchronous version of :meth:`socket.connect() <socket.socket.connect>`.
908
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400909 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200910
Yury Selivanov55c50842016-06-08 12:48:15 -0400911 .. versionchanged:: 3.5.2
912 ``address`` no longer needs to be resolved. ``sock_connect``
913 will try to check if the *address* is already resolved by calling
914 :func:`socket.inet_pton`. If not,
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700915 :meth:`loop.getaddrinfo` will be used to resolve the
Yury Selivanov55c50842016-06-08 12:48:15 -0400916 *address*.
917
Victor Stinnerc1567df2014-02-08 23:22:58 +0100918 .. seealso::
919
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700920 :meth:`loop.create_connection`
Yury Selivanov55c50842016-06-08 12:48:15 -0400921 and :func:`asyncio.open_connection() <open_connection>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100922
923
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700924.. coroutinemethod:: loop.sock_accept(sock)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100925
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700926 Accept a connection. Modeled after the blocking
927 :meth:`socket.accept() <socket.socket.accept>` method.
Yury Selivanov55c50842016-06-08 12:48:15 -0400928
929 The socket must be bound to an address and listening
Victor Stinnerc1567df2014-02-08 23:22:58 +0100930 for connections. The return value is a pair ``(conn, address)`` where *conn*
931 is a *new* socket object usable to send and receive data on the connection,
932 and *address* is the address bound to the socket on the other end of the
933 connection.
934
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400935 *sock* must be a non-blocking socket.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200936
Yury Selivanov19a44f62017-12-14 20:53:26 -0500937 .. versionchanged:: 3.7
938 Even though the method was always documented as a coroutine
939 method, before Python 3.7 it returned a :class:`Future`.
940 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100941
942 .. seealso::
943
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700944 :meth:`loop.create_server` and :func:`start_server`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100945
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700946.. coroutinemethod:: loop.sock_sendfile(sock, file, offset=0, count=None, \
Andre Delfinodcc997c2020-12-16 22:37:28 -0300947 *, fallback=True)
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200948
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700949 Send a file using high-performance :mod:`os.sendfile` if possible.
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400950 Return the total number of bytes sent.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200951
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700952 Asynchronous version of :meth:`socket.sendfile() <socket.socket.sendfile>`.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200953
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400954 *sock* must be a non-blocking :const:`socket.SOCK_STREAM`
955 :class:`~socket.socket`.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200956
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400957 *file* must be a regular file object open in binary mode.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200958
959 *offset* tells from where to start reading the file. If specified,
960 *count* is the total number of bytes to transmit as opposed to
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400961 sending the file until EOF is reached. File position is always updated,
962 even when this method raises an error, and
963 :meth:`file.tell() <io.IOBase.tell>` can be used to obtain the actual
964 number of bytes sent.
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200965
Carol Willing5b7cbd62018-09-12 17:05:17 -0700966 *fallback*, when set to ``True``, makes asyncio manually read and send
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200967 the file when the platform does not support the sendfile syscall
968 (e.g. Windows or SSL socket on Unix).
969
Andrew Svetlov7464e872018-01-19 20:04:29 +0200970 Raise :exc:`SendfileNotAvailableError` if the system does not support
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200971 *sendfile* syscall and *fallback* is ``False``.
972
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400973 *sock* must be a non-blocking socket.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700974
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200975 .. versionadded:: 3.7
976
Victor Stinnerc1567df2014-02-08 23:22:58 +0100977
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700978DNS
979^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100980
Andre Delfinodcc997c2020-12-16 22:37:28 -0300981.. coroutinemethod:: loop.getaddrinfo(host, port, *, family=0, \
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700982 type=0, proto=0, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100983
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700984 Asynchronous version of :meth:`socket.getaddrinfo`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100985
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700986.. coroutinemethod:: loop.getnameinfo(sockaddr, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100987
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700988 Asynchronous version of :meth:`socket.getnameinfo`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100989
Yury Selivanovbec23722018-01-28 14:09:40 -0500990.. versionchanged:: 3.7
991 Both *getaddrinfo* and *getnameinfo* methods were always documented
992 to return a coroutine, but prior to Python 3.7 they were, in fact,
993 returning :class:`asyncio.Future` objects. Starting with Python 3.7
994 both methods are coroutines.
995
Victor Stinnerea3183f2013-12-03 01:08:00 +0100996
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700997Working with pipes
998^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100999
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001000.. coroutinemethod:: loop.connect_read_pipe(protocol_factory, pipe)
Victor Stinner41f3c3f2014-08-31 14:47:37 +02001001
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001002 Register the read end of *pipe* in the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001003
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001004 *protocol_factory* must be a callable returning an
1005 :ref:`asyncio protocol <asyncio-protocol>` implementation.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001006
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001007 *pipe* is a :term:`file-like object <file object>`.
1008
1009 Return pair ``(transport, protocol)``, where *transport* supports
Bumsik Kim5cc583d2018-09-16 19:40:44 -04001010 the :class:`ReadTransport` interface and *protocol* is an object
1011 instantiated by the *protocol_factory*.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001012
Victor Stinnerd84fd732014-08-26 01:01:59 +02001013 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
1014 non-blocking mode.
1015
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001016.. coroutinemethod:: loop.connect_write_pipe(protocol_factory, pipe)
Victor Stinnerea3183f2013-12-03 01:08:00 +01001017
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001018 Register the write end of *pipe* in the event loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001019
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001020 *protocol_factory* must be a callable returning an
1021 :ref:`asyncio protocol <asyncio-protocol>` implementation.
1022
1023 *pipe* is :term:`file-like object <file object>`.
1024
Victor Stinner2cef3002014-10-23 22:38:46 +02001025 Return pair ``(transport, protocol)``, where *transport* supports
Bumsik Kim5cc583d2018-09-16 19:40:44 -04001026 :class:`WriteTransport` interface and *protocol* is an object
1027 instantiated by the *protocol_factory*.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001028
Victor Stinnerd84fd732014-08-26 01:01:59 +02001029 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
1030 non-blocking mode.
1031
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001032.. note::
1033
1034 :class:`SelectorEventLoop` does not support the above methods on
Carol Willing5b7cbd62018-09-12 17:05:17 -07001035 Windows. Use :class:`ProactorEventLoop` instead for Windows.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001036
Victor Stinner08444382014-02-02 22:43:39 +01001037.. seealso::
1038
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001039 The :meth:`loop.subprocess_exec` and
1040 :meth:`loop.subprocess_shell` methods.
Victor Stinner08444382014-02-02 22:43:39 +01001041
Victor Stinnerea3183f2013-12-03 01:08:00 +01001042
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001043Unix signals
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001044^^^^^^^^^^^^
Victor Stinner8b863482014-01-27 10:07:50 +01001045
Andre Delfinodcc997c2020-12-16 22:37:28 -03001046.. method:: loop.add_signal_handler(signum, callback, *args)
Victor Stinner8b863482014-01-27 10:07:50 +01001047
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001048 Set *callback* as the handler for the *signum* signal.
Victor Stinner8b863482014-01-27 10:07:50 +01001049
Hrvoje Nikšiće3666fc2018-12-18 22:31:29 +01001050 The callback will be invoked by *loop*, along with other queued callbacks
1051 and runnable coroutines of that event loop. Unlike signal handlers
1052 registered using :func:`signal.signal`, a callback registered with this
1053 function is allowed to interact with the event loop.
1054
Victor Stinner8b863482014-01-27 10:07:50 +01001055 Raise :exc:`ValueError` if the signal number is invalid or uncatchable.
1056 Raise :exc:`RuntimeError` if there is a problem setting up the handler.
1057
Yury Selivanove247b462018-09-20 12:43:59 -04001058 Use :func:`functools.partial` :ref:`to pass keyword arguments
Naglis17473342018-12-04 09:31:15 +02001059 <asyncio-pass-keywords>` to *callback*.
Victor Stinner8464c242014-11-28 13:15:41 +01001060
Hrvoje Nikšiće3666fc2018-12-18 22:31:29 +01001061 Like :func:`signal.signal`, this function must be invoked in the main
1062 thread.
1063
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001064.. method:: loop.remove_signal_handler(sig)
Victor Stinner8b863482014-01-27 10:07:50 +01001065
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001066 Remove the handler for the *sig* signal.
Victor Stinner8b863482014-01-27 10:07:50 +01001067
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001068 Return ``True`` if the signal handler was removed, or ``False`` if
1069 no handler was set for the given signal.
Victor Stinner8b863482014-01-27 10:07:50 +01001070
Cheryl Sabella2d6097d2018-10-12 10:55:20 -04001071 .. availability:: Unix.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001072
Victor Stinner8b863482014-01-27 10:07:50 +01001073.. seealso::
1074
1075 The :mod:`signal` module.
1076
1077
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001078Executing code in thread or process pools
1079^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +01001080
Andre Delfinodcc997c2020-12-16 22:37:28 -03001081.. awaitablemethod:: loop.run_in_executor(executor, func, *args)
Victor Stinnerea3183f2013-12-03 01:08:00 +01001082
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001083 Arrange for *func* to be called in the specified executor.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001084
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001085 The *executor* argument should be an :class:`concurrent.futures.Executor`
Larry Hastings3732ed22014-03-15 21:13:56 -07001086 instance. The default executor is used if *executor* is ``None``.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001087
Yury Selivanove247b462018-09-20 12:43:59 -04001088 Example::
1089
1090 import asyncio
1091 import concurrent.futures
1092
1093 def blocking_io():
1094 # File operations (such as logging) can block the
1095 # event loop: run them in a thread pool.
1096 with open('/dev/urandom', 'rb') as f:
1097 return f.read(100)
1098
1099 def cpu_bound():
1100 # CPU-bound operations will block the event loop:
1101 # in general it is preferable to run them in a
1102 # process pool.
1103 return sum(i * i for i in range(10 ** 7))
1104
1105 async def main():
1106 loop = asyncio.get_running_loop()
1107
1108 ## Options:
1109
1110 # 1. Run in the default loop's executor:
1111 result = await loop.run_in_executor(
1112 None, blocking_io)
1113 print('default thread pool', result)
1114
1115 # 2. Run in a custom thread pool:
1116 with concurrent.futures.ThreadPoolExecutor() as pool:
1117 result = await loop.run_in_executor(
1118 pool, blocking_io)
1119 print('custom thread pool', result)
1120
1121 # 3. Run in a custom process pool:
1122 with concurrent.futures.ProcessPoolExecutor() as pool:
1123 result = await loop.run_in_executor(
1124 pool, cpu_bound)
1125 print('custom process pool', result)
1126
1127 asyncio.run(main())
Victor Stinner8464c242014-11-28 13:15:41 +01001128
Yury Selivanovbec23722018-01-28 14:09:40 -05001129 This method returns a :class:`asyncio.Future` object.
1130
Yury Selivanove247b462018-09-20 12:43:59 -04001131 Use :func:`functools.partial` :ref:`to pass keyword arguments
1132 <asyncio-pass-keywords>` to *func*.
1133
Yury Selivanove8a60452016-10-21 17:40:42 -04001134 .. versionchanged:: 3.5.3
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001135 :meth:`loop.run_in_executor` no longer configures the
Yury Selivanove8a60452016-10-21 17:40:42 -04001136 ``max_workers`` of the thread pool executor it creates, instead
1137 leaving it up to the thread pool executor
1138 (:class:`~concurrent.futures.ThreadPoolExecutor`) to set the
1139 default.
1140
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001141.. method:: loop.set_default_executor(executor)
Victor Stinnerea3183f2013-12-03 01:08:00 +01001142
Elvis Pranskevichus22d25082018-07-30 11:42:43 +01001143 Set *executor* as the default executor used by :meth:`run_in_executor`.
1144 *executor* should be an instance of
1145 :class:`~concurrent.futures.ThreadPoolExecutor`.
1146
1147 .. deprecated:: 3.8
1148 Using an executor that is not an instance of
1149 :class:`~concurrent.futures.ThreadPoolExecutor` is deprecated and
1150 will trigger an error in Python 3.9.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001151
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001152 *executor* must be an instance of
1153 :class:`concurrent.futures.ThreadPoolExecutor`.
1154
Victor Stinnerea3183f2013-12-03 01:08:00 +01001155
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001156Error Handling API
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001157^^^^^^^^^^^^^^^^^^
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001158
Martin Panterc04fb562016-02-10 05:44:01 +00001159Allows customizing how exceptions are handled in the event loop.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001160
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001161.. method:: loop.set_exception_handler(handler)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001162
1163 Set *handler* as the new event loop exception handler.
1164
1165 If *handler* is ``None``, the default exception handler will
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001166 be set. Otherwise, *handler* must be a callable with the signature
1167 matching ``(loop, context)``, where ``loop``
1168 is a reference to the active event loop, and ``context``
1169 is a ``dict`` object containing the details of the exception
1170 (see :meth:`call_exception_handler` documentation for details
1171 about context).
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001172
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001173.. method:: loop.get_exception_handler()
Yury Selivanov950204d2016-05-16 16:23:00 -04001174
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001175 Return the current exception handler, or ``None`` if no custom
1176 exception handler was set.
Yury Selivanov950204d2016-05-16 16:23:00 -04001177
1178 .. versionadded:: 3.5.2
1179
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001180.. method:: loop.default_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001181
1182 Default exception handler.
1183
1184 This is called when an exception occurs and no exception
Carol Willing5b7cbd62018-09-12 17:05:17 -07001185 handler is set. This can be called by a custom exception
1186 handler that wants to defer to the default handler behavior.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001187
1188 *context* parameter has the same meaning as in
1189 :meth:`call_exception_handler`.
1190
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001191.. method:: loop.call_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001192
1193 Call the current event loop exception handler.
1194
1195 *context* is a ``dict`` object containing the following keys
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001196 (new keys may be introduced in future Python versions):
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001197
1198 * 'message': Error message;
1199 * 'exception' (optional): Exception object;
1200 * 'future' (optional): :class:`asyncio.Future` instance;
Shane Harveya1652da2020-11-26 05:24:48 -08001201 * 'task' (optional): :class:`asyncio.Task` instance;
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001202 * 'handle' (optional): :class:`asyncio.Handle` instance;
1203 * 'protocol' (optional): :ref:`Protocol <asyncio-protocol>` instance;
1204 * 'transport' (optional): :ref:`Transport <asyncio-transport>` instance;
Shane Harveya1652da2020-11-26 05:24:48 -08001205 * 'socket' (optional): :class:`socket.socket` instance;
1206 * 'asyncgen' (optional): Asynchronous generator that caused
1207 the exception.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001208
1209 .. note::
1210
Carol Willing5b7cbd62018-09-12 17:05:17 -07001211 This method should not be overloaded in subclassed
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001212 event loops. For custom exception handling, use
1213 the :meth:`set_exception_handler()` method.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001214
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001215Enabling debug mode
1216^^^^^^^^^^^^^^^^^^^
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001217
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001218.. method:: loop.get_debug()
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001219
Victor Stinner7b7120e2014-06-23 00:12:14 +02001220 Get the debug mode (:class:`bool`) of the event loop.
1221
1222 The default value is ``True`` if the environment variable
1223 :envvar:`PYTHONASYNCIODEBUG` is set to a non-empty string, ``False``
1224 otherwise.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001225
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001226.. method:: loop.set_debug(enabled: bool)
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001227
1228 Set the debug mode of the event loop.
1229
Yury Selivanov805e27e2018-09-14 16:57:11 -07001230 .. versionchanged:: 3.7
1231
Victor Stinnerb9783d22020-01-24 10:22:18 +01001232 The new :ref:`Python Development Mode <devmode>` can now also be used
Yury Selivanov805e27e2018-09-14 16:57:11 -07001233 to enable the debug mode.
1234
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001235.. seealso::
1236
Victor Stinner62511fd2014-06-23 00:36:11 +02001237 The :ref:`debug mode of asyncio <asyncio-debug-mode>`.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01001238
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001239
1240Running Subprocesses
1241^^^^^^^^^^^^^^^^^^^^
1242
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001243Methods described in this subsections are low-level. In regular
1244async/await code consider using the high-level
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001245:func:`asyncio.create_subprocess_shell` and
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001246:func:`asyncio.create_subprocess_exec` convenience functions instead.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001247
1248.. note::
1249
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001250 The default asyncio event loop on **Windows** does not support
1251 subprocesses. See :ref:`Subprocess Support on Windows
1252 <asyncio-windows-subprocess>` for details.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001253
Andre Delfinodcc997c2020-12-16 22:37:28 -03001254.. coroutinemethod:: loop.subprocess_exec(protocol_factory, *args, \
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001255 stdin=subprocess.PIPE, stdout=subprocess.PIPE, \
Andre Delfinodcc997c2020-12-16 22:37:28 -03001256 stderr=subprocess.PIPE, **kwargs)
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001257
1258 Create a subprocess from one or more string arguments specified by
1259 *args*.
1260
1261 *args* must be a list of strings represented by:
1262
1263 * :class:`str`;
1264 * or :class:`bytes`, encoded to the
1265 :ref:`filesystem encoding <filesystem-encoding>`.
1266
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001267 The first string specifies the program executable,
Carol Willing5b7cbd62018-09-12 17:05:17 -07001268 and the remaining strings specify the arguments. Together, string
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001269 arguments form the ``argv`` of the program.
1270
1271 This is similar to the standard library :class:`subprocess.Popen`
1272 class called with ``shell=False`` and the list of strings passed as
1273 the first argument; however, where :class:`~subprocess.Popen` takes
1274 a single argument which is list of strings, *subprocess_exec*
1275 takes multiple string arguments.
1276
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001277 The *protocol_factory* must be a callable returning a subclass of the
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001278 :class:`asyncio.SubprocessProtocol` class.
1279
1280 Other parameters:
1281
sbstpf0d4c642019-05-27 19:51:19 -04001282 * *stdin* can be any of these:
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001283
sbstpf0d4c642019-05-27 19:51:19 -04001284 * a file-like object representing a pipe to be connected to the
1285 subprocess's standard input stream using
1286 :meth:`~loop.connect_write_pipe`
1287 * the :const:`subprocess.PIPE` constant (default) which will create a new
1288 pipe and connect it,
1289 * the value ``None`` which will make the subprocess inherit the file
1290 descriptor from this process
1291 * the :const:`subprocess.DEVNULL` constant which indicates that the
1292 special :data:`os.devnull` file will be used
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001293
sbstpf0d4c642019-05-27 19:51:19 -04001294 * *stdout* can be any of these:
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001295
sbstpf0d4c642019-05-27 19:51:19 -04001296 * a file-like object representing a pipe to be connected to the
1297 subprocess's standard output stream using
1298 :meth:`~loop.connect_write_pipe`
1299 * the :const:`subprocess.PIPE` constant (default) which will create a new
1300 pipe and connect it,
1301 * the value ``None`` which will make the subprocess inherit the file
1302 descriptor from this process
1303 * the :const:`subprocess.DEVNULL` constant which indicates that the
1304 special :data:`os.devnull` file will be used
1305
1306 * *stderr* can be any of these:
1307
1308 * a file-like object representing a pipe to be connected to the
1309 subprocess's standard error stream using
1310 :meth:`~loop.connect_write_pipe`
1311 * the :const:`subprocess.PIPE` constant (default) which will create a new
1312 pipe and connect it,
1313 * the value ``None`` which will make the subprocess inherit the file
1314 descriptor from this process
1315 * the :const:`subprocess.DEVNULL` constant which indicates that the
1316 special :data:`os.devnull` file will be used
1317 * the :const:`subprocess.STDOUT` constant which will connect the standard
1318 error stream to the process' standard output stream
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001319
1320 * All other keyword arguments are passed to :class:`subprocess.Popen`
sbstpf0d4c642019-05-27 19:51:19 -04001321 without interpretation, except for *bufsize*, *universal_newlines*,
1322 *shell*, *text*, *encoding* and *errors*, which should not be specified
1323 at all.
1324
1325 The ``asyncio`` subprocess API does not support decoding the streams
1326 as text. :func:`bytes.decode` can be used to convert the bytes returned
1327 from the stream to text.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001328
1329 See the constructor of the :class:`subprocess.Popen` class
1330 for documentation on other arguments.
1331
1332 Returns a pair of ``(transport, protocol)``, where *transport*
Bumsik Kim5cc583d2018-09-16 19:40:44 -04001333 conforms to the :class:`asyncio.SubprocessTransport` base class and
1334 *protocol* is an object instantiated by the *protocol_factory*.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001335
Andre Delfinodcc997c2020-12-16 22:37:28 -03001336.. coroutinemethod:: loop.subprocess_shell(protocol_factory, cmd, *, \
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001337 stdin=subprocess.PIPE, stdout=subprocess.PIPE, \
Andre Delfinodcc997c2020-12-16 22:37:28 -03001338 stderr=subprocess.PIPE, **kwargs)
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001339
1340 Create a subprocess from *cmd*, which can be a :class:`str` or a
1341 :class:`bytes` string encoded to the
1342 :ref:`filesystem encoding <filesystem-encoding>`,
1343 using the platform's "shell" syntax.
1344
1345 This is similar to the standard library :class:`subprocess.Popen`
1346 class called with ``shell=True``.
1347
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001348 The *protocol_factory* must be a callable returning a subclass of the
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001349 :class:`SubprocessProtocol` class.
1350
1351 See :meth:`~loop.subprocess_exec` for more details about
1352 the remaining arguments.
1353
1354 Returns a pair of ``(transport, protocol)``, where *transport*
Bumsik Kim5cc583d2018-09-16 19:40:44 -04001355 conforms to the :class:`SubprocessTransport` base class and
1356 *protocol* is an object instantiated by the *protocol_factory*.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001357
1358.. note::
1359 It is the application's responsibility to ensure that all whitespace
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001360 and special characters are quoted appropriately to avoid `shell injection
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001361 <https://en.wikipedia.org/wiki/Shell_injection#Shell_injection>`_
1362 vulnerabilities. The :func:`shlex.quote` function can be used to
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001363 properly escape whitespace and special characters in strings that
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001364 are going to be used to construct shell commands.
1365
1366
1367Callback Handles
1368================
1369
1370.. class:: Handle
1371
1372 A callback wrapper object returned by :meth:`loop.call_soon`,
1373 :meth:`loop.call_soon_threadsafe`.
1374
1375 .. method:: cancel()
1376
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001377 Cancel the callback. If the callback has already been canceled
1378 or executed, this method has no effect.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001379
1380 .. method:: cancelled()
1381
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001382 Return ``True`` if the callback was cancelled.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001383
1384 .. versionadded:: 3.7
1385
1386.. class:: TimerHandle
1387
1388 A callback wrapper object returned by :meth:`loop.call_later`,
1389 and :meth:`loop.call_at`.
1390
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001391 This class is a subclass of :class:`Handle`.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001392
1393 .. method:: when()
1394
1395 Return a scheduled callback time as :class:`float` seconds.
1396
1397 The time is an absolute timestamp, using the same time
1398 reference as :meth:`loop.time`.
1399
1400 .. versionadded:: 3.7
1401
1402
1403Server Objects
1404==============
1405
1406Server objects are created by :meth:`loop.create_server`,
1407:meth:`loop.create_unix_server`, :func:`start_server`,
1408and :func:`start_unix_server` functions.
1409
1410Do not instantiate the class directly.
Victor Stinner8c462c52014-01-24 18:11:43 +01001411
Victor Stinner8ebeb032014-07-11 23:47:40 +02001412.. class:: Server
Victor Stinner8c462c52014-01-24 18:11:43 +01001413
Yury Selivanovc9070d02018-01-25 18:08:09 -05001414 *Server* objects are asynchronous context managers. When used in an
1415 ``async with`` statement, it's guaranteed that the Server object is
1416 closed and not accepting new connections when the ``async with``
1417 statement is completed::
1418
1419 srv = await loop.create_server(...)
1420
1421 async with srv:
1422 # some code
1423
Carol Willing5b7cbd62018-09-12 17:05:17 -07001424 # At this point, srv is closed and no longer accepts new connections.
Yury Selivanovc9070d02018-01-25 18:08:09 -05001425
1426
1427 .. versionchanged:: 3.7
1428 Server object is an asynchronous context manager since Python 3.7.
Victor Stinner8c462c52014-01-24 18:11:43 +01001429
1430 .. method:: close()
1431
Victor Stinner4bfb14a2014-07-12 03:20:24 +02001432 Stop serving: close listening sockets and set the :attr:`sockets`
1433 attribute to ``None``.
1434
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001435 The sockets that represent existing incoming client connections
1436 are left open.
Victor Stinner8ebeb032014-07-11 23:47:40 +02001437
Berker Peksag49c9edf2016-01-20 07:14:22 +02001438 The server is closed asynchronously, use the :meth:`wait_closed`
1439 coroutine to wait until the server is closed.
Victor Stinner8c462c52014-01-24 18:11:43 +01001440
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)1634fc22017-12-30 20:39:32 +05301441 .. method:: get_loop()
1442
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001443 Return the event loop associated with the server object.
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)1634fc22017-12-30 20:39:32 +05301444
1445 .. versionadded:: 3.7
1446
Yury Selivanovc9070d02018-01-25 18:08:09 -05001447 .. coroutinemethod:: start_serving()
1448
1449 Start accepting connections.
1450
1451 This method is idempotent, so it can be called when
1452 the server is already being serving.
1453
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001454 The *start_serving* keyword-only parameter to
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001455 :meth:`loop.create_server` and
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001456 :meth:`asyncio.start_server` allows creating a Server object
1457 that is not accepting connections initially. In this case
1458 ``Server.start_serving()``, or :meth:`Server.serve_forever` can be used
1459 to make the Server start accepting connections.
Yury Selivanovc9070d02018-01-25 18:08:09 -05001460
1461 .. versionadded:: 3.7
1462
1463 .. coroutinemethod:: serve_forever()
1464
1465 Start accepting connections until the coroutine is cancelled.
1466 Cancellation of ``serve_forever`` task causes the server
1467 to be closed.
1468
1469 This method can be called if the server is already accepting
1470 connections. Only one ``serve_forever`` task can exist per
1471 one *Server* object.
1472
1473 Example::
1474
1475 async def client_connected(reader, writer):
1476 # Communicate with the client with
1477 # reader/writer streams. For example:
1478 await reader.readline()
1479
1480 async def main(host, port):
1481 srv = await asyncio.start_server(
1482 client_connected, host, port)
Andrew Svetlov17ab8f02018-02-17 19:44:35 +02001483 await srv.serve_forever()
Yury Selivanovc9070d02018-01-25 18:08:09 -05001484
1485 asyncio.run(main('127.0.0.1', 0))
1486
1487 .. versionadded:: 3.7
1488
1489 .. method:: is_serving()
1490
1491 Return ``True`` if the server is accepting new connections.
1492
1493 .. versionadded:: 3.7
1494
Victor Stinnerbdd574d2015-02-12 22:49:18 +01001495 .. coroutinemethod:: wait_closed()
Victor Stinner8c462c52014-01-24 18:11:43 +01001496
Victor Stinner8ebeb032014-07-11 23:47:40 +02001497 Wait until the :meth:`close` method completes.
1498
Victor Stinner8ebeb032014-07-11 23:47:40 +02001499 .. attribute:: sockets
1500
Emmanuel Ariasdf5cdc12019-02-22 14:34:41 -03001501 List of :class:`socket.socket` objects the server is listening on.
Victor Stinner8c462c52014-01-24 18:11:43 +01001502
Yury Selivanovc9070d02018-01-25 18:08:09 -05001503 .. versionchanged:: 3.7
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001504 Prior to Python 3.7 ``Server.sockets`` used to return an
1505 internal list of server sockets directly. In 3.7 a copy
Yury Selivanovc9070d02018-01-25 18:08:09 -05001506 of that list is returned.
1507
Victor Stinner8c462c52014-01-24 18:11:43 +01001508
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001509.. _asyncio-event-loops:
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001510
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001511Event Loop Implementations
1512==========================
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001513
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001514asyncio ships with two different event loop implementations:
1515:class:`SelectorEventLoop` and :class:`ProactorEventLoop`.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001516
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001517By default asyncio is configured to use :class:`SelectorEventLoop`
Ben Darnell9ffca672019-06-22 13:38:21 -04001518on Unix and :class:`ProactorEventLoop` on Windows.
Andrew Svetlov3d4dbd82018-02-01 19:59:32 +02001519
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001520
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001521.. class:: SelectorEventLoop
1522
1523 An event loop based on the :mod:`selectors` module.
1524
1525 Uses the most efficient *selector* available for the given
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001526 platform. It is also possible to manually configure the
1527 exact selector implementation to be used::
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001528
1529 import asyncio
1530 import selectors
1531
1532 selector = selectors.SelectSelector()
1533 loop = asyncio.SelectorEventLoop(selector)
1534 asyncio.set_event_loop(loop)
Andrew Svetlov7464e872018-01-19 20:04:29 +02001535
1536
Cheryl Sabella2d6097d2018-10-12 10:55:20 -04001537 .. availability:: Unix, Windows.
Andrew Svetlov7464e872018-01-19 20:04:29 +02001538
1539
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001540.. class:: ProactorEventLoop
1541
1542 An event loop for Windows that uses "I/O Completion Ports" (IOCP).
1543
Cheryl Sabella2d6097d2018-10-12 10:55:20 -04001544 .. availability:: Windows.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001545
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001546 .. seealso::
1547
1548 `MSDN documentation on I/O Completion Ports
1549 <https://docs.microsoft.com/en-ca/windows/desktop/FileIO/i-o-completion-ports>`_.
1550
1551
1552.. class:: AbstractEventLoop
1553
1554 Abstract base class for asyncio-compliant event loops.
1555
1556 The :ref:`Event Loop Methods <asyncio-event-loop>` section lists all
1557 methods that an alternative implementation of ``AbstractEventLoop``
1558 should have defined.
1559
1560
1561Examples
1562========
1563
1564Note that all examples in this section **purposefully** show how
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001565to use the low-level event loop APIs, such as :meth:`loop.run_forever`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001566and :meth:`loop.call_soon`. Modern asyncio applications rarely
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001567need to be written this way; consider using the high-level functions
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001568like :func:`asyncio.run`.
1569
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001570
Yury Selivanov394374e2018-09-17 15:35:24 -04001571.. _asyncio_example_lowlevel_helloworld:
Victor Stinnerea3183f2013-12-03 01:08:00 +01001572
Victor Stinner7f314ed2014-10-15 18:49:16 +02001573Hello World with call_soon()
Victor Stinnera092a612015-01-09 15:58:41 +01001574^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +01001575
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001576An example using the :meth:`loop.call_soon` method to schedule a
1577callback. The callback displays ``"Hello World"`` and then stops the
1578event loop::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001579
1580 import asyncio
1581
Victor Stinner7f314ed2014-10-15 18:49:16 +02001582 def hello_world(loop):
Carol Willing5b7cbd62018-09-12 17:05:17 -07001583 """A callback to print 'Hello World' and stop the event loop"""
Victor Stinnerea3183f2013-12-03 01:08:00 +01001584 print('Hello World')
Victor Stinner7f314ed2014-10-15 18:49:16 +02001585 loop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001586
1587 loop = asyncio.get_event_loop()
Victor Stinner7f314ed2014-10-15 18:49:16 +02001588
1589 # Schedule a call to hello_world()
1590 loop.call_soon(hello_world, loop)
1591
1592 # Blocking call interrupted by loop.stop()
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001593 try:
1594 loop.run_forever()
1595 finally:
1596 loop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001597
Victor Stinner3e09e322013-12-03 01:22:06 +01001598.. seealso::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001599
Yury Selivanov3faaa882018-09-14 13:32:07 -07001600 A similar :ref:`Hello World <coroutine>`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001601 example created with a coroutine and the :func:`run` function.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001602
Victor Stinner8b863482014-01-27 10:07:50 +01001603
Yury Selivanov394374e2018-09-17 15:35:24 -04001604.. _asyncio_example_call_later:
Victor Stinner7f314ed2014-10-15 18:49:16 +02001605
1606Display the current date with call_later()
Victor Stinnera092a612015-01-09 15:58:41 +01001607^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner7f314ed2014-10-15 18:49:16 +02001608
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001609An example of a callback displaying the current date every second. The
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001610callback uses the :meth:`loop.call_later` method to reschedule itself
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001611after 5 seconds, and then stops the event loop::
Victor Stinner7f314ed2014-10-15 18:49:16 +02001612
1613 import asyncio
1614 import datetime
1615
1616 def display_date(end_time, loop):
1617 print(datetime.datetime.now())
1618 if (loop.time() + 1.0) < end_time:
1619 loop.call_later(1, display_date, end_time, loop)
1620 else:
1621 loop.stop()
1622
1623 loop = asyncio.get_event_loop()
1624
1625 # Schedule the first call to display_date()
1626 end_time = loop.time() + 5.0
1627 loop.call_soon(display_date, end_time, loop)
1628
1629 # Blocking call interrupted by loop.stop()
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001630 try:
1631 loop.run_forever()
1632 finally:
1633 loop.close()
Victor Stinner7f314ed2014-10-15 18:49:16 +02001634
1635.. seealso::
1636
Yury Selivanov7372c3b2018-09-14 15:11:24 -07001637 A similar :ref:`current date <asyncio_example_sleep>` example
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001638 created with a coroutine and the :func:`run` function.
Victor Stinner7f314ed2014-10-15 18:49:16 +02001639
1640
Yury Selivanov394374e2018-09-17 15:35:24 -04001641.. _asyncio_example_watch_fd:
Victor Stinner8b863482014-01-27 10:07:50 +01001642
Victor Stinner04e6df32014-10-11 16:16:27 +02001643Watch a file descriptor for read events
Victor Stinnera092a612015-01-09 15:58:41 +01001644^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001645
1646Wait until a file descriptor received some data using the
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001647:meth:`loop.add_reader` method and then close the event loop::
Victor Stinner04e6df32014-10-11 16:16:27 +02001648
1649 import asyncio
Victor Stinnerac577d72017-11-28 21:33:20 +01001650 from socket import socketpair
Victor Stinner04e6df32014-10-11 16:16:27 +02001651
1652 # Create a pair of connected file descriptors
Victor Stinnerccd8e342014-10-11 16:30:02 +02001653 rsock, wsock = socketpair()
Carol Willing5b7cbd62018-09-12 17:05:17 -07001654
Victor Stinner04e6df32014-10-11 16:16:27 +02001655 loop = asyncio.get_event_loop()
1656
1657 def reader():
1658 data = rsock.recv(100)
1659 print("Received:", data.decode())
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001660
Victor Stinner2cef3002014-10-23 22:38:46 +02001661 # We are done: unregister the file descriptor
Victor Stinner04e6df32014-10-11 16:16:27 +02001662 loop.remove_reader(rsock)
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001663
Victor Stinner04e6df32014-10-11 16:16:27 +02001664 # Stop the event loop
1665 loop.stop()
1666
Victor Stinner2cef3002014-10-23 22:38:46 +02001667 # Register the file descriptor for read event
Victor Stinner04e6df32014-10-11 16:16:27 +02001668 loop.add_reader(rsock, reader)
1669
1670 # Simulate the reception of data from the network
1671 loop.call_soon(wsock.send, 'abc'.encode())
1672
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001673 try:
1674 # Run the event loop
1675 loop.run_forever()
1676 finally:
Carol Willing5b7cbd62018-09-12 17:05:17 -07001677 # We are done. Close sockets and the event loop.
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001678 rsock.close()
1679 wsock.close()
1680 loop.close()
Victor Stinner04e6df32014-10-11 16:16:27 +02001681
1682.. seealso::
1683
Yury Selivanov394374e2018-09-17 15:35:24 -04001684 * A similar :ref:`example <asyncio_example_create_connection>`
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001685 using transports, protocols, and the
1686 :meth:`loop.create_connection` method.
Victor Stinner04e6df32014-10-11 16:16:27 +02001687
Yury Selivanov394374e2018-09-17 15:35:24 -04001688 * Another similar :ref:`example <asyncio_example_create_connection-streams>`
Yury Selivanov6758e6e2019-09-29 21:59:55 -07001689 using the high-level :func:`asyncio.open_connection` function
1690 and streams.
Victor Stinner04e6df32014-10-11 16:16:27 +02001691
1692
Yury Selivanov394374e2018-09-17 15:35:24 -04001693.. _asyncio_example_unix_signals:
1694
Victor Stinner04e6df32014-10-11 16:16:27 +02001695Set signal handlers for SIGINT and SIGTERM
Victor Stinnera092a612015-01-09 15:58:41 +01001696^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001697
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -04001698(This ``signals`` example only works on Unix.)
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001699
1700Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM`
1701using the :meth:`loop.add_signal_handler` method::
Victor Stinner8b863482014-01-27 10:07:50 +01001702
1703 import asyncio
1704 import functools
1705 import os
1706 import signal
1707
Alexander Vasinceb842e2019-05-03 18:25:36 +03001708 def ask_exit(signame, loop):
Victor Stinner8b863482014-01-27 10:07:50 +01001709 print("got signal %s: exit" % signame)
1710 loop.stop()
1711
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001712 async def main():
1713 loop = asyncio.get_running_loop()
Victor Stinner8b863482014-01-27 10:07:50 +01001714
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001715 for signame in {'SIGINT', 'SIGTERM'}:
1716 loop.add_signal_handler(
1717 getattr(signal, signame),
Alexander Vasinceb842e2019-05-03 18:25:36 +03001718 functools.partial(ask_exit, signame, loop))
Victor Stinner2cef3002014-10-23 22:38:46 +02001719
Yury Selivanov7c7605f2018-09-11 09:54:40 -07001720 await asyncio.sleep(3600)
1721
1722 print("Event loop running for 1 hour, press Ctrl+C to interrupt.")
1723 print(f"pid {os.getpid()}: send SIGINT or SIGTERM to exit.")
1724
1725 asyncio.run(main())