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