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