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