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