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