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 | |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 3 | .. _asyncio-event-loop: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 4 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 5 | Base Event Loop |
| 6 | =============== |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 7 | |
| 8 | The event loop is the central execution device provided by :mod:`asyncio`. |
Zachary Ware | 5e580da | 2015-08-27 15:54:39 -0500 | [diff] [blame] | 9 | It provides multiple facilities, including: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 10 | |
Eli Bendersky | b73c833 | 2014-02-09 06:07:47 -0800 | [diff] [blame] | 11 | * Registering, executing and cancelling delayed calls (timeouts). |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 12 | |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 13 | * Creating client and server :ref:`transports <asyncio-transport>` for various |
Eli Bendersky | b73c833 | 2014-02-09 06:07:47 -0800 | [diff] [blame] | 14 | kinds of communication. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 15 | |
Eli Bendersky | 136fea2 | 2014-02-09 06:55:58 -0800 | [diff] [blame] | 16 | * Launching subprocesses and the associated :ref:`transports |
| 17 | <asyncio-transport>` for communication with an external program. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 18 | |
Eli Bendersky | b73c833 | 2014-02-09 06:07:47 -0800 | [diff] [blame] | 19 | * Delegating costly function calls to a pool of threads. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 20 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 21 | .. class:: BaseEventLoop |
Eli Bendersky | 136fea2 | 2014-02-09 06:55:58 -0800 | [diff] [blame] | 22 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 23 | Base class of event loops. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 24 | |
Victor Stinner | 8370496 | 2015-02-25 14:24:15 +0100 | [diff] [blame] | 25 | This class is :ref:`not thread safe <asyncio-multithreading>`. |
| 26 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 27 | Run an event loop |
| 28 | ----------------- |
| 29 | |
| 30 | .. method:: BaseEventLoop.run_forever() |
| 31 | |
Guido van Rossum | 41f69f4 | 2015-11-19 13:28:47 -0800 | [diff] [blame] | 32 | Run until :meth:`stop` is called. If :meth:`stop` is called before |
| 33 | :meth:`run_forever()` is called, this polls the I/O selector once |
| 34 | with a timeout of zero, runs all callbacks scheduled in response to |
| 35 | I/O events (and those that were already scheduled), and then exits. |
| 36 | If :meth:`stop` is called while :meth:`run_forever` is running, |
| 37 | this will run the current batch of callbacks and then exit. Note |
| 38 | that callbacks scheduled by callbacks will not run in that case; |
| 39 | they will run the next time :meth:`run_forever` is called. |
| 40 | |
Guido van Rossum | 82f9fea | 2015-11-19 13:33:34 -0800 | [diff] [blame] | 41 | .. versionchanged:: 3.5.1 |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 42 | |
| 43 | .. method:: BaseEventLoop.run_until_complete(future) |
| 44 | |
Victor Stinner | 99c2ab4 | 2013-12-03 19:17:25 +0100 | [diff] [blame] | 45 | Run until the :class:`Future` is done. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 46 | |
Victor Stinner | 530ef2f | 2014-07-08 12:39:10 +0200 | [diff] [blame] | 47 | If the argument is a :ref:`coroutine object <coroutine>`, it is wrapped by |
Yury Selivanov | 04356e1 | 2015-06-30 22:13:22 -0400 | [diff] [blame] | 48 | :func:`ensure_future`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 49 | |
| 50 | Return the Future's result, or raise its exception. |
| 51 | |
| 52 | .. method:: BaseEventLoop.is_running() |
| 53 | |
| 54 | Returns running status of event loop. |
| 55 | |
Victor Stinner | afbf827 | 2013-12-03 02:05:42 +0100 | [diff] [blame] | 56 | .. method:: BaseEventLoop.stop() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 57 | |
| 58 | Stop running the event loop. |
| 59 | |
Guido van Rossum | 41f69f4 | 2015-11-19 13:28:47 -0800 | [diff] [blame] | 60 | This causes :meth:`run_forever` to exit at the next suitable |
| 61 | opportunity (see there for more details). |
| 62 | |
Guido van Rossum | 82f9fea | 2015-11-19 13:33:34 -0800 | [diff] [blame] | 63 | .. versionchanged:: 3.5.1 |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 64 | |
Victor Stinner | bb2fc5b | 2014-06-10 10:23:10 +0200 | [diff] [blame] | 65 | .. method:: BaseEventLoop.is_closed() |
| 66 | |
| 67 | Returns ``True`` if the event loop was closed. |
| 68 | |
| 69 | .. versionadded:: 3.4.2 |
| 70 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 71 | .. method:: BaseEventLoop.close() |
| 72 | |
Guido van Rossum | 41f69f4 | 2015-11-19 13:28:47 -0800 | [diff] [blame] | 73 | Close the event loop. The loop must not be running. Pending |
| 74 | callbacks will be lost. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 75 | |
| 76 | This clears the queues and shuts down the executor, but does not wait for |
| 77 | the executor to finish. |
| 78 | |
| 79 | This is idempotent and irreversible. No other methods should be called after |
| 80 | this one. |
| 81 | |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 82 | .. _asyncio-pass-keywords: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 83 | |
| 84 | Calls |
| 85 | ----- |
| 86 | |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 87 | Most :mod:`asyncio` functions don't accept keywords. If you want to pass |
| 88 | keywords to your callback, use :func:`functools.partial`. For example, |
| 89 | ``loop.call_soon(functools.partial(print, "Hello", flush=True))`` will call |
| 90 | ``print("Hello", flush=True)``. |
| 91 | |
| 92 | .. note:: |
| 93 | :func:`functools.partial` is better than ``lambda`` functions, because |
| 94 | :mod:`asyncio` can inspect :func:`functools.partial` object to display |
| 95 | parameters in debug mode, whereas ``lambda`` functions have a poor |
| 96 | representation. |
| 97 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 98 | .. method:: BaseEventLoop.call_soon(callback, \*args) |
| 99 | |
Victor Stinner | 4d5115c | 2014-12-15 17:50:55 +0100 | [diff] [blame] | 100 | Arrange for a callback to be called as soon as possible. The callback is |
| 101 | called after :meth:`call_soon` returns, when control returns to the event |
| 102 | loop. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 103 | |
| 104 | This operates as a FIFO queue, callbacks are called in the order in |
| 105 | which they are registered. Each callback will be called exactly once. |
| 106 | |
| 107 | Any positional arguments after the callback will be passed to the |
| 108 | callback when it is called. |
| 109 | |
Yury Selivanov | 1096f76 | 2015-06-25 13:49:52 -0400 | [diff] [blame] | 110 | An instance of :class:`asyncio.Handle` is returned, which can be |
| 111 | used to cancel the callback. |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 112 | |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 113 | :ref:`Use functools.partial to pass keywords to the callback |
| 114 | <asyncio-pass-keywords>`. |
| 115 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 116 | .. method:: BaseEventLoop.call_soon_threadsafe(callback, \*args) |
| 117 | |
| 118 | Like :meth:`call_soon`, but thread safe. |
| 119 | |
Victor Stinner | 8370496 | 2015-02-25 14:24:15 +0100 | [diff] [blame] | 120 | See the :ref:`concurrency and multithreading <asyncio-multithreading>` |
| 121 | section of the documentation. |
| 122 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 123 | |
Victor Stinner | 45b27ed | 2014-02-01 02:36:43 +0100 | [diff] [blame] | 124 | .. _asyncio-delayed-calls: |
| 125 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 126 | Delayed calls |
| 127 | ------------- |
| 128 | |
| 129 | The event loop has its own internal clock for computing timeouts. |
| 130 | Which clock is used depends on the (platform-specific) event loop |
| 131 | implementation; ideally it is a monotonic clock. This will generally be |
| 132 | a different clock than :func:`time.time`. |
| 133 | |
Victor Stinner | fd9d374 | 2014-02-18 09:37:43 +0100 | [diff] [blame] | 134 | .. note:: |
| 135 | |
| 136 | Timeouts (relative *delay* or absolute *when*) should not exceed one day. |
| 137 | |
Victor Stinner | 45b27ed | 2014-02-01 02:36:43 +0100 | [diff] [blame] | 138 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 139 | .. method:: BaseEventLoop.call_later(delay, callback, *args) |
| 140 | |
| 141 | Arrange for the *callback* to be called after the given *delay* |
| 142 | seconds (either an int or float). |
| 143 | |
Yury Selivanov | 1096f76 | 2015-06-25 13:49:52 -0400 | [diff] [blame] | 144 | An instance of :class:`asyncio.Handle` is returned, which can be |
| 145 | used to cancel the callback. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 146 | |
| 147 | *callback* will be called exactly once per call to :meth:`call_later`. |
| 148 | If two callbacks are scheduled for exactly the same time, it is |
| 149 | undefined which will be called first. |
| 150 | |
| 151 | The optional positional *args* will be passed to the callback when it |
| 152 | is called. If you want the callback to be called with some named |
| 153 | arguments, use a closure or :func:`functools.partial`. |
| 154 | |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 155 | :ref:`Use functools.partial to pass keywords to the callback |
| 156 | <asyncio-pass-keywords>`. |
| 157 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 158 | .. method:: BaseEventLoop.call_at(when, callback, *args) |
| 159 | |
| 160 | Arrange for the *callback* to be called at the given absolute timestamp |
Berker Peksag | b556399 | 2014-11-07 19:51:07 +0200 | [diff] [blame] | 161 | *when* (an int or float), using the same time reference as |
| 162 | :meth:`BaseEventLoop.time`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 163 | |
| 164 | This method's behavior is the same as :meth:`call_later`. |
| 165 | |
Yury Selivanov | 1096f76 | 2015-06-25 13:49:52 -0400 | [diff] [blame] | 166 | An instance of :class:`asyncio.Handle` is returned, which can be |
| 167 | used to cancel the callback. |
| 168 | |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 169 | :ref:`Use functools.partial to pass keywords to the callback |
| 170 | <asyncio-pass-keywords>`. |
| 171 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 172 | .. method:: BaseEventLoop.time() |
| 173 | |
| 174 | Return the current time, as a :class:`float` value, according to the |
| 175 | event loop's internal clock. |
| 176 | |
Victor Stinner | 3e09e32 | 2013-12-03 01:22:06 +0100 | [diff] [blame] | 177 | .. seealso:: |
| 178 | |
| 179 | The :func:`asyncio.sleep` function. |
| 180 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 181 | |
Yury Selivanov | 950204d | 2016-05-16 16:23:00 -0400 | [diff] [blame] | 182 | Futures |
| 183 | ------- |
| 184 | |
| 185 | .. method:: BaseEventLoop.create_future() |
| 186 | |
| 187 | Create an :class:`asyncio.Future` object attached to the loop. |
| 188 | |
| 189 | This is a preferred way to create futures in asyncio, as event |
| 190 | loop implementations can provide alternative implementations |
| 191 | of the Future class (with better performance or instrumentation). |
| 192 | |
| 193 | .. versionadded:: 3.5.2 |
| 194 | |
| 195 | |
Yury Selivanov | bb96134 | 2015-06-25 11:54:34 -0400 | [diff] [blame] | 196 | Tasks |
| 197 | ----- |
Victor Stinner | 530ef2f | 2014-07-08 12:39:10 +0200 | [diff] [blame] | 198 | |
| 199 | .. method:: BaseEventLoop.create_task(coro) |
| 200 | |
| 201 | Schedule the execution of a :ref:`coroutine object <coroutine>`: wrap it in |
| 202 | a future. Return a :class:`Task` object. |
| 203 | |
| 204 | Third-party event loops can use their own subclass of :class:`Task` for |
| 205 | interoperability. In this case, the result type is a subclass of |
| 206 | :class:`Task`. |
| 207 | |
Victor Stinner | 337e03f | 2014-08-11 01:11:13 +0200 | [diff] [blame] | 208 | This method was added in Python 3.4.2. Use the :func:`async` function to |
| 209 | support also older Python versions. |
Victor Stinner | 530ef2f | 2014-07-08 12:39:10 +0200 | [diff] [blame] | 210 | |
| 211 | .. versionadded:: 3.4.2 |
| 212 | |
Yury Selivanov | 7185461 | 2015-05-11 16:28:27 -0400 | [diff] [blame] | 213 | .. method:: BaseEventLoop.set_task_factory(factory) |
| 214 | |
| 215 | Set a task factory that will be used by |
| 216 | :meth:`BaseEventLoop.create_task`. |
| 217 | |
| 218 | If *factory* is ``None`` the default task factory will be set. |
| 219 | |
| 220 | If *factory* is a *callable*, it should have a signature matching |
| 221 | ``(loop, coro)``, where *loop* will be a reference to the active |
| 222 | event loop, *coro* will be a coroutine object. The callable |
| 223 | must return an :class:`asyncio.Future` compatible object. |
| 224 | |
| 225 | .. versionadded:: 3.4.4 |
| 226 | |
| 227 | .. method:: BaseEventLoop.get_task_factory() |
| 228 | |
| 229 | Return a task factory, or ``None`` if the default one is in use. |
| 230 | |
| 231 | .. versionadded:: 3.4.4 |
| 232 | |
Victor Stinner | 530ef2f | 2014-07-08 12:39:10 +0200 | [diff] [blame] | 233 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 234 | Creating connections |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 235 | -------------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 236 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 237 | .. coroutinemethod:: BaseEventLoop.create_connection(protocol_factory, host=None, port=None, \*, ssl=None, family=0, proto=0, flags=0, sock=None, local_addr=None, server_hostname=None) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 238 | |
| 239 | Create a streaming transport connection to a given Internet *host* and |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 240 | *port*: socket family :py:data:`~socket.AF_INET` or |
| 241 | :py:data:`~socket.AF_INET6` depending on *host* (or *family* if specified), |
| 242 | socket type :py:data:`~socket.SOCK_STREAM`. *protocol_factory* must be a |
| 243 | callable returning a :ref:`protocol <asyncio-protocol>` instance. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 244 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 245 | This method is a :ref:`coroutine <coroutine>` which will try to |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 246 | establish the connection in the background. When successful, the |
| 247 | coroutine returns a ``(transport, protocol)`` pair. |
| 248 | |
| 249 | The chronological synopsis of the underlying operation is as follows: |
| 250 | |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 251 | #. The connection is established, and a :ref:`transport <asyncio-transport>` |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 252 | is created to represent it. |
| 253 | |
| 254 | #. *protocol_factory* is called without arguments and must return a |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 255 | :ref:`protocol <asyncio-protocol>` instance. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 256 | |
| 257 | #. The protocol instance is tied to the transport, and its |
| 258 | :meth:`connection_made` method is called. |
| 259 | |
| 260 | #. The coroutine returns successfully with the ``(transport, protocol)`` |
| 261 | pair. |
| 262 | |
| 263 | The created transport is an implementation-dependent bidirectional stream. |
| 264 | |
| 265 | .. note:: |
| 266 | *protocol_factory* can be any kind of callable, not necessarily |
| 267 | a class. For example, if you want to use a pre-created |
| 268 | protocol instance, you can pass ``lambda: my_protocol``. |
| 269 | |
Martin Panter | c04fb56 | 2016-02-10 05:44:01 +0000 | [diff] [blame] | 270 | Options that change how the connection is created: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 271 | |
| 272 | * *ssl*: if given and not false, a SSL/TLS transport is created |
| 273 | (by default a plain TCP transport is created). If *ssl* is |
| 274 | a :class:`ssl.SSLContext` object, this context is used to create |
| 275 | the transport; if *ssl* is :const:`True`, a context with some |
| 276 | unspecified default settings is used. |
| 277 | |
Berker Peksag | 9c1dba2 | 2014-09-28 00:00:58 +0300 | [diff] [blame] | 278 | .. seealso:: :ref:`SSL/TLS security considerations <ssl-security>` |
Antoine Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 279 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 280 | * *server_hostname*, is only for use together with *ssl*, |
| 281 | and sets or overrides the hostname that the target server's certificate |
| 282 | will be matched against. By default the value of the *host* argument |
| 283 | is used. If *host* is empty, there is no default and you must pass a |
| 284 | value for *server_hostname*. If *server_hostname* is an empty |
| 285 | string, hostname matching is disabled (which is a serious security |
| 286 | risk, allowing for man-in-the-middle-attacks). |
| 287 | |
| 288 | * *family*, *proto*, *flags* are the optional address family, protocol |
| 289 | and flags to be passed through to getaddrinfo() for *host* resolution. |
| 290 | If given, these should all be integers from the corresponding |
| 291 | :mod:`socket` module constants. |
| 292 | |
| 293 | * *sock*, if given, should be an existing, already connected |
| 294 | :class:`socket.socket` object to be used by the transport. |
| 295 | If *sock* is given, none of *host*, *port*, *family*, *proto*, *flags* |
| 296 | and *local_addr* should be specified. |
| 297 | |
| 298 | * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used |
| 299 | to bind the socket to locally. The *local_host* and *local_port* |
| 300 | are looked up using getaddrinfo(), similarly to *host* and *port*. |
| 301 | |
Victor Stinner | 60208a1 | 2015-09-15 22:41:52 +0200 | [diff] [blame] | 302 | .. versionchanged:: 3.5 |
| 303 | |
| 304 | On Windows with :class:`ProactorEventLoop`, SSL/TLS is now supported. |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 305 | |
Victor Stinner | c8ea813 | 2014-01-23 11:02:09 +0100 | [diff] [blame] | 306 | .. seealso:: |
| 307 | |
| 308 | The :func:`open_connection` function can be used to get a pair of |
| 309 | (:class:`StreamReader`, :class:`StreamWriter`) instead of a protocol. |
| 310 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 311 | |
Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 312 | .. coroutinemethod:: BaseEventLoop.create_datagram_endpoint(protocol_factory, local_addr=None, remote_addr=None, \*, family=0, proto=0, flags=0, reuse_address=None, reuse_port=None, allow_broadcast=None, sock=None) |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 313 | |
| 314 | Create datagram connection: socket family :py:data:`~socket.AF_INET` or |
| 315 | :py:data:`~socket.AF_INET6` depending on *host* (or *family* if specified), |
Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 316 | socket type :py:data:`~socket.SOCK_DGRAM`. *protocol_factory* must be a |
| 317 | callable returning a :ref:`protocol <asyncio-protocol>` instance. |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 318 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 319 | This method is a :ref:`coroutine <coroutine>` which will try to |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 320 | establish the connection in the background. When successful, the |
| 321 | coroutine returns a ``(transport, protocol)`` pair. |
| 322 | |
Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 323 | Options changing how the connection is created: |
| 324 | |
| 325 | * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used |
| 326 | to bind the socket to locally. The *local_host* and *local_port* |
| 327 | are looked up using :meth:`getaddrinfo`. |
| 328 | |
| 329 | * *remote_addr*, if given, is a ``(remote_host, remote_port)`` tuple used |
| 330 | to connect the socket to a remote address. The *remote_host* and |
| 331 | *remote_port* are looked up using :meth:`getaddrinfo`. |
| 332 | |
| 333 | * *family*, *proto*, *flags* are the optional address family, protocol |
| 334 | and flags to be passed through to :meth:`getaddrinfo` for *host* |
| 335 | resolution. If given, these should all be integers from the |
| 336 | corresponding :mod:`socket` module constants. |
| 337 | |
| 338 | * *reuse_address* tells the kernel to reuse a local socket in |
| 339 | TIME_WAIT state, without waiting for its natural timeout to |
| 340 | expire. If not specified will automatically be set to True on |
| 341 | UNIX. |
| 342 | |
| 343 | * *reuse_port* tells the kernel to allow this endpoint to be bound to the |
| 344 | same port as other existing endpoints are bound to, so long as they all |
| 345 | set this flag when being created. This option is not supported on Windows |
| 346 | and some UNIX's. If the :py:data:`~socket.SO_REUSEPORT` constant is not |
| 347 | defined then this capability is unsupported. |
| 348 | |
| 349 | * *allow_broadcast* tells the kernel to allow this endpoint to send |
| 350 | messages to the broadcast address. |
| 351 | |
| 352 | * *sock* can optionally be specified in order to use a preexisting, |
| 353 | already connected, :class:`socket.socket` object to be used by the |
| 354 | transport. If specified, *local_addr* and *remote_addr* should be omitted |
| 355 | (must be :const:`None`). |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 356 | |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 357 | On Windows with :class:`ProactorEventLoop`, this method is not supported. |
| 358 | |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 359 | See :ref:`UDP echo client protocol <asyncio-udp-echo-client-protocol>` and |
| 360 | :ref:`UDP echo server protocol <asyncio-udp-echo-server-protocol>` examples. |
| 361 | |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 362 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 363 | .. coroutinemethod:: BaseEventLoop.create_unix_connection(protocol_factory, path, \*, ssl=None, sock=None, server_hostname=None) |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 364 | |
| 365 | Create UNIX connection: socket family :py:data:`~socket.AF_UNIX`, socket |
| 366 | type :py:data:`~socket.SOCK_STREAM`. The :py:data:`~socket.AF_UNIX` socket |
| 367 | family is used to communicate between processes on the same machine |
| 368 | efficiently. |
| 369 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 370 | This method is a :ref:`coroutine <coroutine>` which will try to |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 371 | establish the connection in the background. When successful, the |
| 372 | coroutine returns a ``(transport, protocol)`` pair. |
| 373 | |
| 374 | See the :meth:`BaseEventLoop.create_connection` method for parameters. |
| 375 | |
| 376 | Availability: UNIX. |
| 377 | |
| 378 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 379 | Creating listening connections |
| 380 | ------------------------------ |
| 381 | |
Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 382 | .. coroutinemethod:: BaseEventLoop.create_server(protocol_factory, host=None, port=None, \*, family=socket.AF_UNSPEC, flags=socket.AI_PASSIVE, sock=None, backlog=100, ssl=None, reuse_address=None, reuse_port=None) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 383 | |
Victor Stinner | 33f6abe | 2014-10-12 20:36:04 +0200 | [diff] [blame] | 384 | Create a TCP server (socket type :data:`~socket.SOCK_STREAM`) bound to |
| 385 | *host* and *port*. |
| 386 | |
| 387 | Return a :class:`Server` object, its :attr:`~Server.sockets` attribute |
| 388 | contains created sockets. Use the :meth:`Server.close` method to stop the |
| 389 | server: close listening sockets. |
| 390 | |
| 391 | Parameters: |
| 392 | |
Victor Stinner | 5e4a7d8 | 2015-09-21 18:33:43 +0200 | [diff] [blame] | 393 | * The *host* parameter can be a string, in that case the TCP server is |
| 394 | bound to *host* and *port*. The *host* parameter can also be a sequence |
| 395 | of strings and in that case the TCP server is bound to all hosts of the |
| 396 | sequence. If *host* is an empty string or ``None``, all interfaces are |
| 397 | assumed and a list of multiple sockets will be returned (most likely one |
| 398 | for IPv4 and another one for IPv6). |
Victor Stinner | 33f6abe | 2014-10-12 20:36:04 +0200 | [diff] [blame] | 399 | |
| 400 | * *family* can be set to either :data:`socket.AF_INET` or |
| 401 | :data:`~socket.AF_INET6` to force the socket to use IPv4 or IPv6. If not set |
| 402 | it will be determined from host (defaults to :data:`socket.AF_UNSPEC`). |
| 403 | |
| 404 | * *flags* is a bitmask for :meth:`getaddrinfo`. |
| 405 | |
| 406 | * *sock* can optionally be specified in order to use a preexisting |
| 407 | socket object. If specified, *host* and *port* should be omitted (must be |
| 408 | :const:`None`). |
| 409 | |
| 410 | * *backlog* is the maximum number of queued connections passed to |
| 411 | :meth:`~socket.socket.listen` (defaults to 100). |
| 412 | |
| 413 | * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over the |
| 414 | accepted connections. |
| 415 | |
| 416 | * *reuse_address* tells the kernel to reuse a local socket in |
| 417 | TIME_WAIT state, without waiting for its natural timeout to |
| 418 | expire. If not specified will automatically be set to True on |
| 419 | UNIX. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 420 | |
Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 421 | * *reuse_port* tells the kernel to allow this endpoint to be bound to the |
| 422 | same port as other existing endpoints are bound to, so long as they all |
| 423 | set this flag when being created. This option is not supported on |
| 424 | Windows. |
| 425 | |
Victor Stinner | d143209 | 2014-06-19 17:11:49 +0200 | [diff] [blame] | 426 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 427 | |
Victor Stinner | 60208a1 | 2015-09-15 22:41:52 +0200 | [diff] [blame] | 428 | .. versionchanged:: 3.5 |
| 429 | |
| 430 | On Windows with :class:`ProactorEventLoop`, SSL/TLS is now supported. |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 431 | |
Victor Stinner | c8ea813 | 2014-01-23 11:02:09 +0100 | [diff] [blame] | 432 | .. seealso:: |
| 433 | |
| 434 | The function :func:`start_server` creates a (:class:`StreamReader`, |
| 435 | :class:`StreamWriter`) pair and calls back a function with this pair. |
| 436 | |
Victor Stinner | 7b58a2b | 2015-09-21 18:41:05 +0200 | [diff] [blame] | 437 | .. versionchanged:: 3.5.1 |
Victor Stinner | 5e4a7d8 | 2015-09-21 18:33:43 +0200 | [diff] [blame] | 438 | |
| 439 | The *host* parameter can now be a sequence of strings. |
| 440 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 441 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 442 | .. coroutinemethod:: BaseEventLoop.create_unix_server(protocol_factory, path=None, \*, sock=None, backlog=100, ssl=None) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 443 | |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 444 | Similar to :meth:`BaseEventLoop.create_server`, but specific to the |
| 445 | socket family :py:data:`~socket.AF_UNIX`. |
| 446 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 447 | This method is a :ref:`coroutine <coroutine>`. |
| 448 | |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 449 | Availability: UNIX. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 450 | |
| 451 | |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 452 | Watch file descriptors |
| 453 | ---------------------- |
| 454 | |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 455 | On Windows with :class:`SelectorEventLoop`, only socket handles are supported |
| 456 | (ex: pipe file descriptors are not supported). |
| 457 | |
| 458 | On Windows with :class:`ProactorEventLoop`, these methods are not supported. |
| 459 | |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 460 | .. method:: BaseEventLoop.add_reader(fd, callback, \*args) |
| 461 | |
| 462 | Start watching the file descriptor for read availability and then call the |
| 463 | *callback* with specified arguments. |
| 464 | |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 465 | :ref:`Use functools.partial to pass keywords to the callback |
| 466 | <asyncio-pass-keywords>`. |
| 467 | |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 468 | .. method:: BaseEventLoop.remove_reader(fd) |
| 469 | |
| 470 | Stop watching the file descriptor for read availability. |
| 471 | |
| 472 | .. method:: BaseEventLoop.add_writer(fd, callback, \*args) |
| 473 | |
| 474 | Start watching the file descriptor for write availability and then call the |
| 475 | *callback* with specified arguments. |
| 476 | |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 477 | :ref:`Use functools.partial to pass keywords to the callback |
| 478 | <asyncio-pass-keywords>`. |
| 479 | |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 480 | .. method:: BaseEventLoop.remove_writer(fd) |
| 481 | |
| 482 | Stop watching the file descriptor for write availability. |
| 483 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 484 | The :ref:`watch a file descriptor for read events <asyncio-watch-read-event>` |
| 485 | example uses the low-level :meth:`BaseEventLoop.add_reader` method to register |
| 486 | the file descriptor of a socket. |
| 487 | |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 488 | |
| 489 | Low-level socket operations |
| 490 | --------------------------- |
| 491 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 492 | .. coroutinemethod:: BaseEventLoop.sock_recv(sock, nbytes) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 493 | |
| 494 | Receive data from the socket. The return value is a bytes object |
| 495 | representing the data received. The maximum amount of data to be received |
| 496 | at once is specified by *nbytes*. |
| 497 | |
Victor Stinner | d84fd73 | 2014-08-26 01:01:59 +0200 | [diff] [blame] | 498 | With :class:`SelectorEventLoop` event loop, the socket *sock* must be |
| 499 | non-blocking. |
Victor Stinner | ec2ce09 | 2014-07-29 23:12:22 +0200 | [diff] [blame] | 500 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 501 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 502 | |
| 503 | .. seealso:: |
| 504 | |
| 505 | The :meth:`socket.socket.recv` method. |
| 506 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 507 | .. coroutinemethod:: BaseEventLoop.sock_sendall(sock, data) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 508 | |
| 509 | Send data to the socket. The socket must be connected to a remote socket. |
| 510 | This method continues to send data from *data* until either all data has |
| 511 | been sent or an error occurs. ``None`` is returned on success. On error, |
| 512 | an exception is raised, and there is no way to determine how much data, if |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 513 | any, was successfully processed by the receiving end of the connection. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 514 | |
Victor Stinner | d84fd73 | 2014-08-26 01:01:59 +0200 | [diff] [blame] | 515 | With :class:`SelectorEventLoop` event loop, the socket *sock* must be |
| 516 | non-blocking. |
Victor Stinner | ec2ce09 | 2014-07-29 23:12:22 +0200 | [diff] [blame] | 517 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 518 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 519 | |
| 520 | .. seealso:: |
| 521 | |
| 522 | The :meth:`socket.socket.sendall` method. |
| 523 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 524 | .. coroutinemethod:: BaseEventLoop.sock_connect(sock, address) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 525 | |
| 526 | Connect to a remote socket at *address*. |
| 527 | |
Victor Stinner | 1b0580b | 2014-02-13 09:24:37 +0100 | [diff] [blame] | 528 | The *address* must be already resolved to avoid the trap of hanging the |
| 529 | entire event loop when the address requires doing a DNS lookup. For |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 530 | example, it must be an IP address, not a hostname, for |
Victor Stinner | 1b0580b | 2014-02-13 09:24:37 +0100 | [diff] [blame] | 531 | :py:data:`~socket.AF_INET` and :py:data:`~socket.AF_INET6` address families. |
| 532 | Use :meth:`getaddrinfo` to resolve the hostname asynchronously. |
| 533 | |
Victor Stinner | d84fd73 | 2014-08-26 01:01:59 +0200 | [diff] [blame] | 534 | With :class:`SelectorEventLoop` event loop, the socket *sock* must be |
| 535 | non-blocking. |
Victor Stinner | ec2ce09 | 2014-07-29 23:12:22 +0200 | [diff] [blame] | 536 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 537 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 538 | |
| 539 | .. seealso:: |
| 540 | |
| 541 | The :meth:`BaseEventLoop.create_connection` method, the |
| 542 | :func:`open_connection` function and the :meth:`socket.socket.connect` |
| 543 | method. |
| 544 | |
| 545 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 546 | .. coroutinemethod:: BaseEventLoop.sock_accept(sock) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 547 | |
| 548 | Accept a connection. The socket must be bound to an address and listening |
| 549 | for connections. The return value is a pair ``(conn, address)`` where *conn* |
| 550 | is a *new* socket object usable to send and receive data on the connection, |
| 551 | and *address* is the address bound to the socket on the other end of the |
| 552 | connection. |
| 553 | |
Victor Stinner | ec2ce09 | 2014-07-29 23:12:22 +0200 | [diff] [blame] | 554 | The socket *sock* must be non-blocking. |
| 555 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 556 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 557 | |
| 558 | .. seealso:: |
| 559 | |
| 560 | The :meth:`BaseEventLoop.create_server` method, the :func:`start_server` |
| 561 | function and the :meth:`socket.socket.accept` method. |
| 562 | |
| 563 | |
| 564 | Resolve host name |
| 565 | ----------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 566 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 567 | .. coroutinemethod:: BaseEventLoop.getaddrinfo(host, port, \*, family=0, type=0, proto=0, flags=0) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 568 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 569 | This method is a :ref:`coroutine <coroutine>`, similar to |
| 570 | :meth:`socket.getaddrinfo` function but non-blocking. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 571 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 572 | .. coroutinemethod:: BaseEventLoop.getnameinfo(sockaddr, flags=0) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 573 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 574 | This method is a :ref:`coroutine <coroutine>`, similar to |
| 575 | :meth:`socket.getnameinfo` function but non-blocking. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 576 | |
| 577 | |
Victor Stinner | 984600f | 2014-03-25 09:40:26 +0100 | [diff] [blame] | 578 | Connect pipes |
| 579 | ------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 580 | |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 581 | On Windows with :class:`SelectorEventLoop`, these methods are not supported. |
| 582 | Use :class:`ProactorEventLoop` to support pipes on Windows. |
| 583 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 584 | .. coroutinemethod:: BaseEventLoop.connect_read_pipe(protocol_factory, pipe) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 585 | |
Victor Stinner | d84fd73 | 2014-08-26 01:01:59 +0200 | [diff] [blame] | 586 | Register read pipe in eventloop. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 587 | |
| 588 | *protocol_factory* should instantiate object with :class:`Protocol` |
Victor Stinner | a5b257a | 2014-05-29 00:14:03 +0200 | [diff] [blame] | 589 | interface. *pipe* is a :term:`file-like object <file object>`. |
| 590 | Return pair ``(transport, protocol)``, where *transport* supports the |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 591 | :class:`ReadTransport` interface. |
| 592 | |
Victor Stinner | d84fd73 | 2014-08-26 01:01:59 +0200 | [diff] [blame] | 593 | With :class:`SelectorEventLoop` event loop, the *pipe* is set to |
| 594 | non-blocking mode. |
| 595 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 596 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 597 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 598 | .. coroutinemethod:: BaseEventLoop.connect_write_pipe(protocol_factory, pipe) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 599 | |
| 600 | Register write pipe in eventloop. |
| 601 | |
| 602 | *protocol_factory* should instantiate object with :class:`BaseProtocol` |
Victor Stinner | 2cef300 | 2014-10-23 22:38:46 +0200 | [diff] [blame] | 603 | interface. *pipe* is :term:`file-like object <file object>`. |
| 604 | Return pair ``(transport, protocol)``, where *transport* supports |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 605 | :class:`WriteTransport` interface. |
| 606 | |
Victor Stinner | d84fd73 | 2014-08-26 01:01:59 +0200 | [diff] [blame] | 607 | With :class:`SelectorEventLoop` event loop, the *pipe* is set to |
| 608 | non-blocking mode. |
| 609 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 610 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 611 | |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 612 | .. seealso:: |
| 613 | |
Victor Stinner | 984600f | 2014-03-25 09:40:26 +0100 | [diff] [blame] | 614 | The :meth:`BaseEventLoop.subprocess_exec` and |
| 615 | :meth:`BaseEventLoop.subprocess_shell` methods. |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 616 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 617 | |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 618 | UNIX signals |
| 619 | ------------ |
| 620 | |
| 621 | Availability: UNIX only. |
| 622 | |
| 623 | .. method:: BaseEventLoop.add_signal_handler(signum, callback, \*args) |
| 624 | |
| 625 | Add a handler for a signal. |
| 626 | |
| 627 | Raise :exc:`ValueError` if the signal number is invalid or uncatchable. |
| 628 | Raise :exc:`RuntimeError` if there is a problem setting up the handler. |
| 629 | |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 630 | :ref:`Use functools.partial to pass keywords to the callback |
| 631 | <asyncio-pass-keywords>`. |
| 632 | |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 633 | .. method:: BaseEventLoop.remove_signal_handler(sig) |
| 634 | |
| 635 | Remove a handler for a signal. |
| 636 | |
| 637 | Return ``True`` if a signal handler was removed, ``False`` if not. |
| 638 | |
| 639 | .. seealso:: |
| 640 | |
| 641 | The :mod:`signal` module. |
| 642 | |
| 643 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 644 | Executor |
| 645 | -------- |
| 646 | |
| 647 | Call a function in an :class:`~concurrent.futures.Executor` (pool of threads or |
| 648 | pool of processes). By default, an event loop uses a thread pool executor |
| 649 | (:class:`~concurrent.futures.ThreadPoolExecutor`). |
| 650 | |
Andrew Svetlov | 1c62b52 | 2015-10-01 09:48:08 +0300 | [diff] [blame] | 651 | .. coroutinemethod:: BaseEventLoop.run_in_executor(executor, func, \*args) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 652 | |
Andrew Svetlov | 1c62b52 | 2015-10-01 09:48:08 +0300 | [diff] [blame] | 653 | Arrange for a *func* to be called in the specified executor. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 654 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 655 | The *executor* argument should be an :class:`~concurrent.futures.Executor` |
| 656 | instance. The default executor is used if *executor* is ``None``. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 657 | |
Andrew Svetlov | 1c62b52 | 2015-10-01 09:48:08 +0300 | [diff] [blame] | 658 | :ref:`Use functools.partial to pass keywords to the *func* |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 659 | <asyncio-pass-keywords>`. |
| 660 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 661 | This method is a :ref:`coroutine <coroutine>`. |
| 662 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 663 | .. method:: BaseEventLoop.set_default_executor(executor) |
| 664 | |
| 665 | Set the default executor used by :meth:`run_in_executor`. |
| 666 | |
| 667 | |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 668 | Error Handling API |
| 669 | ------------------ |
| 670 | |
Martin Panter | c04fb56 | 2016-02-10 05:44:01 +0000 | [diff] [blame] | 671 | Allows customizing how exceptions are handled in the event loop. |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 672 | |
| 673 | .. method:: BaseEventLoop.set_exception_handler(handler) |
| 674 | |
| 675 | Set *handler* as the new event loop exception handler. |
| 676 | |
| 677 | If *handler* is ``None``, the default exception handler will |
| 678 | be set. |
| 679 | |
| 680 | If *handler* is a callable object, it should have a |
| 681 | matching signature to ``(loop, context)``, where ``loop`` |
| 682 | will be a reference to the active event loop, ``context`` |
| 683 | will be a ``dict`` object (see :meth:`call_exception_handler` |
| 684 | documentation for details about context). |
| 685 | |
Yury Selivanov | 950204d | 2016-05-16 16:23:00 -0400 | [diff] [blame] | 686 | .. method:: BaseEventLoop.get_exception_handler() |
| 687 | |
| 688 | Return the exception handler, or ``None`` if the default one |
| 689 | is in use. |
| 690 | |
| 691 | .. versionadded:: 3.5.2 |
| 692 | |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 693 | .. method:: BaseEventLoop.default_exception_handler(context) |
| 694 | |
| 695 | Default exception handler. |
| 696 | |
| 697 | This is called when an exception occurs and no exception |
| 698 | handler is set, and can be called by a custom exception |
| 699 | handler that wants to defer to the default behavior. |
| 700 | |
| 701 | *context* parameter has the same meaning as in |
| 702 | :meth:`call_exception_handler`. |
| 703 | |
| 704 | .. method:: BaseEventLoop.call_exception_handler(context) |
| 705 | |
| 706 | Call the current event loop exception handler. |
| 707 | |
| 708 | *context* is a ``dict`` object containing the following keys |
| 709 | (new keys may be introduced later): |
| 710 | |
| 711 | * 'message': Error message; |
| 712 | * 'exception' (optional): Exception object; |
| 713 | * 'future' (optional): :class:`asyncio.Future` instance; |
| 714 | * 'handle' (optional): :class:`asyncio.Handle` instance; |
| 715 | * 'protocol' (optional): :ref:`Protocol <asyncio-protocol>` instance; |
| 716 | * 'transport' (optional): :ref:`Transport <asyncio-transport>` instance; |
| 717 | * 'socket' (optional): :class:`socket.socket` instance. |
| 718 | |
| 719 | .. note:: |
| 720 | |
| 721 | Note: this method should not be overloaded in subclassed |
| 722 | event loops. For any custom exception handling, use |
| 723 | :meth:`set_exception_handler()` method. |
| 724 | |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 725 | Debug mode |
| 726 | ---------- |
| 727 | |
| 728 | .. method:: BaseEventLoop.get_debug() |
| 729 | |
Victor Stinner | 7b7120e | 2014-06-23 00:12:14 +0200 | [diff] [blame] | 730 | Get the debug mode (:class:`bool`) of the event loop. |
| 731 | |
| 732 | The default value is ``True`` if the environment variable |
| 733 | :envvar:`PYTHONASYNCIODEBUG` is set to a non-empty string, ``False`` |
| 734 | otherwise. |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 735 | |
Victor Stinner | 64d750b | 2014-06-18 03:25:23 +0200 | [diff] [blame] | 736 | .. versionadded:: 3.4.2 |
| 737 | |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 738 | .. method:: BaseEventLoop.set_debug(enabled: bool) |
| 739 | |
| 740 | Set the debug mode of the event loop. |
| 741 | |
Victor Stinner | 64d750b | 2014-06-18 03:25:23 +0200 | [diff] [blame] | 742 | .. versionadded:: 3.4.2 |
| 743 | |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 744 | .. seealso:: |
| 745 | |
Victor Stinner | 62511fd | 2014-06-23 00:36:11 +0200 | [diff] [blame] | 746 | The :ref:`debug mode of asyncio <asyncio-debug-mode>`. |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 747 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 748 | Server |
| 749 | ------ |
| 750 | |
Victor Stinner | 8ebeb03 | 2014-07-11 23:47:40 +0200 | [diff] [blame] | 751 | .. class:: Server |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 752 | |
Victor Stinner | 8ebeb03 | 2014-07-11 23:47:40 +0200 | [diff] [blame] | 753 | Server listening on sockets. |
| 754 | |
| 755 | Object created by the :meth:`BaseEventLoop.create_server` method and the |
R David Murray | 756f0b1 | 2015-01-29 19:53:33 -0500 | [diff] [blame] | 756 | :func:`start_server` function. Don't instantiate the class directly. |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 757 | |
| 758 | .. method:: close() |
| 759 | |
Victor Stinner | 4bfb14a | 2014-07-12 03:20:24 +0200 | [diff] [blame] | 760 | Stop serving: close listening sockets and set the :attr:`sockets` |
| 761 | attribute to ``None``. |
| 762 | |
Berker Peksag | 49c9edf | 2016-01-20 07:14:22 +0200 | [diff] [blame] | 763 | The sockets that represent existing incoming client connections are left |
| 764 | open. |
Victor Stinner | 8ebeb03 | 2014-07-11 23:47:40 +0200 | [diff] [blame] | 765 | |
Berker Peksag | 49c9edf | 2016-01-20 07:14:22 +0200 | [diff] [blame] | 766 | The server is closed asynchronously, use the :meth:`wait_closed` |
| 767 | coroutine to wait until the server is closed. |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 768 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 769 | .. coroutinemethod:: wait_closed() |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 770 | |
Victor Stinner | 8ebeb03 | 2014-07-11 23:47:40 +0200 | [diff] [blame] | 771 | Wait until the :meth:`close` method completes. |
| 772 | |
| 773 | This method is a :ref:`coroutine <coroutine>`. |
| 774 | |
| 775 | .. attribute:: sockets |
| 776 | |
| 777 | List of :class:`socket.socket` objects the server is listening to, or |
| 778 | ``None`` if the server is closed. |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 779 | |
| 780 | |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 781 | Handle |
| 782 | ------ |
| 783 | |
| 784 | .. class:: Handle |
| 785 | |
| 786 | A callback wrapper object returned by :func:`BaseEventLoop.call_soon`, |
| 787 | :func:`BaseEventLoop.call_soon_threadsafe`, :func:`BaseEventLoop.call_later`, |
| 788 | and :func:`BaseEventLoop.call_at`. |
| 789 | |
| 790 | .. method:: cancel() |
| 791 | |
Yury Selivanov | 1096f76 | 2015-06-25 13:49:52 -0400 | [diff] [blame] | 792 | Cancel the call. If the callback is already canceled or executed, |
| 793 | this method has no effect. |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 794 | |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 795 | |
Victor Stinner | 6888b96 | 2014-10-11 16:15:58 +0200 | [diff] [blame] | 796 | Event loop examples |
Victor Stinner | a092a61 | 2015-01-09 15:58:41 +0100 | [diff] [blame] | 797 | ------------------- |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 798 | |
Victor Stinner | 3e09e32 | 2013-12-03 01:22:06 +0100 | [diff] [blame] | 799 | .. _asyncio-hello-world-callback: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 800 | |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 801 | Hello World with call_soon() |
Victor Stinner | a092a61 | 2015-01-09 15:58:41 +0100 | [diff] [blame] | 802 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 803 | |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 804 | Example using the :meth:`BaseEventLoop.call_soon` method to schedule a |
| 805 | callback. The callback displays ``"Hello World"`` and then stops the event |
| 806 | loop:: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 807 | |
| 808 | import asyncio |
| 809 | |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 810 | def hello_world(loop): |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 811 | print('Hello World') |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 812 | loop.stop() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 813 | |
| 814 | loop = asyncio.get_event_loop() |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 815 | |
| 816 | # Schedule a call to hello_world() |
| 817 | loop.call_soon(hello_world, loop) |
| 818 | |
| 819 | # Blocking call interrupted by loop.stop() |
| 820 | loop.run_forever() |
| 821 | loop.close() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 822 | |
Victor Stinner | 3e09e32 | 2013-12-03 01:22:06 +0100 | [diff] [blame] | 823 | .. seealso:: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 824 | |
Victor Stinner | 6888b96 | 2014-10-11 16:15:58 +0200 | [diff] [blame] | 825 | The :ref:`Hello World coroutine <asyncio-hello-world-coroutine>` example |
| 826 | uses a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 827 | |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 828 | |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 829 | .. _asyncio-date-callback: |
| 830 | |
| 831 | Display the current date with call_later() |
Victor Stinner | a092a61 | 2015-01-09 15:58:41 +0100 | [diff] [blame] | 832 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 833 | |
| 834 | Example of callback displaying the current date every second. The callback uses |
| 835 | the :meth:`BaseEventLoop.call_later` method to reschedule itself during 5 |
| 836 | seconds, and then stops the event loop:: |
| 837 | |
| 838 | import asyncio |
| 839 | import datetime |
| 840 | |
| 841 | def display_date(end_time, loop): |
| 842 | print(datetime.datetime.now()) |
| 843 | if (loop.time() + 1.0) < end_time: |
| 844 | loop.call_later(1, display_date, end_time, loop) |
| 845 | else: |
| 846 | loop.stop() |
| 847 | |
| 848 | loop = asyncio.get_event_loop() |
| 849 | |
| 850 | # Schedule the first call to display_date() |
| 851 | end_time = loop.time() + 5.0 |
| 852 | loop.call_soon(display_date, end_time, loop) |
| 853 | |
| 854 | # Blocking call interrupted by loop.stop() |
| 855 | loop.run_forever() |
| 856 | loop.close() |
| 857 | |
| 858 | .. seealso:: |
| 859 | |
| 860 | The :ref:`coroutine displaying the current date |
| 861 | <asyncio-date-coroutine>` example uses a :ref:`coroutine |
| 862 | <coroutine>`. |
| 863 | |
| 864 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 865 | .. _asyncio-watch-read-event: |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 866 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 867 | Watch a file descriptor for read events |
Victor Stinner | a092a61 | 2015-01-09 15:58:41 +0100 | [diff] [blame] | 868 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 869 | |
| 870 | Wait until a file descriptor received some data using the |
| 871 | :meth:`BaseEventLoop.add_reader` method and then close the event loop:: |
| 872 | |
| 873 | import asyncio |
Victor Stinner | ccd8e34 | 2014-10-11 16:30:02 +0200 | [diff] [blame] | 874 | try: |
| 875 | from socket import socketpair |
| 876 | except ImportError: |
| 877 | from asyncio.windows_utils import socketpair |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 878 | |
| 879 | # Create a pair of connected file descriptors |
Victor Stinner | ccd8e34 | 2014-10-11 16:30:02 +0200 | [diff] [blame] | 880 | rsock, wsock = socketpair() |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 881 | loop = asyncio.get_event_loop() |
| 882 | |
| 883 | def reader(): |
| 884 | data = rsock.recv(100) |
| 885 | print("Received:", data.decode()) |
Victor Stinner | 2cef300 | 2014-10-23 22:38:46 +0200 | [diff] [blame] | 886 | # We are done: unregister the file descriptor |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 887 | loop.remove_reader(rsock) |
| 888 | # Stop the event loop |
| 889 | loop.stop() |
| 890 | |
Victor Stinner | 2cef300 | 2014-10-23 22:38:46 +0200 | [diff] [blame] | 891 | # Register the file descriptor for read event |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 892 | loop.add_reader(rsock, reader) |
| 893 | |
| 894 | # Simulate the reception of data from the network |
| 895 | loop.call_soon(wsock.send, 'abc'.encode()) |
| 896 | |
| 897 | # Run the event loop |
| 898 | loop.run_forever() |
| 899 | |
| 900 | # We are done, close sockets and the event loop |
| 901 | rsock.close() |
| 902 | wsock.close() |
| 903 | loop.close() |
| 904 | |
| 905 | .. seealso:: |
| 906 | |
| 907 | The :ref:`register an open socket to wait for data using a protocol |
| 908 | <asyncio-register-socket>` example uses a low-level protocol created by the |
| 909 | :meth:`BaseEventLoop.create_connection` method. |
| 910 | |
| 911 | The :ref:`register an open socket to wait for data using streams |
| 912 | <asyncio-register-socket-streams>` example uses high-level streams |
| 913 | created by the :func:`open_connection` function in a coroutine. |
| 914 | |
| 915 | |
| 916 | Set signal handlers for SIGINT and SIGTERM |
Victor Stinner | a092a61 | 2015-01-09 15:58:41 +0100 | [diff] [blame] | 917 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 918 | |
| 919 | Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM` using |
| 920 | the :meth:`BaseEventLoop.add_signal_handler` method:: |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 921 | |
| 922 | import asyncio |
| 923 | import functools |
| 924 | import os |
| 925 | import signal |
| 926 | |
| 927 | def ask_exit(signame): |
| 928 | print("got signal %s: exit" % signame) |
| 929 | loop.stop() |
| 930 | |
| 931 | loop = asyncio.get_event_loop() |
| 932 | for signame in ('SIGINT', 'SIGTERM'): |
| 933 | loop.add_signal_handler(getattr(signal, signame), |
| 934 | functools.partial(ask_exit, signame)) |
| 935 | |
Serhiy Storchaka | 0424eaf | 2015-09-12 17:45:25 +0300 | [diff] [blame] | 936 | print("Event loop running forever, press Ctrl+C to interrupt.") |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 937 | print("pid %s: send SIGINT or SIGTERM to exit." % os.getpid()) |
Victor Stinner | 63b21a8 | 2014-07-05 15:38:59 +0200 | [diff] [blame] | 938 | try: |
| 939 | loop.run_forever() |
| 940 | finally: |
| 941 | loop.close() |
Victor Stinner | 2cef300 | 2014-10-23 22:38:46 +0200 | [diff] [blame] | 942 | |
| 943 | This example only works on UNIX. |