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