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