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