R David Murray | 6a14381 | 2013-12-20 14:37:39 -0500 | [diff] [blame] | 1 | .. currentmodule:: asyncio |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 2 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 3 | |
| 4 | ========== |
| 5 | Event Loop |
| 6 | ========== |
| 7 | |
Kyle Stanley | f900064 | 2019-10-10 19:18:46 -0400 | [diff] [blame] | 8 | **Source code:** :source:`Lib/asyncio/events.py`, |
| 9 | :source:`Lib/asyncio/base_events.py` |
| 10 | |
| 11 | ------------------------------------ |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 12 | |
| 13 | .. rubric:: Preface |
| 14 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 15 | The event loop is the core of every asyncio application. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 16 | Event loops run asynchronous tasks and callbacks, perform network |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 17 | IO operations, and run subprocesses. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 18 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 19 | Application developers should typically use the high-level asyncio functions, |
| 20 | such as :func:`asyncio.run`, and should rarely need to reference the loop |
| 21 | object or call its methods. This section is intended mostly for authors |
| 22 | of lower-level code, libraries, and frameworks, who need finer control over |
| 23 | the event loop behavior. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 24 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 25 | .. rubric:: Obtaining the Event Loop |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 26 | |
| 27 | The following low-level functions can be used to get, set, or create |
| 28 | an 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 Svetlov | 2c49bec | 2020-01-21 00:46:38 +0200 | [diff] [blame] | 41 | 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 Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 45 | been called, asyncio will create a new event loop and set it as the |
| 46 | current one. |
| 47 | |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 48 | 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 Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 52 | |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 53 | Consider also using the :func:`asyncio.run` function instead of using |
| 54 | lower level functions to manually create and close an event loop. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 55 | |
Serhiy Storchaka | 172c0f2 | 2021-04-25 13:40:44 +0300 | [diff] [blame] | 56 | .. deprecated:: 3.10 |
| 57 | Deprecation warning is emitted if there is no running event loop. |
Miss Islington (bot) | 1557cff | 2021-07-26 21:07:30 -0700 | [diff] [blame] | 58 | In future Python releases, this function will be an alias of |
Serhiy Storchaka | 172c0f2 | 2021-04-25 13:40:44 +0300 | [diff] [blame] | 59 | :func:`get_running_loop`. |
| 60 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 61 | .. 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 | |
| 69 | Note that the behaviour of :func:`get_event_loop`, :func:`set_event_loop`, |
| 70 | and :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 | |
| 76 | This documentation page contains the following sections: |
| 77 | |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 78 | * The `Event Loop Methods`_ section is the reference documentation of |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 79 | the event loop APIs; |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 80 | |
| 81 | * The `Callback Handles`_ section documents the :class:`Handle` and |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 82 | :class:`TimerHandle` instances which are returned from scheduling |
| 83 | methods such as :meth:`loop.call_soon` and :meth:`loop.call_later`; |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 84 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 85 | * The `Server Objects`_ section documents types returned from |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 86 | event loop methods like :meth:`loop.create_server`; |
| 87 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 88 | * The `Event Loop Implementations`_ section documents the |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 89 | :class:`SelectorEventLoop` and :class:`ProactorEventLoop` classes; |
| 90 | |
| 91 | * The `Examples`_ section showcases how to work with some event |
| 92 | loop APIs. |
| 93 | |
| 94 | |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 95 | .. _asyncio-event-loop: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 96 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 97 | Event Loop Methods |
| 98 | ================== |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 99 | |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 100 | Event loops have **low-level** APIs for the following: |
lf | 627d2c8 | 2017-07-25 17:03:51 -0600 | [diff] [blame] | 101 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 102 | .. contents:: |
| 103 | :depth: 1 |
| 104 | :local: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 105 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 106 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 107 | Running and stopping the loop |
| 108 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 109 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 110 | .. method:: loop.run_until_complete(future) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 111 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 112 | Run until the *future* (an instance of :class:`Future`) has |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 113 | completed. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 114 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 115 | If the argument is a :ref:`coroutine object <coroutine>` it |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 116 | is implicitly scheduled to run as a :class:`asyncio.Task`. |
Eli Bendersky | 136fea2 | 2014-02-09 06:55:58 -0800 | [diff] [blame] | 117 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 118 | Return the Future's result or raise its exception. |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 119 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 120 | .. method:: loop.run_forever() |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 121 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 122 | Run the event loop until :meth:`stop` is called. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 123 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 124 | 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 Stinner | 8370496 | 2015-02-25 14:24:15 +0100 | [diff] [blame] | 128 | |
Guido van Rossum | 41f69f4 | 2015-11-19 13:28:47 -0800 | [diff] [blame] | 129 | If :meth:`stop` is called while :meth:`run_forever` is running, |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 130 | the loop will run the current batch of callbacks and then exit. |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 131 | Note that new callbacks scheduled by callbacks will not run in this |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 132 | case; instead, they will run the next time :meth:`run_forever` or |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 133 | :meth:`run_until_complete` is called. |
Guido van Rossum | 41f69f4 | 2015-11-19 13:28:47 -0800 | [diff] [blame] | 134 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 135 | .. method:: loop.stop() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 136 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 137 | Stop the event loop. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 138 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 139 | .. method:: loop.is_running() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 140 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 141 | Return ``True`` if the event loop is currently running. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 142 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 143 | .. method:: loop.is_closed() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 144 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 145 | Return ``True`` if the event loop was closed. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 146 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 147 | .. method:: loop.close() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 148 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 149 | Close the event loop. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 150 | |
Andriy Maletsky | b83d917 | 2018-10-29 23:39:21 +0200 | [diff] [blame] | 151 | The loop must not be running when this function is called. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 152 | Any pending callbacks will be discarded. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 153 | |
Łukasz Langa | 7f9a2ae | 2019-06-04 13:03:20 +0200 | [diff] [blame] | 154 | This method clears all queues and shuts down the executor, but does |
| 155 | not wait for the executor to finish. |
Guido van Rossum | 41f69f4 | 2015-11-19 13:28:47 -0800 | [diff] [blame] | 156 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 157 | This method is idempotent and irreversible. No other methods |
| 158 | should be called after the event loop is closed. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 159 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 160 | .. coroutinemethod:: loop.shutdown_asyncgens() |
Yury Selivanov | 0366004 | 2016-12-15 17:36:05 -0500 | [diff] [blame] | 161 | |
| 162 | Schedule all currently open :term:`asynchronous generator` objects to |
| 163 | close with an :meth:`~agen.aclose()` call. After calling this method, |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 164 | the event loop will issue a warning if a new asynchronous generator |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 165 | is iterated. This should be used to reliably finalize all scheduled |
Yury Selivanov | ac94e38 | 2018-09-17 23:58:00 -0400 | [diff] [blame] | 166 | asynchronous generators. |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 167 | |
Yury Selivanov | ac94e38 | 2018-09-17 23:58:00 -0400 | [diff] [blame] | 168 | Note that there is no need to call this function when |
| 169 | :func:`asyncio.run` is used. |
| 170 | |
| 171 | Example:: |
Yury Selivanov | 0366004 | 2016-12-15 17:36:05 -0500 | [diff] [blame] | 172 | |
| 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 Stanley | 9fdc64c | 2019-09-19 08:47:22 -0400 | [diff] [blame] | 181 | .. 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 Selivanov | 0366004 | 2016-12-15 17:36:05 -0500 | [diff] [blame] | 193 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 194 | Scheduling callbacks |
| 195 | ^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 196 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 197 | .. method:: loop.call_soon(callback, *args, context=None) |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 198 | |
Roger Iyengar | a16d697 | 2020-06-22 22:16:00 -0400 | [diff] [blame] | 199 | Schedule the *callback* :term:`callback` to be called with |
| 200 | *args* arguments at the next iteration of the event loop. |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 201 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 202 | Callbacks are called in the order in which they are registered. |
| 203 | Each callback will be called exactly once. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 204 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 205 | 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 Selivanov | 28b9178 | 2018-05-23 13:35:04 -0400 | [diff] [blame] | 208 | |
Yury Selivanov | 1096f76 | 2015-06-25 13:49:52 -0400 | [diff] [blame] | 209 | An instance of :class:`asyncio.Handle` is returned, which can be |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 210 | used later to cancel the callback. |
| 211 | |
| 212 | This method is not thread-safe. |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 213 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 214 | .. method:: loop.call_soon_threadsafe(callback, *args, context=None) |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 215 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 216 | A thread-safe variant of :meth:`call_soon`. Must be used to |
| 217 | schedule callbacks *from another thread*. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 218 | |
Victor Stinner | 8370496 | 2015-02-25 14:24:15 +0100 | [diff] [blame] | 219 | See the :ref:`concurrency and multithreading <asyncio-multithreading>` |
| 220 | section of the documentation. |
| 221 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 222 | .. versionchanged:: 3.7 |
| 223 | The *context* keyword-only parameter was added. See :pep:`567` |
| 224 | for more details. |
| 225 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 226 | .. _asyncio-pass-keywords: |
| 227 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 228 | .. note:: |
| 229 | |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 230 | Most :mod:`asyncio` scheduling functions don't allow passing |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 231 | keyword arguments. To do that, use :func:`functools.partial`:: |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 232 | |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 233 | # will schedule "print("Hello", flush=True)" |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 234 | loop.call_soon( |
| 235 | functools.partial(print, "Hello", flush=True)) |
| 236 | |
| 237 | Using partial objects is usually more convenient than using lambdas, |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 238 | as asyncio can render partial objects better in debug and error |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 239 | messages. |
Yury Selivanov | 28b9178 | 2018-05-23 13:35:04 -0400 | [diff] [blame] | 240 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 241 | |
Victor Stinner | 45b27ed | 2014-02-01 02:36:43 +0100 | [diff] [blame] | 242 | .. _asyncio-delayed-calls: |
| 243 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 244 | Scheduling delayed callbacks |
| 245 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 246 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 247 | Event loop provides mechanisms to schedule callback functions |
| 248 | to be called at some point in the future. Event loop uses monotonic |
| 249 | clocks to track time. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 250 | |
Victor Stinner | 45b27ed | 2014-02-01 02:36:43 +0100 | [diff] [blame] | 251 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 252 | .. method:: loop.call_later(delay, callback, *args, context=None) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 253 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 254 | Schedule *callback* to be called after the given *delay* |
| 255 | number of seconds (can be either an int or a float). |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 256 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 257 | An instance of :class:`asyncio.TimerHandle` is returned which can |
| 258 | be used to cancel the callback. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 259 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 260 | *callback* will be called exactly once. If two callbacks are |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 261 | scheduled for exactly the same time, the order in which they |
| 262 | are called is undefined. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 263 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 264 | The optional positional *args* will be passed to the callback when |
| 265 | it is called. If you want the callback to be called with keyword |
| 266 | arguments use :func:`functools.partial`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 267 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 268 | An optional keyword-only *context* argument allows specifying a |
| 269 | custom :class:`contextvars.Context` for the *callback* to run in. |
| 270 | The current context is used when no *context* is provided. |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 271 | |
Yury Selivanov | 28b9178 | 2018-05-23 13:35:04 -0400 | [diff] [blame] | 272 | .. versionchanged:: 3.7 |
| 273 | The *context* keyword-only parameter was added. See :pep:`567` |
| 274 | for more details. |
| 275 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 276 | .. versionchanged:: 3.8 |
| 277 | In Python 3.7 and earlier with the default event loop implementation, |
| 278 | the *delay* could not exceed one day. |
| 279 | This has been fixed in Python 3.8. |
| 280 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 281 | .. method:: loop.call_at(when, callback, *args, context=None) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 282 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 283 | Schedule *callback* to be called at the given absolute timestamp |
| 284 | *when* (an int or a float), using the same time reference as |
| 285 | :meth:`loop.time`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 286 | |
| 287 | This method's behavior is the same as :meth:`call_later`. |
| 288 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 289 | An instance of :class:`asyncio.TimerHandle` is returned which can |
| 290 | be used to cancel the callback. |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 291 | |
Yury Selivanov | 28b9178 | 2018-05-23 13:35:04 -0400 | [diff] [blame] | 292 | .. versionchanged:: 3.7 |
| 293 | The *context* keyword-only parameter was added. See :pep:`567` |
| 294 | for more details. |
| 295 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 296 | .. versionchanged:: 3.8 |
| 297 | In Python 3.7 and earlier with the default event loop implementation, |
| 298 | the difference between *when* and the current time could not exceed |
| 299 | one day. This has been fixed in Python 3.8. |
| 300 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 301 | .. method:: loop.time() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 302 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 303 | Return the current time, as a :class:`float` value, according to |
| 304 | the event loop's internal monotonic clock. |
| 305 | |
| 306 | .. note:: |
Enrico Alarico Carbognani | 7e954e7 | 2019-04-18 14:43:14 +0200 | [diff] [blame] | 307 | .. versionchanged:: 3.8 |
| 308 | In Python 3.7 and earlier timeouts (relative *delay* or absolute *when*) |
| 309 | should not exceed one day. This has been fixed in Python 3.8. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 310 | |
Victor Stinner | 3e09e32 | 2013-12-03 01:22:06 +0100 | [diff] [blame] | 311 | .. seealso:: |
| 312 | |
| 313 | The :func:`asyncio.sleep` function. |
| 314 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 315 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 316 | Creating Futures and Tasks |
| 317 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Yury Selivanov | 950204d | 2016-05-16 16:23:00 -0400 | [diff] [blame] | 318 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 319 | .. method:: loop.create_future() |
Yury Selivanov | 950204d | 2016-05-16 16:23:00 -0400 | [diff] [blame] | 320 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 321 | Create an :class:`asyncio.Future` object attached to the event loop. |
Yury Selivanov | 950204d | 2016-05-16 16:23:00 -0400 | [diff] [blame] | 322 | |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 323 | This is the preferred way to create Futures in asyncio. This lets |
| 324 | third-party event loops provide alternative implementations of |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 325 | the Future object (with better performance or instrumentation). |
Yury Selivanov | 950204d | 2016-05-16 16:23:00 -0400 | [diff] [blame] | 326 | |
| 327 | .. versionadded:: 3.5.2 |
| 328 | |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 329 | .. method:: loop.create_task(coro, *, name=None) |
Yury Selivanov | 950204d | 2016-05-16 16:23:00 -0400 | [diff] [blame] | 330 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 331 | Schedule the execution of a :ref:`coroutine`. |
| 332 | Return a :class:`Task` object. |
Victor Stinner | 530ef2f | 2014-07-08 12:39:10 +0200 | [diff] [blame] | 333 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 334 | Third-party event loops can use their own subclass of :class:`Task` |
| 335 | for interoperability. In this case, the result type is a subclass |
| 336 | of :class:`Task`. |
Victor Stinner | 530ef2f | 2014-07-08 12:39:10 +0200 | [diff] [blame] | 337 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 338 | If the *name* argument is provided and not ``None``, it is set as |
| 339 | the name of the task using :meth:`Task.set_name`. |
Victor Stinner | 530ef2f | 2014-07-08 12:39:10 +0200 | [diff] [blame] | 340 | |
Alex Grönholm | cca4eec | 2018-08-09 00:06:47 +0300 | [diff] [blame] | 341 | .. versionchanged:: 3.8 |
| 342 | Added the ``name`` parameter. |
| 343 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 344 | .. method:: loop.set_task_factory(factory) |
Yury Selivanov | 7185461 | 2015-05-11 16:28:27 -0400 | [diff] [blame] | 345 | |
| 346 | Set a task factory that will be used by |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 347 | :meth:`loop.create_task`. |
Yury Selivanov | 7185461 | 2015-05-11 16:28:27 -0400 | [diff] [blame] | 348 | |
| 349 | If *factory* is ``None`` the default task factory will be set. |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 350 | Otherwise, *factory* must be a *callable* with the signature matching |
| 351 | ``(loop, coro)``, where *loop* is a reference to the active |
| 352 | event loop, and *coro* is a coroutine object. The callable |
| 353 | must return a :class:`asyncio.Future`-compatible object. |
Yury Selivanov | 7185461 | 2015-05-11 16:28:27 -0400 | [diff] [blame] | 354 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 355 | .. method:: loop.get_task_factory() |
Yury Selivanov | 7185461 | 2015-05-11 16:28:27 -0400 | [diff] [blame] | 356 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 357 | Return a task factory or ``None`` if the default one is in use. |
Yury Selivanov | 7185461 | 2015-05-11 16:28:27 -0400 | [diff] [blame] | 358 | |
Victor Stinner | 530ef2f | 2014-07-08 12:39:10 +0200 | [diff] [blame] | 359 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 360 | Opening network connections |
| 361 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 362 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 363 | .. coroutinemethod:: loop.create_connection(protocol_factory, \ |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 364 | host=None, port=None, *, ssl=None, \ |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 365 | family=0, proto=0, flags=0, sock=None, \ |
| 366 | local_addr=None, server_hostname=None, \ |
idomic | 5305cc9 | 2020-02-10 04:48:40 -0500 | [diff] [blame] | 367 | ssl_handshake_timeout=None, \ |
| 368 | happy_eyeballs_delay=None, interleave=None) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 369 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 370 | Open a streaming transport connection to a given |
| 371 | address specified by *host* and *port*. |
| 372 | |
| 373 | The socket family can be either :py:data:`~socket.AF_INET` or |
| 374 | :py:data:`~socket.AF_INET6` depending on *host* (or the *family* |
| 375 | argument, if provided). |
| 376 | |
| 377 | The socket type will be :py:data:`~socket.SOCK_STREAM`. |
| 378 | |
| 379 | *protocol_factory* must be a callable returning an |
| 380 | :ref:`asyncio protocol <asyncio-protocol>` implementation. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 381 | |
Yury Selivanov | 19a44f6 | 2017-12-14 20:53:26 -0500 | [diff] [blame] | 382 | This method will try to establish the connection in the background. |
| 383 | When successful, it returns a ``(transport, protocol)`` pair. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 384 | |
| 385 | The chronological synopsis of the underlying operation is as follows: |
| 386 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 387 | #. The connection is established and a :ref:`transport <asyncio-transport>` |
| 388 | is created for it. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 389 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 390 | #. *protocol_factory* is called without arguments and is expected to |
| 391 | return a :ref:`protocol <asyncio-protocol>` instance. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 392 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 393 | #. The protocol instance is coupled with the transport by calling its |
| 394 | :meth:`~BaseProtocol.connection_made` method. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 395 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 396 | #. A ``(transport, protocol)`` tuple is returned on success. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 397 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 398 | The created transport is an implementation-dependent bidirectional |
| 399 | stream. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 400 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 401 | Other arguments: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 402 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 403 | * *ssl*: if given and not false, a SSL/TLS transport is created |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 404 | (by default a plain TCP transport is created). If *ssl* is |
| 405 | a :class:`ssl.SSLContext` object, this context is used to create |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 406 | the transport; if *ssl* is :const:`True`, a default context returned |
| 407 | from :func:`ssl.create_default_context` is used. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 408 | |
Berker Peksag | 9c1dba2 | 2014-09-28 00:00:58 +0300 | [diff] [blame] | 409 | .. seealso:: :ref:`SSL/TLS security considerations <ssl-security>` |
Antoine Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 410 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 411 | * *server_hostname* sets or overrides the hostname that the target |
| 412 | server's certificate will be matched against. Should only be passed |
| 413 | if *ssl* is not ``None``. By default the value of the *host* argument |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 414 | is used. If *host* is empty, there is no default and you must pass a |
| 415 | value for *server_hostname*. If *server_hostname* is an empty |
| 416 | string, hostname matching is disabled (which is a serious security |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 417 | risk, allowing for potential man-in-the-middle attacks). |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 418 | |
| 419 | * *family*, *proto*, *flags* are the optional address family, protocol |
| 420 | and flags to be passed through to getaddrinfo() for *host* resolution. |
| 421 | If given, these should all be integers from the corresponding |
| 422 | :mod:`socket` module constants. |
| 423 | |
twisteroid ambassador | 88f07a8 | 2019-05-05 19:14:35 +0800 | [diff] [blame] | 424 | * *happy_eyeballs_delay*, if given, enables Happy Eyeballs for this |
| 425 | connection. It should |
| 426 | be a floating-point number representing the amount of time in seconds |
| 427 | to wait for a connection attempt to complete, before starting the next |
| 428 | attempt in parallel. This is the "Connection Attempt Delay" as defined |
| 429 | in :rfc:`8305`. A sensible default value recommended by the RFC is ``0.25`` |
| 430 | (250 milliseconds). |
| 431 | |
| 432 | * *interleave* controls address reordering when a host name resolves to |
| 433 | multiple IP addresses. |
| 434 | If ``0`` or unspecified, no reordering is done, and addresses are |
| 435 | tried in the order returned by :meth:`getaddrinfo`. If a positive integer |
| 436 | is specified, the addresses are interleaved by address family, and the |
| 437 | given integer is interpreted as "First Address Family Count" as defined |
| 438 | in :rfc:`8305`. The default is ``0`` if *happy_eyeballs_delay* is not |
| 439 | specified, and ``1`` if it is. |
| 440 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 441 | * *sock*, if given, should be an existing, already connected |
| 442 | :class:`socket.socket` object to be used by the transport. |
twisteroid ambassador | 88f07a8 | 2019-05-05 19:14:35 +0800 | [diff] [blame] | 443 | If *sock* is given, none of *host*, *port*, *family*, *proto*, *flags*, |
| 444 | *happy_eyeballs_delay*, *interleave* |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 445 | and *local_addr* should be specified. |
| 446 | |
| 447 | * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used |
Miss Islington (bot) | 9d16b1a | 2021-05-19 14:19:53 -0700 | [diff] [blame] | 448 | to bind the socket locally. The *local_host* and *local_port* |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 449 | are looked up using ``getaddrinfo()``, similarly to *host* and *port*. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 450 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 451 | * *ssl_handshake_timeout* is (for a TLS connection) the time in seconds |
| 452 | to wait for the TLS handshake to complete before aborting the connection. |
Yury Selivanov | 9602643 | 2018-06-04 11:32:35 -0400 | [diff] [blame] | 453 | ``60.0`` seconds if ``None`` (default). |
Neil Aspinall | f7686c1 | 2017-12-19 19:45:42 +0000 | [diff] [blame] | 454 | |
twisteroid ambassador | 88f07a8 | 2019-05-05 19:14:35 +0800 | [diff] [blame] | 455 | .. versionadded:: 3.8 |
| 456 | |
idomic | 5305cc9 | 2020-02-10 04:48:40 -0500 | [diff] [blame] | 457 | Added the *happy_eyeballs_delay* and *interleave* parameters. |
twisteroid ambassador | 88f07a8 | 2019-05-05 19:14:35 +0800 | [diff] [blame] | 458 | |
idomic | 8af4712 | 2020-02-24 09:59:40 -0500 | [diff] [blame] | 459 | Happy Eyeballs Algorithm: Success with Dual-Stack Hosts. |
| 460 | When a server's IPv4 path and protocol are working, but the server's |
| 461 | IPv6 path and protocol are not working, a dual-stack client |
| 462 | application experiences significant connection delay compared to an |
| 463 | IPv4-only client. This is undesirable because it causes the dual- |
| 464 | stack client to have a worse user experience. This document |
| 465 | specifies requirements for algorithms that reduce this user-visible |
| 466 | delay and provides an algorithm. |
| 467 | |
| 468 | For more information: https://tools.ietf.org/html/rfc6555 |
| 469 | |
Neil Aspinall | f7686c1 | 2017-12-19 19:45:42 +0000 | [diff] [blame] | 470 | .. versionadded:: 3.7 |
| 471 | |
| 472 | The *ssl_handshake_timeout* parameter. |
| 473 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 474 | .. versionchanged:: 3.6 |
| 475 | |
| 476 | The socket option :py:data:`~socket.TCP_NODELAY` is set by default |
| 477 | for all TCP connections. |
| 478 | |
Victor Stinner | 60208a1 | 2015-09-15 22:41:52 +0200 | [diff] [blame] | 479 | .. versionchanged:: 3.5 |
| 480 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 481 | Added support for SSL/TLS in :class:`ProactorEventLoop`. |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 482 | |
Victor Stinner | c8ea813 | 2014-01-23 11:02:09 +0100 | [diff] [blame] | 483 | .. seealso:: |
| 484 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 485 | The :func:`open_connection` function is a high-level alternative |
| 486 | API. It returns a pair of (:class:`StreamReader`, :class:`StreamWriter`) |
| 487 | that can be used directly in async/await code. |
Victor Stinner | c8ea813 | 2014-01-23 11:02:09 +0100 | [diff] [blame] | 488 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 489 | .. coroutinemethod:: loop.create_datagram_endpoint(protocol_factory, \ |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 490 | local_addr=None, remote_addr=None, *, \ |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 491 | family=0, proto=0, flags=0, \ |
| 492 | reuse_address=None, reuse_port=None, \ |
| 493 | allow_broadcast=None, sock=None) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 494 | |
Kyle Stanley | ab513a3 | 2019-12-09 09:21:10 -0500 | [diff] [blame] | 495 | .. note:: |
| 496 | The parameter *reuse_address* is no longer supported, as using |
| 497 | :py:data:`~sockets.SO_REUSEADDR` poses a significant security concern for |
| 498 | UDP. Explicitly passing ``reuse_address=True`` will raise an exception. |
| 499 | |
| 500 | When multiple processes with differing UIDs assign sockets to an |
Jesús Cea | b0d4949 | 2019-12-20 03:21:03 +0100 | [diff] [blame] | 501 | identical UDP socket address with ``SO_REUSEADDR``, incoming packets can |
Kyle Stanley | ab513a3 | 2019-12-09 09:21:10 -0500 | [diff] [blame] | 502 | become randomly distributed among the sockets. |
| 503 | |
| 504 | For supported platforms, *reuse_port* can be used as a replacement for |
| 505 | similar functionality. With *reuse_port*, |
| 506 | :py:data:`~sockets.SO_REUSEPORT` is used instead, which specifically |
| 507 | prevents processes with differing UIDs from assigning sockets to the same |
| 508 | socket address. |
| 509 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 510 | Create a datagram connection. |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 511 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 512 | The socket family can be either :py:data:`~socket.AF_INET`, |
| 513 | :py:data:`~socket.AF_INET6`, or :py:data:`~socket.AF_UNIX`, |
| 514 | depending on *host* (or the *family* argument, if provided). |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 515 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 516 | The socket type will be :py:data:`~socket.SOCK_DGRAM`. |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 517 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 518 | *protocol_factory* must be a callable returning a |
| 519 | :ref:`protocol <asyncio-protocol>` implementation. |
| 520 | |
| 521 | A tuple of ``(transport, protocol)`` is returned on success. |
| 522 | |
| 523 | Other arguments: |
Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 524 | |
| 525 | * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used |
Miss Islington (bot) | 9d16b1a | 2021-05-19 14:19:53 -0700 | [diff] [blame] | 526 | to bind the socket locally. The *local_host* and *local_port* |
Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 527 | are looked up using :meth:`getaddrinfo`. |
| 528 | |
| 529 | * *remote_addr*, if given, is a ``(remote_host, remote_port)`` tuple used |
| 530 | to connect the socket to a remote address. The *remote_host* and |
| 531 | *remote_port* are looked up using :meth:`getaddrinfo`. |
| 532 | |
| 533 | * *family*, *proto*, *flags* are the optional address family, protocol |
| 534 | and flags to be passed through to :meth:`getaddrinfo` for *host* |
| 535 | resolution. If given, these should all be integers from the |
| 536 | corresponding :mod:`socket` module constants. |
| 537 | |
Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 538 | * *reuse_port* tells the kernel to allow this endpoint to be bound to the |
| 539 | same port as other existing endpoints are bound to, so long as they all |
| 540 | set this flag when being created. This option is not supported on Windows |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 541 | and some Unixes. If the :py:data:`~socket.SO_REUSEPORT` constant is not |
Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 542 | defined then this capability is unsupported. |
| 543 | |
| 544 | * *allow_broadcast* tells the kernel to allow this endpoint to send |
| 545 | messages to the broadcast address. |
| 546 | |
| 547 | * *sock* can optionally be specified in order to use a preexisting, |
| 548 | already connected, :class:`socket.socket` object to be used by the |
| 549 | transport. If specified, *local_addr* and *remote_addr* should be omitted |
| 550 | (must be :const:`None`). |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 551 | |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 552 | See :ref:`UDP echo client protocol <asyncio-udp-echo-client-protocol>` and |
| 553 | :ref:`UDP echo server protocol <asyncio-udp-echo-server-protocol>` examples. |
| 554 | |
Romuald Brunet | 0ded580 | 2018-05-14 18:22:00 +0200 | [diff] [blame] | 555 | .. versionchanged:: 3.4.4 |
| 556 | The *family*, *proto*, *flags*, *reuse_address*, *reuse_port, |
| 557 | *allow_broadcast*, and *sock* parameters were added. |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 558 | |
Kyle Stanley | ab513a3 | 2019-12-09 09:21:10 -0500 | [diff] [blame] | 559 | .. versionchanged:: 3.8.1 |
| 560 | The *reuse_address* parameter is no longer supported due to security |
| 561 | concerns. |
| 562 | |
Andrew Svetlov | bafd4b5 | 2019-05-28 12:52:15 +0300 | [diff] [blame] | 563 | .. versionchanged:: 3.8 |
| 564 | Added support for Windows. |
| 565 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 566 | .. coroutinemethod:: loop.create_unix_connection(protocol_factory, \ |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 567 | path=None, *, ssl=None, sock=None, \ |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 568 | server_hostname=None, ssl_handshake_timeout=None) |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 569 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 570 | Create a Unix connection. |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 571 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 572 | The socket family will be :py:data:`~socket.AF_UNIX`; socket |
| 573 | type will be :py:data:`~socket.SOCK_STREAM`. |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 574 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 575 | A tuple of ``(transport, protocol)`` is returned on success. |
Guido van Rossum | 9e80eeb | 2016-11-03 14:17:25 -0700 | [diff] [blame] | 576 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 577 | *path* is the name of a Unix domain socket and is required, |
| 578 | unless a *sock* parameter is specified. Abstract Unix sockets, |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 579 | :class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths are |
| 580 | supported. |
| 581 | |
| 582 | See the documentation of the :meth:`loop.create_connection` method |
| 583 | for information about arguments to this method. |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 584 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 585 | .. availability:: Unix. |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 586 | |
Neil Aspinall | f7686c1 | 2017-12-19 19:45:42 +0000 | [diff] [blame] | 587 | .. versionadded:: 3.7 |
| 588 | |
| 589 | The *ssl_handshake_timeout* parameter. |
| 590 | |
Yury Selivanov | 423fd36 | 2017-11-20 17:26:28 -0500 | [diff] [blame] | 591 | .. versionchanged:: 3.7 |
| 592 | |
Elvis Pranskevichus | c0d062f | 2018-06-08 11:36:00 -0400 | [diff] [blame] | 593 | The *path* parameter can now be a :term:`path-like object`. |
Yury Selivanov | 423fd36 | 2017-11-20 17:26:28 -0500 | [diff] [blame] | 594 | |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 595 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 596 | Creating network servers |
| 597 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 598 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 599 | .. coroutinemethod:: loop.create_server(protocol_factory, \ |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 600 | host=None, port=None, *, \ |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 601 | family=socket.AF_UNSPEC, \ |
| 602 | flags=socket.AI_PASSIVE, \ |
| 603 | sock=None, backlog=100, ssl=None, \ |
| 604 | reuse_address=None, reuse_port=None, \ |
| 605 | ssl_handshake_timeout=None, start_serving=True) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 606 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 607 | Create a TCP server (socket type :data:`~socket.SOCK_STREAM`) listening |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 608 | on *port* of the *host* address. |
Victor Stinner | 33f6abe | 2014-10-12 20:36:04 +0200 | [diff] [blame] | 609 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 610 | Returns a :class:`Server` object. |
Victor Stinner | 33f6abe | 2014-10-12 20:36:04 +0200 | [diff] [blame] | 611 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 612 | Arguments: |
Victor Stinner | 33f6abe | 2014-10-12 20:36:04 +0200 | [diff] [blame] | 613 | |
Bumsik Kim | 5cc583d | 2018-09-16 19:40:44 -0400 | [diff] [blame] | 614 | * *protocol_factory* must be a callable returning a |
| 615 | :ref:`protocol <asyncio-protocol>` implementation. |
| 616 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 617 | * The *host* parameter can be set to several types which determine where |
| 618 | the server would be listening: |
| 619 | |
| 620 | - If *host* is a string, the TCP server is bound to a single network |
| 621 | interface specified by *host*. |
| 622 | |
| 623 | - If *host* is a sequence of strings, the TCP server is bound to all |
| 624 | network interfaces specified by the sequence. |
| 625 | |
| 626 | - If *host* is an empty string or ``None``, all interfaces are |
| 627 | assumed and a list of multiple sockets will be returned (most likely |
| 628 | one for IPv4 and another one for IPv6). |
Victor Stinner | 33f6abe | 2014-10-12 20:36:04 +0200 | [diff] [blame] | 629 | |
| 630 | * *family* can be set to either :data:`socket.AF_INET` or |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 631 | :data:`~socket.AF_INET6` to force the socket to use IPv4 or IPv6. |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 632 | If not set, the *family* will be determined from host name |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 633 | (defaults to :data:`~socket.AF_UNSPEC`). |
Victor Stinner | 33f6abe | 2014-10-12 20:36:04 +0200 | [diff] [blame] | 634 | |
| 635 | * *flags* is a bitmask for :meth:`getaddrinfo`. |
| 636 | |
| 637 | * *sock* can optionally be specified in order to use a preexisting |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 638 | socket object. If specified, *host* and *port* must not be specified. |
Victor Stinner | 33f6abe | 2014-10-12 20:36:04 +0200 | [diff] [blame] | 639 | |
| 640 | * *backlog* is the maximum number of queued connections passed to |
| 641 | :meth:`~socket.socket.listen` (defaults to 100). |
| 642 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 643 | * *ssl* can be set to an :class:`~ssl.SSLContext` instance to enable |
| 644 | TLS over the accepted connections. |
Victor Stinner | 33f6abe | 2014-10-12 20:36:04 +0200 | [diff] [blame] | 645 | |
| 646 | * *reuse_address* tells the kernel to reuse a local socket in |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 647 | ``TIME_WAIT`` state, without waiting for its natural timeout to |
Serhiy Storchaka | 4adf01c | 2016-10-19 18:30:05 +0300 | [diff] [blame] | 648 | expire. If not specified will automatically be set to ``True`` on |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 649 | Unix. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 650 | |
Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 651 | * *reuse_port* tells the kernel to allow this endpoint to be bound to the |
| 652 | same port as other existing endpoints are bound to, so long as they all |
| 653 | set this flag when being created. This option is not supported on |
| 654 | Windows. |
| 655 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 656 | * *ssl_handshake_timeout* is (for a TLS server) the time in seconds to wait |
| 657 | for the TLS handshake to complete before aborting the connection. |
Yury Selivanov | 9602643 | 2018-06-04 11:32:35 -0400 | [diff] [blame] | 658 | ``60.0`` seconds if ``None`` (default). |
Neil Aspinall | f7686c1 | 2017-12-19 19:45:42 +0000 | [diff] [blame] | 659 | |
Yury Selivanov | c9070d0 | 2018-01-25 18:08:09 -0500 | [diff] [blame] | 660 | * *start_serving* set to ``True`` (the default) causes the created server |
| 661 | to start accepting connections immediately. When set to ``False``, |
| 662 | the user should await on :meth:`Server.start_serving` or |
| 663 | :meth:`Server.serve_forever` to make the server to start accepting |
| 664 | connections. |
| 665 | |
Neil Aspinall | f7686c1 | 2017-12-19 19:45:42 +0000 | [diff] [blame] | 666 | .. versionadded:: 3.7 |
| 667 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 668 | Added *ssl_handshake_timeout* and *start_serving* parameters. |
| 669 | |
| 670 | .. versionchanged:: 3.6 |
| 671 | |
| 672 | The socket option :py:data:`~socket.TCP_NODELAY` is set by default |
| 673 | for all TCP connections. |
Neil Aspinall | f7686c1 | 2017-12-19 19:45:42 +0000 | [diff] [blame] | 674 | |
Victor Stinner | 60208a1 | 2015-09-15 22:41:52 +0200 | [diff] [blame] | 675 | .. versionchanged:: 3.5 |
| 676 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 677 | Added support for SSL/TLS in :class:`ProactorEventLoop`. |
Victor Stinner | c8ea813 | 2014-01-23 11:02:09 +0100 | [diff] [blame] | 678 | |
Victor Stinner | 7b58a2b | 2015-09-21 18:41:05 +0200 | [diff] [blame] | 679 | .. versionchanged:: 3.5.1 |
Victor Stinner | 5e4a7d8 | 2015-09-21 18:33:43 +0200 | [diff] [blame] | 680 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 681 | The *host* parameter can be a sequence of strings. |
| 682 | |
| 683 | .. seealso:: |
| 684 | |
| 685 | The :func:`start_server` function is a higher-level alternative API |
| 686 | that returns a pair of :class:`StreamReader` and :class:`StreamWriter` |
| 687 | that can be used in an async/await code. |
Victor Stinner | 5e4a7d8 | 2015-09-21 18:33:43 +0200 | [diff] [blame] | 688 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 689 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 690 | .. coroutinemethod:: loop.create_unix_server(protocol_factory, path=None, \ |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 691 | *, sock=None, backlog=100, ssl=None, \ |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 692 | ssl_handshake_timeout=None, start_serving=True) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 693 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 694 | Similar to :meth:`loop.create_server` but works with the |
| 695 | :py:data:`~socket.AF_UNIX` socket family. |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 696 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 697 | *path* is the name of a Unix domain socket, and is required, |
| 698 | unless a *sock* argument is provided. Abstract Unix sockets, |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 699 | :class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths |
| 700 | are supported. |
Yury Selivanov | 423fd36 | 2017-11-20 17:26:28 -0500 | [diff] [blame] | 701 | |
Bumsik Kim | 5cc583d | 2018-09-16 19:40:44 -0400 | [diff] [blame] | 702 | See the documentation of the :meth:`loop.create_server` method |
| 703 | for information about arguments to this method. |
| 704 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 705 | .. availability:: Unix. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 706 | |
Neil Aspinall | f7686c1 | 2017-12-19 19:45:42 +0000 | [diff] [blame] | 707 | .. versionadded:: 3.7 |
| 708 | |
Elvis Pranskevichus | c0d062f | 2018-06-08 11:36:00 -0400 | [diff] [blame] | 709 | The *ssl_handshake_timeout* and *start_serving* parameters. |
Neil Aspinall | f7686c1 | 2017-12-19 19:45:42 +0000 | [diff] [blame] | 710 | |
Yury Selivanov | 423fd36 | 2017-11-20 17:26:28 -0500 | [diff] [blame] | 711 | .. versionchanged:: 3.7 |
| 712 | |
| 713 | The *path* parameter can now be a :class:`~pathlib.Path` object. |
| 714 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 715 | .. coroutinemethod:: loop.connect_accepted_socket(protocol_factory, \ |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 716 | sock, *, ssl=None, ssl_handshake_timeout=None) |
Yury Selivanov | 3b3a141 | 2016-11-07 15:35:25 -0500 | [diff] [blame] | 717 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 718 | Wrap an already accepted connection into a transport/protocol pair. |
Yury Selivanov | 3b3a141 | 2016-11-07 15:35:25 -0500 | [diff] [blame] | 719 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 720 | This method can be used by servers that accept connections outside |
| 721 | of asyncio but that use asyncio to handle them. |
Yury Selivanov | 3b3a141 | 2016-11-07 15:35:25 -0500 | [diff] [blame] | 722 | |
| 723 | Parameters: |
| 724 | |
Bumsik Kim | 5cc583d | 2018-09-16 19:40:44 -0400 | [diff] [blame] | 725 | * *protocol_factory* must be a callable returning a |
| 726 | :ref:`protocol <asyncio-protocol>` implementation. |
| 727 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 728 | * *sock* is a preexisting socket object returned from |
| 729 | :meth:`socket.accept <socket.socket.accept>`. |
Yury Selivanov | 3b3a141 | 2016-11-07 15:35:25 -0500 | [diff] [blame] | 730 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 731 | * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over |
| 732 | the accepted connections. |
Yury Selivanov | 3b3a141 | 2016-11-07 15:35:25 -0500 | [diff] [blame] | 733 | |
Neil Aspinall | f7686c1 | 2017-12-19 19:45:42 +0000 | [diff] [blame] | 734 | * *ssl_handshake_timeout* is (for an SSL connection) the time in seconds to |
| 735 | wait for the SSL handshake to complete before aborting the connection. |
Yury Selivanov | 9602643 | 2018-06-04 11:32:35 -0400 | [diff] [blame] | 736 | ``60.0`` seconds if ``None`` (default). |
Neil Aspinall | f7686c1 | 2017-12-19 19:45:42 +0000 | [diff] [blame] | 737 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 738 | Returns a ``(transport, protocol)`` pair. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 739 | |
Neil Aspinall | f7686c1 | 2017-12-19 19:45:42 +0000 | [diff] [blame] | 740 | .. versionadded:: 3.7 |
| 741 | |
| 742 | The *ssl_handshake_timeout* parameter. |
| 743 | |
AraHaan | 431665b | 2017-11-21 11:06:26 -0500 | [diff] [blame] | 744 | .. versionadded:: 3.5.3 |
| 745 | |
| 746 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 747 | Transferring files |
| 748 | ^^^^^^^^^^^^^^^^^^ |
Andrew Svetlov | 7c68407 | 2018-01-27 21:22:47 +0200 | [diff] [blame] | 749 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 750 | .. coroutinemethod:: loop.sendfile(transport, file, \ |
| 751 | offset=0, count=None, *, fallback=True) |
Andrew Svetlov | 7c68407 | 2018-01-27 21:22:47 +0200 | [diff] [blame] | 752 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 753 | Send a *file* over a *transport*. Return the total number of bytes |
| 754 | sent. |
Andrew Svetlov | 7c68407 | 2018-01-27 21:22:47 +0200 | [diff] [blame] | 755 | |
| 756 | The method uses high-performance :meth:`os.sendfile` if available. |
| 757 | |
| 758 | *file* must be a regular file object opened in binary mode. |
| 759 | |
| 760 | *offset* tells from where to start reading the file. If specified, |
| 761 | *count* is the total number of bytes to transmit as opposed to |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 762 | sending the file until EOF is reached. File position is always updated, |
| 763 | even when this method raises an error, and |
| 764 | :meth:`file.tell() <io.IOBase.tell>` can be used to obtain the actual |
| 765 | number of bytes sent. |
Andrew Svetlov | 7c68407 | 2018-01-27 21:22:47 +0200 | [diff] [blame] | 766 | |
| 767 | *fallback* set to ``True`` makes asyncio to manually read and send |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 768 | the file when the platform does not support the sendfile system call |
Andrew Svetlov | 7c68407 | 2018-01-27 21:22:47 +0200 | [diff] [blame] | 769 | (e.g. Windows or SSL socket on Unix). |
| 770 | |
| 771 | Raise :exc:`SendfileNotAvailableError` if the system does not support |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 772 | the *sendfile* syscall and *fallback* is ``False``. |
Andrew Svetlov | 7c68407 | 2018-01-27 21:22:47 +0200 | [diff] [blame] | 773 | |
| 774 | .. versionadded:: 3.7 |
| 775 | |
| 776 | |
Yury Selivanov | f111b3d | 2017-12-30 00:35:36 -0500 | [diff] [blame] | 777 | TLS Upgrade |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 778 | ^^^^^^^^^^^ |
Yury Selivanov | f111b3d | 2017-12-30 00:35:36 -0500 | [diff] [blame] | 779 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 780 | .. coroutinemethod:: loop.start_tls(transport, protocol, \ |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 781 | sslcontext, *, server_side=False, \ |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 782 | server_hostname=None, ssl_handshake_timeout=None) |
Yury Selivanov | f111b3d | 2017-12-30 00:35:36 -0500 | [diff] [blame] | 783 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 784 | Upgrade an existing transport-based connection to TLS. |
Yury Selivanov | f111b3d | 2017-12-30 00:35:36 -0500 | [diff] [blame] | 785 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 786 | Return a new transport instance, that the *protocol* must start using |
Yury Selivanov | f111b3d | 2017-12-30 00:35:36 -0500 | [diff] [blame] | 787 | immediately after the *await*. The *transport* instance passed to |
| 788 | the *start_tls* method should never be used again. |
| 789 | |
| 790 | Parameters: |
| 791 | |
| 792 | * *transport* and *protocol* instances that methods like |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 793 | :meth:`~loop.create_server` and |
| 794 | :meth:`~loop.create_connection` return. |
Yury Selivanov | f111b3d | 2017-12-30 00:35:36 -0500 | [diff] [blame] | 795 | |
| 796 | * *sslcontext*: a configured instance of :class:`~ssl.SSLContext`. |
| 797 | |
| 798 | * *server_side* pass ``True`` when a server-side connection is being |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 799 | upgraded (like the one created by :meth:`~loop.create_server`). |
Yury Selivanov | f111b3d | 2017-12-30 00:35:36 -0500 | [diff] [blame] | 800 | |
| 801 | * *server_hostname*: sets or overrides the host name that the target |
| 802 | server's certificate will be matched against. |
| 803 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 804 | * *ssl_handshake_timeout* is (for a TLS connection) the time in seconds to |
| 805 | wait for the TLS handshake to complete before aborting the connection. |
Yury Selivanov | 9602643 | 2018-06-04 11:32:35 -0400 | [diff] [blame] | 806 | ``60.0`` seconds if ``None`` (default). |
Yury Selivanov | f111b3d | 2017-12-30 00:35:36 -0500 | [diff] [blame] | 807 | |
| 808 | .. versionadded:: 3.7 |
| 809 | |
| 810 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 811 | Watching file descriptors |
| 812 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 813 | |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 814 | .. method:: loop.add_reader(fd, callback, *args) |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 815 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 816 | Start monitoring the *fd* file descriptor for read availability and |
| 817 | invoke *callback* with the specified arguments once *fd* is available for |
| 818 | reading. |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 819 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 820 | .. method:: loop.remove_reader(fd) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 821 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 822 | Stop monitoring the *fd* file descriptor for read availability. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 823 | |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 824 | .. method:: loop.add_writer(fd, callback, *args) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 825 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 826 | Start monitoring the *fd* file descriptor for write availability and |
| 827 | invoke *callback* with the specified arguments once *fd* is available for |
| 828 | writing. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 829 | |
Yury Selivanov | e247b46 | 2018-09-20 12:43:59 -0400 | [diff] [blame] | 830 | Use :func:`functools.partial` :ref:`to pass keyword arguments |
Naglis | 1747334 | 2018-12-04 09:31:15 +0200 | [diff] [blame] | 831 | <asyncio-pass-keywords>` to *callback*. |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 832 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 833 | .. method:: loop.remove_writer(fd) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 834 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 835 | Stop monitoring the *fd* file descriptor for write availability. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 836 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 837 | See also :ref:`Platform Support <asyncio-platform-support>` section |
| 838 | for some limitations of these methods. |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 839 | |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 840 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 841 | Working with socket objects directly |
| 842 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 843 | |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 844 | In general, protocol implementations that use transport-based APIs |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 845 | such as :meth:`loop.create_connection` and :meth:`loop.create_server` |
| 846 | are faster than implementations that work with sockets directly. |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 847 | However, there are some use cases when performance is not critical, and |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 848 | working with :class:`~socket.socket` objects directly is more |
| 849 | convenient. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 850 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 851 | .. coroutinemethod:: loop.sock_recv(sock, nbytes) |
Yury Selivanov | 55c5084 | 2016-06-08 12:48:15 -0400 | [diff] [blame] | 852 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 853 | Receive up to *nbytes* from *sock*. Asynchronous version of |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 854 | :meth:`socket.recv() <socket.socket.recv>`. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 855 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 856 | Return the received data as a bytes object. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 857 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 858 | *sock* must be a non-blocking socket. |
Victor Stinner | ec2ce09 | 2014-07-29 23:12:22 +0200 | [diff] [blame] | 859 | |
Yury Selivanov | 19a44f6 | 2017-12-14 20:53:26 -0500 | [diff] [blame] | 860 | .. versionchanged:: 3.7 |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 861 | Even though this method was always documented as a coroutine |
| 862 | method, releases before Python 3.7 returned a :class:`Future`. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 863 | Since Python 3.7 this is an ``async def`` method. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 864 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 865 | .. coroutinemethod:: loop.sock_recv_into(sock, buf) |
Antoine Pitrou | 525f40d | 2017-10-19 21:46:40 +0200 | [diff] [blame] | 866 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 867 | Receive data from *sock* into the *buf* buffer. Modeled after the blocking |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 868 | :meth:`socket.recv_into() <socket.socket.recv_into>` method. |
Antoine Pitrou | 525f40d | 2017-10-19 21:46:40 +0200 | [diff] [blame] | 869 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 870 | Return the number of bytes written to the buffer. |
Antoine Pitrou | 525f40d | 2017-10-19 21:46:40 +0200 | [diff] [blame] | 871 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 872 | *sock* must be a non-blocking socket. |
Antoine Pitrou | 525f40d | 2017-10-19 21:46:40 +0200 | [diff] [blame] | 873 | |
Antoine Pitrou | 525f40d | 2017-10-19 21:46:40 +0200 | [diff] [blame] | 874 | .. versionadded:: 3.7 |
| 875 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 876 | .. coroutinemethod:: loop.sock_sendall(sock, data) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 877 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 878 | Send *data* to the *sock* socket. Asynchronous version of |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 879 | :meth:`socket.sendall() <socket.socket.sendall>`. |
Yury Selivanov | 55c5084 | 2016-06-08 12:48:15 -0400 | [diff] [blame] | 880 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 881 | This method continues to send to the socket until either all data |
| 882 | in *data* has been sent or an error occurs. ``None`` is returned |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 883 | on success. On error, an exception is raised. Additionally, there is no way |
| 884 | to determine how much data, if any, was successfully processed by the |
| 885 | receiving end of the connection. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 886 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 887 | *sock* must be a non-blocking socket. |
Victor Stinner | ec2ce09 | 2014-07-29 23:12:22 +0200 | [diff] [blame] | 888 | |
Yury Selivanov | 19a44f6 | 2017-12-14 20:53:26 -0500 | [diff] [blame] | 889 | .. versionchanged:: 3.7 |
| 890 | Even though the method was always documented as a coroutine |
| 891 | method, before Python 3.7 it returned an :class:`Future`. |
| 892 | Since Python 3.7, this is an ``async def`` method. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 893 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 894 | .. coroutinemethod:: loop.sock_connect(sock, address) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 895 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 896 | Connect *sock* to a remote socket at *address*. |
Victor Stinner | 1b0580b | 2014-02-13 09:24:37 +0100 | [diff] [blame] | 897 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 898 | Asynchronous version of :meth:`socket.connect() <socket.socket.connect>`. |
| 899 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 900 | *sock* must be a non-blocking socket. |
Victor Stinner | ec2ce09 | 2014-07-29 23:12:22 +0200 | [diff] [blame] | 901 | |
Yury Selivanov | 55c5084 | 2016-06-08 12:48:15 -0400 | [diff] [blame] | 902 | .. versionchanged:: 3.5.2 |
| 903 | ``address`` no longer needs to be resolved. ``sock_connect`` |
| 904 | will try to check if the *address* is already resolved by calling |
| 905 | :func:`socket.inet_pton`. If not, |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 906 | :meth:`loop.getaddrinfo` will be used to resolve the |
Yury Selivanov | 55c5084 | 2016-06-08 12:48:15 -0400 | [diff] [blame] | 907 | *address*. |
| 908 | |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 909 | .. seealso:: |
| 910 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 911 | :meth:`loop.create_connection` |
Yury Selivanov | 55c5084 | 2016-06-08 12:48:15 -0400 | [diff] [blame] | 912 | and :func:`asyncio.open_connection() <open_connection>`. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 913 | |
| 914 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 915 | .. coroutinemethod:: loop.sock_accept(sock) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 916 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 917 | Accept a connection. Modeled after the blocking |
| 918 | :meth:`socket.accept() <socket.socket.accept>` method. |
Yury Selivanov | 55c5084 | 2016-06-08 12:48:15 -0400 | [diff] [blame] | 919 | |
| 920 | The socket must be bound to an address and listening |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 921 | for connections. The return value is a pair ``(conn, address)`` where *conn* |
| 922 | is a *new* socket object usable to send and receive data on the connection, |
| 923 | and *address* is the address bound to the socket on the other end of the |
| 924 | connection. |
| 925 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 926 | *sock* must be a non-blocking socket. |
Victor Stinner | ec2ce09 | 2014-07-29 23:12:22 +0200 | [diff] [blame] | 927 | |
Yury Selivanov | 19a44f6 | 2017-12-14 20:53:26 -0500 | [diff] [blame] | 928 | .. versionchanged:: 3.7 |
| 929 | Even though the method was always documented as a coroutine |
| 930 | method, before Python 3.7 it returned a :class:`Future`. |
| 931 | Since Python 3.7, this is an ``async def`` method. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 932 | |
| 933 | .. seealso:: |
| 934 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 935 | :meth:`loop.create_server` and :func:`start_server`. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 936 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 937 | .. coroutinemethod:: loop.sock_sendfile(sock, file, offset=0, count=None, \ |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 938 | *, fallback=True) |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 939 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 940 | Send a file using high-performance :mod:`os.sendfile` if possible. |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 941 | Return the total number of bytes sent. |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 942 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 943 | Asynchronous version of :meth:`socket.sendfile() <socket.socket.sendfile>`. |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 944 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 945 | *sock* must be a non-blocking :const:`socket.SOCK_STREAM` |
| 946 | :class:`~socket.socket`. |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 947 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 948 | *file* must be a regular file object open in binary mode. |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 949 | |
| 950 | *offset* tells from where to start reading the file. If specified, |
| 951 | *count* is the total number of bytes to transmit as opposed to |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 952 | sending the file until EOF is reached. File position is always updated, |
| 953 | even when this method raises an error, and |
| 954 | :meth:`file.tell() <io.IOBase.tell>` can be used to obtain the actual |
| 955 | number of bytes sent. |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 956 | |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 957 | *fallback*, when set to ``True``, makes asyncio manually read and send |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 958 | the file when the platform does not support the sendfile syscall |
| 959 | (e.g. Windows or SSL socket on Unix). |
| 960 | |
Andrew Svetlov | 7464e87 | 2018-01-19 20:04:29 +0200 | [diff] [blame] | 961 | Raise :exc:`SendfileNotAvailableError` if the system does not support |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 962 | *sendfile* syscall and *fallback* is ``False``. |
| 963 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 964 | *sock* must be a non-blocking socket. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 965 | |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 966 | .. versionadded:: 3.7 |
| 967 | |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 968 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 969 | DNS |
| 970 | ^^^ |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 971 | |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 972 | .. coroutinemethod:: loop.getaddrinfo(host, port, *, family=0, \ |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 973 | type=0, proto=0, flags=0) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 974 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 975 | Asynchronous version of :meth:`socket.getaddrinfo`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 976 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 977 | .. coroutinemethod:: loop.getnameinfo(sockaddr, flags=0) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 978 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 979 | Asynchronous version of :meth:`socket.getnameinfo`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 980 | |
Yury Selivanov | bec2372 | 2018-01-28 14:09:40 -0500 | [diff] [blame] | 981 | .. versionchanged:: 3.7 |
| 982 | Both *getaddrinfo* and *getnameinfo* methods were always documented |
| 983 | to return a coroutine, but prior to Python 3.7 they were, in fact, |
| 984 | returning :class:`asyncio.Future` objects. Starting with Python 3.7 |
| 985 | both methods are coroutines. |
| 986 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 987 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 988 | Working with pipes |
| 989 | ^^^^^^^^^^^^^^^^^^ |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 990 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 991 | .. coroutinemethod:: loop.connect_read_pipe(protocol_factory, pipe) |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 992 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 993 | Register the read end of *pipe* in the event loop. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 994 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 995 | *protocol_factory* must be a callable returning an |
| 996 | :ref:`asyncio protocol <asyncio-protocol>` implementation. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 997 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 998 | *pipe* is a :term:`file-like object <file object>`. |
| 999 | |
| 1000 | Return pair ``(transport, protocol)``, where *transport* supports |
Bumsik Kim | 5cc583d | 2018-09-16 19:40:44 -0400 | [diff] [blame] | 1001 | the :class:`ReadTransport` interface and *protocol* is an object |
| 1002 | instantiated by the *protocol_factory*. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1003 | |
Victor Stinner | d84fd73 | 2014-08-26 01:01:59 +0200 | [diff] [blame] | 1004 | With :class:`SelectorEventLoop` event loop, the *pipe* is set to |
| 1005 | non-blocking mode. |
| 1006 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1007 | .. coroutinemethod:: loop.connect_write_pipe(protocol_factory, pipe) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1008 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1009 | Register the write end of *pipe* in the event loop. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1010 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1011 | *protocol_factory* must be a callable returning an |
| 1012 | :ref:`asyncio protocol <asyncio-protocol>` implementation. |
| 1013 | |
| 1014 | *pipe* is :term:`file-like object <file object>`. |
| 1015 | |
Victor Stinner | 2cef300 | 2014-10-23 22:38:46 +0200 | [diff] [blame] | 1016 | Return pair ``(transport, protocol)``, where *transport* supports |
Bumsik Kim | 5cc583d | 2018-09-16 19:40:44 -0400 | [diff] [blame] | 1017 | :class:`WriteTransport` interface and *protocol* is an object |
| 1018 | instantiated by the *protocol_factory*. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1019 | |
Victor Stinner | d84fd73 | 2014-08-26 01:01:59 +0200 | [diff] [blame] | 1020 | With :class:`SelectorEventLoop` event loop, the *pipe* is set to |
| 1021 | non-blocking mode. |
| 1022 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1023 | .. note:: |
| 1024 | |
| 1025 | :class:`SelectorEventLoop` does not support the above methods on |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 1026 | Windows. Use :class:`ProactorEventLoop` instead for Windows. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1027 | |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 1028 | .. seealso:: |
| 1029 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1030 | The :meth:`loop.subprocess_exec` and |
| 1031 | :meth:`loop.subprocess_shell` methods. |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 1032 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1033 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1034 | Unix signals |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1035 | ^^^^^^^^^^^^ |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 1036 | |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 1037 | .. method:: loop.add_signal_handler(signum, callback, *args) |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 1038 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1039 | Set *callback* as the handler for the *signum* signal. |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 1040 | |
Hrvoje Nikšić | e3666fc | 2018-12-18 22:31:29 +0100 | [diff] [blame] | 1041 | The callback will be invoked by *loop*, along with other queued callbacks |
| 1042 | and runnable coroutines of that event loop. Unlike signal handlers |
| 1043 | registered using :func:`signal.signal`, a callback registered with this |
| 1044 | function is allowed to interact with the event loop. |
| 1045 | |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 1046 | Raise :exc:`ValueError` if the signal number is invalid or uncatchable. |
| 1047 | Raise :exc:`RuntimeError` if there is a problem setting up the handler. |
| 1048 | |
Yury Selivanov | e247b46 | 2018-09-20 12:43:59 -0400 | [diff] [blame] | 1049 | Use :func:`functools.partial` :ref:`to pass keyword arguments |
Naglis | 1747334 | 2018-12-04 09:31:15 +0200 | [diff] [blame] | 1050 | <asyncio-pass-keywords>` to *callback*. |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 1051 | |
Hrvoje Nikšić | e3666fc | 2018-12-18 22:31:29 +0100 | [diff] [blame] | 1052 | Like :func:`signal.signal`, this function must be invoked in the main |
| 1053 | thread. |
| 1054 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1055 | .. method:: loop.remove_signal_handler(sig) |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 1056 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1057 | Remove the handler for the *sig* signal. |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 1058 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1059 | Return ``True`` if the signal handler was removed, or ``False`` if |
| 1060 | no handler was set for the given signal. |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 1061 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 1062 | .. availability:: Unix. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1063 | |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 1064 | .. seealso:: |
| 1065 | |
| 1066 | The :mod:`signal` module. |
| 1067 | |
| 1068 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1069 | Executing code in thread or process pools |
| 1070 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1071 | |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 1072 | .. awaitablemethod:: loop.run_in_executor(executor, func, *args) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1073 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1074 | Arrange for *func* to be called in the specified executor. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1075 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1076 | The *executor* argument should be an :class:`concurrent.futures.Executor` |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 1077 | instance. The default executor is used if *executor* is ``None``. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1078 | |
Yury Selivanov | e247b46 | 2018-09-20 12:43:59 -0400 | [diff] [blame] | 1079 | Example:: |
| 1080 | |
| 1081 | import asyncio |
| 1082 | import concurrent.futures |
| 1083 | |
| 1084 | def blocking_io(): |
| 1085 | # File operations (such as logging) can block the |
| 1086 | # event loop: run them in a thread pool. |
| 1087 | with open('/dev/urandom', 'rb') as f: |
| 1088 | return f.read(100) |
| 1089 | |
| 1090 | def cpu_bound(): |
| 1091 | # CPU-bound operations will block the event loop: |
| 1092 | # in general it is preferable to run them in a |
| 1093 | # process pool. |
| 1094 | return sum(i * i for i in range(10 ** 7)) |
| 1095 | |
| 1096 | async def main(): |
| 1097 | loop = asyncio.get_running_loop() |
| 1098 | |
| 1099 | ## Options: |
| 1100 | |
| 1101 | # 1. Run in the default loop's executor: |
| 1102 | result = await loop.run_in_executor( |
| 1103 | None, blocking_io) |
| 1104 | print('default thread pool', result) |
| 1105 | |
| 1106 | # 2. Run in a custom thread pool: |
| 1107 | with concurrent.futures.ThreadPoolExecutor() as pool: |
| 1108 | result = await loop.run_in_executor( |
| 1109 | pool, blocking_io) |
| 1110 | print('custom thread pool', result) |
| 1111 | |
| 1112 | # 3. Run in a custom process pool: |
| 1113 | with concurrent.futures.ProcessPoolExecutor() as pool: |
| 1114 | result = await loop.run_in_executor( |
| 1115 | pool, cpu_bound) |
| 1116 | print('custom process pool', result) |
| 1117 | |
| 1118 | asyncio.run(main()) |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 1119 | |
Yury Selivanov | bec2372 | 2018-01-28 14:09:40 -0500 | [diff] [blame] | 1120 | This method returns a :class:`asyncio.Future` object. |
| 1121 | |
Yury Selivanov | e247b46 | 2018-09-20 12:43:59 -0400 | [diff] [blame] | 1122 | Use :func:`functools.partial` :ref:`to pass keyword arguments |
| 1123 | <asyncio-pass-keywords>` to *func*. |
| 1124 | |
Yury Selivanov | e8a6045 | 2016-10-21 17:40:42 -0400 | [diff] [blame] | 1125 | .. versionchanged:: 3.5.3 |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1126 | :meth:`loop.run_in_executor` no longer configures the |
Yury Selivanov | e8a6045 | 2016-10-21 17:40:42 -0400 | [diff] [blame] | 1127 | ``max_workers`` of the thread pool executor it creates, instead |
| 1128 | leaving it up to the thread pool executor |
| 1129 | (:class:`~concurrent.futures.ThreadPoolExecutor`) to set the |
| 1130 | default. |
| 1131 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1132 | .. method:: loop.set_default_executor(executor) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1133 | |
Elvis Pranskevichus | 22d2508 | 2018-07-30 11:42:43 +0100 | [diff] [blame] | 1134 | Set *executor* as the default executor used by :meth:`run_in_executor`. |
| 1135 | *executor* should be an instance of |
| 1136 | :class:`~concurrent.futures.ThreadPoolExecutor`. |
| 1137 | |
| 1138 | .. deprecated:: 3.8 |
| 1139 | Using an executor that is not an instance of |
| 1140 | :class:`~concurrent.futures.ThreadPoolExecutor` is deprecated and |
| 1141 | will trigger an error in Python 3.9. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1142 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1143 | *executor* must be an instance of |
| 1144 | :class:`concurrent.futures.ThreadPoolExecutor`. |
| 1145 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1146 | |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1147 | Error Handling API |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1148 | ^^^^^^^^^^^^^^^^^^ |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1149 | |
Martin Panter | c04fb56 | 2016-02-10 05:44:01 +0000 | [diff] [blame] | 1150 | Allows customizing how exceptions are handled in the event loop. |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1151 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1152 | .. method:: loop.set_exception_handler(handler) |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1153 | |
| 1154 | Set *handler* as the new event loop exception handler. |
| 1155 | |
| 1156 | If *handler* is ``None``, the default exception handler will |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1157 | be set. Otherwise, *handler* must be a callable with the signature |
| 1158 | matching ``(loop, context)``, where ``loop`` |
| 1159 | is a reference to the active event loop, and ``context`` |
| 1160 | is a ``dict`` object containing the details of the exception |
| 1161 | (see :meth:`call_exception_handler` documentation for details |
| 1162 | about context). |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1163 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1164 | .. method:: loop.get_exception_handler() |
Yury Selivanov | 950204d | 2016-05-16 16:23:00 -0400 | [diff] [blame] | 1165 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1166 | Return the current exception handler, or ``None`` if no custom |
| 1167 | exception handler was set. |
Yury Selivanov | 950204d | 2016-05-16 16:23:00 -0400 | [diff] [blame] | 1168 | |
| 1169 | .. versionadded:: 3.5.2 |
| 1170 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1171 | .. method:: loop.default_exception_handler(context) |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1172 | |
| 1173 | Default exception handler. |
| 1174 | |
| 1175 | This is called when an exception occurs and no exception |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 1176 | handler is set. This can be called by a custom exception |
| 1177 | handler that wants to defer to the default handler behavior. |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1178 | |
| 1179 | *context* parameter has the same meaning as in |
| 1180 | :meth:`call_exception_handler`. |
| 1181 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1182 | .. method:: loop.call_exception_handler(context) |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1183 | |
| 1184 | Call the current event loop exception handler. |
| 1185 | |
| 1186 | *context* is a ``dict`` object containing the following keys |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1187 | (new keys may be introduced in future Python versions): |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1188 | |
| 1189 | * 'message': Error message; |
| 1190 | * 'exception' (optional): Exception object; |
| 1191 | * 'future' (optional): :class:`asyncio.Future` instance; |
Shane Harvey | a1652da | 2020-11-26 05:24:48 -0800 | [diff] [blame] | 1192 | * 'task' (optional): :class:`asyncio.Task` instance; |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1193 | * 'handle' (optional): :class:`asyncio.Handle` instance; |
| 1194 | * 'protocol' (optional): :ref:`Protocol <asyncio-protocol>` instance; |
| 1195 | * 'transport' (optional): :ref:`Transport <asyncio-transport>` instance; |
Shane Harvey | a1652da | 2020-11-26 05:24:48 -0800 | [diff] [blame] | 1196 | * 'socket' (optional): :class:`socket.socket` instance; |
| 1197 | * 'asyncgen' (optional): Asynchronous generator that caused |
| 1198 | the exception. |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1199 | |
| 1200 | .. note:: |
| 1201 | |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 1202 | This method should not be overloaded in subclassed |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1203 | event loops. For custom exception handling, use |
| 1204 | the :meth:`set_exception_handler()` method. |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1205 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1206 | Enabling debug mode |
| 1207 | ^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 1208 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1209 | .. method:: loop.get_debug() |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 1210 | |
Victor Stinner | 7b7120e | 2014-06-23 00:12:14 +0200 | [diff] [blame] | 1211 | Get the debug mode (:class:`bool`) of the event loop. |
| 1212 | |
| 1213 | The default value is ``True`` if the environment variable |
| 1214 | :envvar:`PYTHONASYNCIODEBUG` is set to a non-empty string, ``False`` |
| 1215 | otherwise. |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 1216 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1217 | .. method:: loop.set_debug(enabled: bool) |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 1218 | |
| 1219 | Set the debug mode of the event loop. |
| 1220 | |
Yury Selivanov | 805e27e | 2018-09-14 16:57:11 -0700 | [diff] [blame] | 1221 | .. versionchanged:: 3.7 |
| 1222 | |
Victor Stinner | b9783d2 | 2020-01-24 10:22:18 +0100 | [diff] [blame] | 1223 | The new :ref:`Python Development Mode <devmode>` can now also be used |
Yury Selivanov | 805e27e | 2018-09-14 16:57:11 -0700 | [diff] [blame] | 1224 | to enable the debug mode. |
| 1225 | |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 1226 | .. seealso:: |
| 1227 | |
Victor Stinner | 62511fd | 2014-06-23 00:36:11 +0200 | [diff] [blame] | 1228 | The :ref:`debug mode of asyncio <asyncio-debug-mode>`. |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 1229 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1230 | |
| 1231 | Running Subprocesses |
| 1232 | ^^^^^^^^^^^^^^^^^^^^ |
| 1233 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1234 | Methods described in this subsections are low-level. In regular |
| 1235 | async/await code consider using the high-level |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1236 | :func:`asyncio.create_subprocess_shell` and |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1237 | :func:`asyncio.create_subprocess_exec` convenience functions instead. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1238 | |
| 1239 | .. note:: |
| 1240 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1241 | The default asyncio event loop on **Windows** does not support |
| 1242 | subprocesses. See :ref:`Subprocess Support on Windows |
| 1243 | <asyncio-windows-subprocess>` for details. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1244 | |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 1245 | .. coroutinemethod:: loop.subprocess_exec(protocol_factory, *args, \ |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1246 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, \ |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 1247 | stderr=subprocess.PIPE, **kwargs) |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1248 | |
| 1249 | Create a subprocess from one or more string arguments specified by |
| 1250 | *args*. |
| 1251 | |
| 1252 | *args* must be a list of strings represented by: |
| 1253 | |
| 1254 | * :class:`str`; |
| 1255 | * or :class:`bytes`, encoded to the |
| 1256 | :ref:`filesystem encoding <filesystem-encoding>`. |
| 1257 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1258 | The first string specifies the program executable, |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 1259 | and the remaining strings specify the arguments. Together, string |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1260 | arguments form the ``argv`` of the program. |
| 1261 | |
| 1262 | This is similar to the standard library :class:`subprocess.Popen` |
| 1263 | class called with ``shell=False`` and the list of strings passed as |
| 1264 | the first argument; however, where :class:`~subprocess.Popen` takes |
| 1265 | a single argument which is list of strings, *subprocess_exec* |
| 1266 | takes multiple string arguments. |
| 1267 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1268 | The *protocol_factory* must be a callable returning a subclass of the |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1269 | :class:`asyncio.SubprocessProtocol` class. |
| 1270 | |
| 1271 | Other parameters: |
| 1272 | |
sbstp | f0d4c64 | 2019-05-27 19:51:19 -0400 | [diff] [blame] | 1273 | * *stdin* can be any of these: |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1274 | |
sbstp | f0d4c64 | 2019-05-27 19:51:19 -0400 | [diff] [blame] | 1275 | * a file-like object representing a pipe to be connected to the |
| 1276 | subprocess's standard input stream using |
| 1277 | :meth:`~loop.connect_write_pipe` |
| 1278 | * the :const:`subprocess.PIPE` constant (default) which will create a new |
| 1279 | pipe and connect it, |
| 1280 | * the value ``None`` which will make the subprocess inherit the file |
| 1281 | descriptor from this process |
| 1282 | * the :const:`subprocess.DEVNULL` constant which indicates that the |
| 1283 | special :data:`os.devnull` file will be used |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1284 | |
sbstp | f0d4c64 | 2019-05-27 19:51:19 -0400 | [diff] [blame] | 1285 | * *stdout* can be any of these: |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1286 | |
sbstp | f0d4c64 | 2019-05-27 19:51:19 -0400 | [diff] [blame] | 1287 | * a file-like object representing a pipe to be connected to the |
| 1288 | subprocess's standard output stream using |
| 1289 | :meth:`~loop.connect_write_pipe` |
| 1290 | * the :const:`subprocess.PIPE` constant (default) which will create a new |
| 1291 | pipe and connect it, |
| 1292 | * the value ``None`` which will make the subprocess inherit the file |
| 1293 | descriptor from this process |
| 1294 | * the :const:`subprocess.DEVNULL` constant which indicates that the |
| 1295 | special :data:`os.devnull` file will be used |
| 1296 | |
| 1297 | * *stderr* can be any of these: |
| 1298 | |
| 1299 | * a file-like object representing a pipe to be connected to the |
| 1300 | subprocess's standard error stream using |
| 1301 | :meth:`~loop.connect_write_pipe` |
| 1302 | * the :const:`subprocess.PIPE` constant (default) which will create a new |
| 1303 | pipe and connect it, |
| 1304 | * the value ``None`` which will make the subprocess inherit the file |
| 1305 | descriptor from this process |
| 1306 | * the :const:`subprocess.DEVNULL` constant which indicates that the |
| 1307 | special :data:`os.devnull` file will be used |
| 1308 | * the :const:`subprocess.STDOUT` constant which will connect the standard |
| 1309 | error stream to the process' standard output stream |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1310 | |
| 1311 | * All other keyword arguments are passed to :class:`subprocess.Popen` |
sbstp | f0d4c64 | 2019-05-27 19:51:19 -0400 | [diff] [blame] | 1312 | without interpretation, except for *bufsize*, *universal_newlines*, |
| 1313 | *shell*, *text*, *encoding* and *errors*, which should not be specified |
| 1314 | at all. |
| 1315 | |
| 1316 | The ``asyncio`` subprocess API does not support decoding the streams |
| 1317 | as text. :func:`bytes.decode` can be used to convert the bytes returned |
| 1318 | from the stream to text. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1319 | |
| 1320 | See the constructor of the :class:`subprocess.Popen` class |
| 1321 | for documentation on other arguments. |
| 1322 | |
| 1323 | Returns a pair of ``(transport, protocol)``, where *transport* |
Bumsik Kim | 5cc583d | 2018-09-16 19:40:44 -0400 | [diff] [blame] | 1324 | conforms to the :class:`asyncio.SubprocessTransport` base class and |
| 1325 | *protocol* is an object instantiated by the *protocol_factory*. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1326 | |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 1327 | .. coroutinemethod:: loop.subprocess_shell(protocol_factory, cmd, *, \ |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1328 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, \ |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 1329 | stderr=subprocess.PIPE, **kwargs) |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1330 | |
| 1331 | Create a subprocess from *cmd*, which can be a :class:`str` or a |
| 1332 | :class:`bytes` string encoded to the |
| 1333 | :ref:`filesystem encoding <filesystem-encoding>`, |
| 1334 | using the platform's "shell" syntax. |
| 1335 | |
| 1336 | This is similar to the standard library :class:`subprocess.Popen` |
| 1337 | class called with ``shell=True``. |
| 1338 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1339 | The *protocol_factory* must be a callable returning a subclass of the |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1340 | :class:`SubprocessProtocol` class. |
| 1341 | |
| 1342 | See :meth:`~loop.subprocess_exec` for more details about |
| 1343 | the remaining arguments. |
| 1344 | |
| 1345 | Returns a pair of ``(transport, protocol)``, where *transport* |
Bumsik Kim | 5cc583d | 2018-09-16 19:40:44 -0400 | [diff] [blame] | 1346 | conforms to the :class:`SubprocessTransport` base class and |
| 1347 | *protocol* is an object instantiated by the *protocol_factory*. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1348 | |
| 1349 | .. note:: |
| 1350 | It is the application's responsibility to ensure that all whitespace |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1351 | and special characters are quoted appropriately to avoid `shell injection |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1352 | <https://en.wikipedia.org/wiki/Shell_injection#Shell_injection>`_ |
| 1353 | vulnerabilities. The :func:`shlex.quote` function can be used to |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1354 | properly escape whitespace and special characters in strings that |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1355 | are going to be used to construct shell commands. |
| 1356 | |
| 1357 | |
| 1358 | Callback Handles |
| 1359 | ================ |
| 1360 | |
| 1361 | .. class:: Handle |
| 1362 | |
| 1363 | A callback wrapper object returned by :meth:`loop.call_soon`, |
| 1364 | :meth:`loop.call_soon_threadsafe`. |
| 1365 | |
| 1366 | .. method:: cancel() |
| 1367 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1368 | Cancel the callback. If the callback has already been canceled |
| 1369 | or executed, this method has no effect. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1370 | |
| 1371 | .. method:: cancelled() |
| 1372 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1373 | Return ``True`` if the callback was cancelled. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1374 | |
| 1375 | .. versionadded:: 3.7 |
| 1376 | |
| 1377 | .. class:: TimerHandle |
| 1378 | |
| 1379 | A callback wrapper object returned by :meth:`loop.call_later`, |
| 1380 | and :meth:`loop.call_at`. |
| 1381 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1382 | This class is a subclass of :class:`Handle`. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1383 | |
| 1384 | .. method:: when() |
| 1385 | |
| 1386 | Return a scheduled callback time as :class:`float` seconds. |
| 1387 | |
| 1388 | The time is an absolute timestamp, using the same time |
| 1389 | reference as :meth:`loop.time`. |
| 1390 | |
| 1391 | .. versionadded:: 3.7 |
| 1392 | |
| 1393 | |
| 1394 | Server Objects |
| 1395 | ============== |
| 1396 | |
| 1397 | Server objects are created by :meth:`loop.create_server`, |
| 1398 | :meth:`loop.create_unix_server`, :func:`start_server`, |
| 1399 | and :func:`start_unix_server` functions. |
| 1400 | |
| 1401 | Do not instantiate the class directly. |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 1402 | |
Victor Stinner | 8ebeb03 | 2014-07-11 23:47:40 +0200 | [diff] [blame] | 1403 | .. class:: Server |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 1404 | |
Yury Selivanov | c9070d0 | 2018-01-25 18:08:09 -0500 | [diff] [blame] | 1405 | *Server* objects are asynchronous context managers. When used in an |
| 1406 | ``async with`` statement, it's guaranteed that the Server object is |
| 1407 | closed and not accepting new connections when the ``async with`` |
| 1408 | statement is completed:: |
| 1409 | |
| 1410 | srv = await loop.create_server(...) |
| 1411 | |
| 1412 | async with srv: |
| 1413 | # some code |
| 1414 | |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 1415 | # At this point, srv is closed and no longer accepts new connections. |
Yury Selivanov | c9070d0 | 2018-01-25 18:08:09 -0500 | [diff] [blame] | 1416 | |
| 1417 | |
| 1418 | .. versionchanged:: 3.7 |
| 1419 | Server object is an asynchronous context manager since Python 3.7. |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 1420 | |
| 1421 | .. method:: close() |
| 1422 | |
Victor Stinner | 4bfb14a | 2014-07-12 03:20:24 +0200 | [diff] [blame] | 1423 | Stop serving: close listening sockets and set the :attr:`sockets` |
| 1424 | attribute to ``None``. |
| 1425 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1426 | The sockets that represent existing incoming client connections |
| 1427 | are left open. |
Victor Stinner | 8ebeb03 | 2014-07-11 23:47:40 +0200 | [diff] [blame] | 1428 | |
Berker Peksag | 49c9edf | 2016-01-20 07:14:22 +0200 | [diff] [blame] | 1429 | The server is closed asynchronously, use the :meth:`wait_closed` |
| 1430 | coroutine to wait until the server is closed. |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 1431 | |
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి) | 1634fc2 | 2017-12-30 20:39:32 +0530 | [diff] [blame] | 1432 | .. method:: get_loop() |
| 1433 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1434 | Return the event loop associated with the server object. |
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి) | 1634fc2 | 2017-12-30 20:39:32 +0530 | [diff] [blame] | 1435 | |
| 1436 | .. versionadded:: 3.7 |
| 1437 | |
Yury Selivanov | c9070d0 | 2018-01-25 18:08:09 -0500 | [diff] [blame] | 1438 | .. coroutinemethod:: start_serving() |
| 1439 | |
| 1440 | Start accepting connections. |
| 1441 | |
| 1442 | This method is idempotent, so it can be called when |
| 1443 | the server is already being serving. |
| 1444 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1445 | The *start_serving* keyword-only parameter to |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1446 | :meth:`loop.create_server` and |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1447 | :meth:`asyncio.start_server` allows creating a Server object |
| 1448 | that is not accepting connections initially. In this case |
| 1449 | ``Server.start_serving()``, or :meth:`Server.serve_forever` can be used |
| 1450 | to make the Server start accepting connections. |
Yury Selivanov | c9070d0 | 2018-01-25 18:08:09 -0500 | [diff] [blame] | 1451 | |
| 1452 | .. versionadded:: 3.7 |
| 1453 | |
| 1454 | .. coroutinemethod:: serve_forever() |
| 1455 | |
| 1456 | Start accepting connections until the coroutine is cancelled. |
| 1457 | Cancellation of ``serve_forever`` task causes the server |
| 1458 | to be closed. |
| 1459 | |
| 1460 | This method can be called if the server is already accepting |
| 1461 | connections. Only one ``serve_forever`` task can exist per |
| 1462 | one *Server* object. |
| 1463 | |
| 1464 | Example:: |
| 1465 | |
| 1466 | async def client_connected(reader, writer): |
| 1467 | # Communicate with the client with |
| 1468 | # reader/writer streams. For example: |
| 1469 | await reader.readline() |
| 1470 | |
| 1471 | async def main(host, port): |
| 1472 | srv = await asyncio.start_server( |
| 1473 | client_connected, host, port) |
Andrew Svetlov | 17ab8f0 | 2018-02-17 19:44:35 +0200 | [diff] [blame] | 1474 | await srv.serve_forever() |
Yury Selivanov | c9070d0 | 2018-01-25 18:08:09 -0500 | [diff] [blame] | 1475 | |
| 1476 | asyncio.run(main('127.0.0.1', 0)) |
| 1477 | |
| 1478 | .. versionadded:: 3.7 |
| 1479 | |
| 1480 | .. method:: is_serving() |
| 1481 | |
| 1482 | Return ``True`` if the server is accepting new connections. |
| 1483 | |
| 1484 | .. versionadded:: 3.7 |
| 1485 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 1486 | .. coroutinemethod:: wait_closed() |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 1487 | |
Victor Stinner | 8ebeb03 | 2014-07-11 23:47:40 +0200 | [diff] [blame] | 1488 | Wait until the :meth:`close` method completes. |
| 1489 | |
Victor Stinner | 8ebeb03 | 2014-07-11 23:47:40 +0200 | [diff] [blame] | 1490 | .. attribute:: sockets |
| 1491 | |
Emmanuel Arias | df5cdc1 | 2019-02-22 14:34:41 -0300 | [diff] [blame] | 1492 | List of :class:`socket.socket` objects the server is listening on. |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 1493 | |
Yury Selivanov | c9070d0 | 2018-01-25 18:08:09 -0500 | [diff] [blame] | 1494 | .. versionchanged:: 3.7 |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1495 | Prior to Python 3.7 ``Server.sockets`` used to return an |
| 1496 | internal list of server sockets directly. In 3.7 a copy |
Yury Selivanov | c9070d0 | 2018-01-25 18:08:09 -0500 | [diff] [blame] | 1497 | of that list is returned. |
| 1498 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 1499 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1500 | .. _asyncio-event-loops: |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1501 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1502 | Event Loop Implementations |
| 1503 | ========================== |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1504 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1505 | asyncio ships with two different event loop implementations: |
| 1506 | :class:`SelectorEventLoop` and :class:`ProactorEventLoop`. |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1507 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1508 | By default asyncio is configured to use :class:`SelectorEventLoop` |
Ben Darnell | 9ffca67 | 2019-06-22 13:38:21 -0400 | [diff] [blame] | 1509 | on Unix and :class:`ProactorEventLoop` on Windows. |
Andrew Svetlov | 3d4dbd8 | 2018-02-01 19:59:32 +0200 | [diff] [blame] | 1510 | |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1511 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1512 | .. class:: SelectorEventLoop |
| 1513 | |
| 1514 | An event loop based on the :mod:`selectors` module. |
| 1515 | |
| 1516 | Uses the most efficient *selector* available for the given |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1517 | platform. It is also possible to manually configure the |
| 1518 | exact selector implementation to be used:: |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1519 | |
| 1520 | import asyncio |
| 1521 | import selectors |
| 1522 | |
| 1523 | selector = selectors.SelectSelector() |
| 1524 | loop = asyncio.SelectorEventLoop(selector) |
| 1525 | asyncio.set_event_loop(loop) |
Andrew Svetlov | 7464e87 | 2018-01-19 20:04:29 +0200 | [diff] [blame] | 1526 | |
| 1527 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 1528 | .. availability:: Unix, Windows. |
Andrew Svetlov | 7464e87 | 2018-01-19 20:04:29 +0200 | [diff] [blame] | 1529 | |
| 1530 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1531 | .. class:: ProactorEventLoop |
| 1532 | |
| 1533 | An event loop for Windows that uses "I/O Completion Ports" (IOCP). |
| 1534 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 1535 | .. availability:: Windows. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1536 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1537 | .. seealso:: |
| 1538 | |
| 1539 | `MSDN documentation on I/O Completion Ports |
| 1540 | <https://docs.microsoft.com/en-ca/windows/desktop/FileIO/i-o-completion-ports>`_. |
| 1541 | |
| 1542 | |
| 1543 | .. class:: AbstractEventLoop |
| 1544 | |
| 1545 | Abstract base class for asyncio-compliant event loops. |
| 1546 | |
| 1547 | The :ref:`Event Loop Methods <asyncio-event-loop>` section lists all |
| 1548 | methods that an alternative implementation of ``AbstractEventLoop`` |
| 1549 | should have defined. |
| 1550 | |
| 1551 | |
| 1552 | Examples |
| 1553 | ======== |
| 1554 | |
| 1555 | Note that all examples in this section **purposefully** show how |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1556 | to use the low-level event loop APIs, such as :meth:`loop.run_forever` |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1557 | and :meth:`loop.call_soon`. Modern asyncio applications rarely |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1558 | need to be written this way; consider using the high-level functions |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1559 | like :func:`asyncio.run`. |
| 1560 | |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 1561 | |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 1562 | .. _asyncio_example_lowlevel_helloworld: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1563 | |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 1564 | Hello World with call_soon() |
Victor Stinner | a092a61 | 2015-01-09 15:58:41 +0100 | [diff] [blame] | 1565 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1566 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1567 | An example using the :meth:`loop.call_soon` method to schedule a |
| 1568 | callback. The callback displays ``"Hello World"`` and then stops the |
| 1569 | event loop:: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1570 | |
| 1571 | import asyncio |
| 1572 | |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 1573 | def hello_world(loop): |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 1574 | """A callback to print 'Hello World' and stop the event loop""" |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1575 | print('Hello World') |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 1576 | loop.stop() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1577 | |
| 1578 | loop = asyncio.get_event_loop() |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 1579 | |
| 1580 | # Schedule a call to hello_world() |
| 1581 | loop.call_soon(hello_world, loop) |
| 1582 | |
| 1583 | # Blocking call interrupted by loop.stop() |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1584 | try: |
| 1585 | loop.run_forever() |
| 1586 | finally: |
| 1587 | loop.close() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1588 | |
Victor Stinner | 3e09e32 | 2013-12-03 01:22:06 +0100 | [diff] [blame] | 1589 | .. seealso:: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1590 | |
Yury Selivanov | 3faaa88 | 2018-09-14 13:32:07 -0700 | [diff] [blame] | 1591 | A similar :ref:`Hello World <coroutine>` |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1592 | example created with a coroutine and the :func:`run` function. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1593 | |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 1594 | |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 1595 | .. _asyncio_example_call_later: |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 1596 | |
| 1597 | Display the current date with call_later() |
Victor Stinner | a092a61 | 2015-01-09 15:58:41 +0100 | [diff] [blame] | 1598 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 1599 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1600 | An example of a callback displaying the current date every second. The |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1601 | callback uses the :meth:`loop.call_later` method to reschedule itself |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1602 | after 5 seconds, and then stops the event loop:: |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 1603 | |
| 1604 | import asyncio |
| 1605 | import datetime |
| 1606 | |
| 1607 | def display_date(end_time, loop): |
| 1608 | print(datetime.datetime.now()) |
| 1609 | if (loop.time() + 1.0) < end_time: |
| 1610 | loop.call_later(1, display_date, end_time, loop) |
| 1611 | else: |
| 1612 | loop.stop() |
| 1613 | |
| 1614 | loop = asyncio.get_event_loop() |
| 1615 | |
| 1616 | # Schedule the first call to display_date() |
| 1617 | end_time = loop.time() + 5.0 |
| 1618 | loop.call_soon(display_date, end_time, loop) |
| 1619 | |
| 1620 | # Blocking call interrupted by loop.stop() |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1621 | try: |
| 1622 | loop.run_forever() |
| 1623 | finally: |
| 1624 | loop.close() |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 1625 | |
| 1626 | .. seealso:: |
| 1627 | |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 1628 | A similar :ref:`current date <asyncio_example_sleep>` example |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1629 | created with a coroutine and the :func:`run` function. |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 1630 | |
| 1631 | |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 1632 | .. _asyncio_example_watch_fd: |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 1633 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 1634 | Watch a file descriptor for read events |
Victor Stinner | a092a61 | 2015-01-09 15:58:41 +0100 | [diff] [blame] | 1635 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 1636 | |
| 1637 | Wait until a file descriptor received some data using the |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1638 | :meth:`loop.add_reader` method and then close the event loop:: |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 1639 | |
| 1640 | import asyncio |
Victor Stinner | ac577d7 | 2017-11-28 21:33:20 +0100 | [diff] [blame] | 1641 | from socket import socketpair |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 1642 | |
| 1643 | # Create a pair of connected file descriptors |
Victor Stinner | ccd8e34 | 2014-10-11 16:30:02 +0200 | [diff] [blame] | 1644 | rsock, wsock = socketpair() |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 1645 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 1646 | loop = asyncio.get_event_loop() |
| 1647 | |
| 1648 | def reader(): |
| 1649 | data = rsock.recv(100) |
| 1650 | print("Received:", data.decode()) |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1651 | |
Victor Stinner | 2cef300 | 2014-10-23 22:38:46 +0200 | [diff] [blame] | 1652 | # We are done: unregister the file descriptor |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 1653 | loop.remove_reader(rsock) |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1654 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 1655 | # Stop the event loop |
| 1656 | loop.stop() |
| 1657 | |
Victor Stinner | 2cef300 | 2014-10-23 22:38:46 +0200 | [diff] [blame] | 1658 | # Register the file descriptor for read event |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 1659 | loop.add_reader(rsock, reader) |
| 1660 | |
| 1661 | # Simulate the reception of data from the network |
| 1662 | loop.call_soon(wsock.send, 'abc'.encode()) |
| 1663 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1664 | try: |
| 1665 | # Run the event loop |
| 1666 | loop.run_forever() |
| 1667 | finally: |
Carol Willing | 5b7cbd6 | 2018-09-12 17:05:17 -0700 | [diff] [blame] | 1668 | # We are done. Close sockets and the event loop. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1669 | rsock.close() |
| 1670 | wsock.close() |
| 1671 | loop.close() |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 1672 | |
| 1673 | .. seealso:: |
| 1674 | |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 1675 | * A similar :ref:`example <asyncio_example_create_connection>` |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1676 | using transports, protocols, and the |
| 1677 | :meth:`loop.create_connection` method. |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 1678 | |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 1679 | * Another similar :ref:`example <asyncio_example_create_connection-streams>` |
Yury Selivanov | 6758e6e | 2019-09-29 21:59:55 -0700 | [diff] [blame] | 1680 | using the high-level :func:`asyncio.open_connection` function |
| 1681 | and streams. |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 1682 | |
| 1683 | |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 1684 | .. _asyncio_example_unix_signals: |
| 1685 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 1686 | Set signal handlers for SIGINT and SIGTERM |
Victor Stinner | a092a61 | 2015-01-09 15:58:41 +0100 | [diff] [blame] | 1687 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 1688 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 1689 | (This ``signals`` example only works on Unix.) |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1690 | |
| 1691 | Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM` |
| 1692 | using the :meth:`loop.add_signal_handler` method:: |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 1693 | |
| 1694 | import asyncio |
| 1695 | import functools |
| 1696 | import os |
| 1697 | import signal |
| 1698 | |
Alexander Vasin | ceb842e | 2019-05-03 18:25:36 +0300 | [diff] [blame] | 1699 | def ask_exit(signame, loop): |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 1700 | print("got signal %s: exit" % signame) |
| 1701 | loop.stop() |
| 1702 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1703 | async def main(): |
| 1704 | loop = asyncio.get_running_loop() |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 1705 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1706 | for signame in {'SIGINT', 'SIGTERM'}: |
| 1707 | loop.add_signal_handler( |
| 1708 | getattr(signal, signame), |
Alexander Vasin | ceb842e | 2019-05-03 18:25:36 +0300 | [diff] [blame] | 1709 | functools.partial(ask_exit, signame, loop)) |
Victor Stinner | 2cef300 | 2014-10-23 22:38:46 +0200 | [diff] [blame] | 1710 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1711 | await asyncio.sleep(3600) |
| 1712 | |
| 1713 | print("Event loop running for 1 hour, press Ctrl+C to interrupt.") |
| 1714 | print(f"pid {os.getpid()}: send SIGINT or SIGTERM to exit.") |
| 1715 | |
| 1716 | asyncio.run(main()) |