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