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