Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1 | .. module:: asyncio |
| 2 | |
| 3 | .. _event-loop: |
| 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 | |
| 11 | * Registering, executing and cancelling delayed calls (timeouts) |
| 12 | |
| 13 | * Creating client and server :ref:`transports <transport>` for various |
| 14 | kinds of communication |
| 15 | |
| 16 | * Launching subprocesses and the associated :ref:`transports <transport>` |
| 17 | for communication with an external program |
| 18 | |
| 19 | * Delegating costly function calls to a pool of threads |
| 20 | |
| 21 | Event loop functions |
| 22 | -------------------- |
| 23 | |
| 24 | The easiest way to get an event loop is to call the :func:`get_event_loop` |
| 25 | function. |
| 26 | |
| 27 | .. function:: get_event_loop() |
| 28 | |
| 29 | Get the event loop for current context. Returns an event loop object |
| 30 | implementing :class:`BaseEventLoop` interface, or raises an exception in case no |
| 31 | event loop has been set for the current context and the current policy does |
| 32 | not specify to create one. It should never return ``None``. |
| 33 | |
| 34 | .. function:: set_event_loop(loop) |
| 35 | |
| 36 | XXX |
| 37 | |
| 38 | .. function:: new_event_loop() |
| 39 | |
| 40 | XXX |
| 41 | |
| 42 | |
| 43 | Event loop policy |
| 44 | ----------------- |
| 45 | |
| 46 | .. function:: get_event_loop_policy() |
| 47 | |
| 48 | XXX |
| 49 | |
| 50 | .. function:: set_event_loop_policy(policy) |
| 51 | |
| 52 | XXX |
| 53 | |
| 54 | |
| 55 | Run an event loop |
| 56 | ----------------- |
| 57 | |
| 58 | .. method:: BaseEventLoop.run_forever() |
| 59 | |
| 60 | Run until :meth:`stop` is called. |
| 61 | |
| 62 | .. method:: BaseEventLoop.run_until_complete(future) |
| 63 | |
Victor Stinner | 99c2ab4 | 2013-12-03 19:17:25 +0100 | [diff] [blame] | 64 | Run until the :class:`Future` is done. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 65 | |
| 66 | If the argument is a coroutine, it is wrapped in a :class:`Task`. |
| 67 | |
| 68 | Return the Future's result, or raise its exception. |
| 69 | |
| 70 | .. method:: BaseEventLoop.is_running() |
| 71 | |
| 72 | Returns running status of event loop. |
| 73 | |
Victor Stinner | afbf827 | 2013-12-03 02:05:42 +0100 | [diff] [blame] | 74 | .. method:: BaseEventLoop.stop() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 75 | |
| 76 | Stop running the event loop. |
| 77 | |
| 78 | Every callback scheduled before :meth:`stop` is called will run. |
| 79 | Callback scheduled after :meth:`stop` is called won't. However, those |
| 80 | callbacks will run if :meth:`run_forever` is called again later. |
| 81 | |
| 82 | .. method:: BaseEventLoop.close() |
| 83 | |
| 84 | Close the event loop. The loop should not be running. |
| 85 | |
| 86 | This clears the queues and shuts down the executor, but does not wait for |
| 87 | the executor to finish. |
| 88 | |
| 89 | This is idempotent and irreversible. No other methods should be called after |
| 90 | this one. |
| 91 | |
| 92 | |
| 93 | Calls |
| 94 | ----- |
| 95 | |
| 96 | .. method:: BaseEventLoop.call_soon(callback, \*args) |
| 97 | |
| 98 | Arrange for a callback to be called as soon as possible. |
| 99 | |
| 100 | This operates as a FIFO queue, callbacks are called in the order in |
| 101 | which they are registered. Each callback will be called exactly once. |
| 102 | |
| 103 | Any positional arguments after the callback will be passed to the |
| 104 | callback when it is called. |
| 105 | |
| 106 | .. method:: BaseEventLoop.call_soon_threadsafe(callback, \*args) |
| 107 | |
| 108 | Like :meth:`call_soon`, but thread safe. |
| 109 | |
| 110 | |
| 111 | Delayed calls |
| 112 | ------------- |
| 113 | |
| 114 | The event loop has its own internal clock for computing timeouts. |
| 115 | Which clock is used depends on the (platform-specific) event loop |
| 116 | implementation; ideally it is a monotonic clock. This will generally be |
| 117 | a different clock than :func:`time.time`. |
| 118 | |
| 119 | .. method:: BaseEventLoop.call_later(delay, callback, *args) |
| 120 | |
| 121 | Arrange for the *callback* to be called after the given *delay* |
| 122 | seconds (either an int or float). |
| 123 | |
| 124 | A "handle" is returned: an opaque object with a :meth:`cancel` method |
| 125 | that can be used to cancel the call. |
| 126 | |
| 127 | *callback* will be called exactly once per call to :meth:`call_later`. |
| 128 | If two callbacks are scheduled for exactly the same time, it is |
| 129 | undefined which will be called first. |
| 130 | |
| 131 | The optional positional *args* will be passed to the callback when it |
| 132 | is called. If you want the callback to be called with some named |
| 133 | arguments, use a closure or :func:`functools.partial`. |
| 134 | |
| 135 | .. method:: BaseEventLoop.call_at(when, callback, *args) |
| 136 | |
| 137 | Arrange for the *callback* to be called at the given absolute timestamp |
| 138 | *when* (an int or float), using the same time reference as :meth:`time`. |
| 139 | |
| 140 | This method's behavior is the same as :meth:`call_later`. |
| 141 | |
| 142 | .. method:: BaseEventLoop.time() |
| 143 | |
| 144 | Return the current time, as a :class:`float` value, according to the |
| 145 | event loop's internal clock. |
| 146 | |
Victor Stinner | 3e09e32 | 2013-12-03 01:22:06 +0100 | [diff] [blame] | 147 | .. seealso:: |
| 148 | |
| 149 | The :func:`asyncio.sleep` function. |
| 150 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 151 | |
| 152 | Creating connections |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 153 | -------------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 154 | |
| 155 | .. 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) |
| 156 | |
| 157 | Create a streaming transport connection to a given Internet *host* and |
| 158 | *port*. *protocol_factory* must be a callable returning a |
| 159 | :ref:`protocol <protocol>` instance. |
| 160 | |
| 161 | This method returns a :ref:`coroutine <coroutine>` which will try to |
| 162 | establish the connection in the background. When successful, the |
| 163 | coroutine returns a ``(transport, protocol)`` pair. |
| 164 | |
| 165 | The chronological synopsis of the underlying operation is as follows: |
| 166 | |
| 167 | #. The connection is established, and a :ref:`transport <transport>` |
| 168 | is created to represent it. |
| 169 | |
| 170 | #. *protocol_factory* is called without arguments and must return a |
| 171 | :ref:`protocol <protocol>` instance. |
| 172 | |
| 173 | #. The protocol instance is tied to the transport, and its |
| 174 | :meth:`connection_made` method is called. |
| 175 | |
| 176 | #. The coroutine returns successfully with the ``(transport, protocol)`` |
| 177 | pair. |
| 178 | |
| 179 | The created transport is an implementation-dependent bidirectional stream. |
| 180 | |
| 181 | .. note:: |
| 182 | *protocol_factory* can be any kind of callable, not necessarily |
| 183 | a class. For example, if you want to use a pre-created |
| 184 | protocol instance, you can pass ``lambda: my_protocol``. |
| 185 | |
| 186 | Options allowing to change how the connection is created: |
| 187 | |
| 188 | * *ssl*: if given and not false, a SSL/TLS transport is created |
| 189 | (by default a plain TCP transport is created). If *ssl* is |
| 190 | a :class:`ssl.SSLContext` object, this context is used to create |
| 191 | the transport; if *ssl* is :const:`True`, a context with some |
| 192 | unspecified default settings is used. |
| 193 | |
| 194 | * *server_hostname*, is only for use together with *ssl*, |
| 195 | and sets or overrides the hostname that the target server's certificate |
| 196 | will be matched against. By default the value of the *host* argument |
| 197 | is used. If *host* is empty, there is no default and you must pass a |
| 198 | value for *server_hostname*. If *server_hostname* is an empty |
| 199 | string, hostname matching is disabled (which is a serious security |
| 200 | risk, allowing for man-in-the-middle-attacks). |
| 201 | |
| 202 | * *family*, *proto*, *flags* are the optional address family, protocol |
| 203 | and flags to be passed through to getaddrinfo() for *host* resolution. |
| 204 | If given, these should all be integers from the corresponding |
| 205 | :mod:`socket` module constants. |
| 206 | |
| 207 | * *sock*, if given, should be an existing, already connected |
| 208 | :class:`socket.socket` object to be used by the transport. |
| 209 | If *sock* is given, none of *host*, *port*, *family*, *proto*, *flags* |
| 210 | and *local_addr* should be specified. |
| 211 | |
| 212 | * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used |
| 213 | to bind the socket to locally. The *local_host* and *local_port* |
| 214 | are looked up using getaddrinfo(), similarly to *host* and *port*. |
| 215 | |
| 216 | |
| 217 | Creating listening connections |
| 218 | ------------------------------ |
| 219 | |
| 220 | .. 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) |
| 221 | |
| 222 | A :ref:`coroutine <coroutine>` which creates a TCP server bound to host and |
| 223 | port. |
| 224 | |
| 225 | The return value is a :class:`AbstractServer` object which can be used to stop |
| 226 | the service. |
| 227 | |
| 228 | If *host* is an empty string or None all interfaces are assumed |
| 229 | and a list of multiple sockets will be returned (most likely |
| 230 | one for IPv4 and another one for IPv6). |
| 231 | |
| 232 | *family* can be set to either :data:`~socket.AF_INET` or |
| 233 | :data:`~socket.AF_INET6` to force the socket to use IPv4 or IPv6. If not set |
| 234 | it will be determined from host (defaults to :data:`~socket.AF_UNSPEC`). |
| 235 | |
| 236 | *flags* is a bitmask for :meth:`getaddrinfo`. |
| 237 | |
| 238 | *sock* can optionally be specified in order to use a preexisting |
| 239 | socket object. |
| 240 | |
| 241 | *backlog* is the maximum number of queued connections passed to |
| 242 | :meth:`~socket.socket.listen` (defaults to 100). |
| 243 | |
| 244 | ssl can be set to an :class:`~ssl.SSLContext` to enable SSL over the |
| 245 | accepted connections. |
| 246 | |
| 247 | *reuse_address* tells the kernel to reuse a local socket in |
| 248 | TIME_WAIT state, without waiting for its natural timeout to |
| 249 | expire. If not specified will automatically be set to True on |
| 250 | UNIX. |
| 251 | |
| 252 | This method returns a :ref:`coroutine <coroutine>`. |
| 253 | |
| 254 | .. method:: BaseEventLoop.create_datagram_endpoint(protocol_factory, local_addr=None, remote_addr=None, \*, family=0, proto=0, flags=0) |
| 255 | |
| 256 | Create datagram connection. |
| 257 | |
| 258 | This method returns a :ref:`coroutine <coroutine>`. |
| 259 | |
| 260 | |
| 261 | |
| 262 | Resolve name |
| 263 | ------------ |
| 264 | |
| 265 | .. method:: BaseEventLoop.getaddrinfo(host, port, \*, family=0, type=0, proto=0, flags=0) |
| 266 | |
| 267 | XXX |
| 268 | |
| 269 | .. method:: BaseEventLoop.getnameinfo(sockaddr, flags=0) |
| 270 | |
| 271 | XXX |
| 272 | |
| 273 | |
| 274 | Running subprocesses |
| 275 | -------------------- |
| 276 | |
| 277 | Run subprocesses asynchronously using the :mod:`subprocess` module. |
| 278 | |
| 279 | .. method:: BaseEventLoop.subprocess_exec(protocol_factory, \*args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=False, shell=False, bufsize=0, \*\*kwargs) |
| 280 | |
| 281 | XXX |
| 282 | |
| 283 | This method returns a :ref:`coroutine <coroutine>`. |
| 284 | |
| 285 | See the constructor of the :class:`subprocess.Popen` class for parameters. |
| 286 | |
| 287 | .. method:: BaseEventLoop.subprocess_shell(protocol_factory, cmd, \*, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=False, shell=True, bufsize=0, \*\*kwargs) |
| 288 | |
| 289 | XXX |
| 290 | |
| 291 | This method returns a :ref:`coroutine <coroutine>`. |
| 292 | |
| 293 | See the constructor of the :class:`subprocess.Popen` class for parameters. |
| 294 | |
| 295 | .. method:: BaseEventLoop.connect_read_pipe(protocol_factory, pipe) |
| 296 | |
| 297 | Register read pipe in eventloop. |
| 298 | |
| 299 | *protocol_factory* should instantiate object with :class:`Protocol` |
| 300 | interface. pipe is file-like object already switched to nonblocking. |
| 301 | Return pair (transport, protocol), where transport support |
| 302 | :class:`ReadTransport` interface. |
| 303 | |
| 304 | This method returns a :ref:`coroutine <coroutine>`. |
| 305 | |
| 306 | .. method:: BaseEventLoop.connect_write_pipe(protocol_factory, pipe) |
| 307 | |
| 308 | Register write pipe in eventloop. |
| 309 | |
| 310 | *protocol_factory* should instantiate object with :class:`BaseProtocol` |
| 311 | interface. Pipe is file-like object already switched to nonblocking. |
| 312 | Return pair (transport, protocol), where transport support |
| 313 | :class:`WriteTransport` interface. |
| 314 | |
| 315 | This method returns a :ref:`coroutine <coroutine>`. |
| 316 | |
| 317 | |
| 318 | Executor |
| 319 | -------- |
| 320 | |
| 321 | Call a function in an :class:`~concurrent.futures.Executor` (pool of threads or |
| 322 | pool of processes). By default, an event loop uses a thread pool executor |
| 323 | (:class:`~concurrent.futures.ThreadPoolExecutor`). |
| 324 | |
| 325 | .. method:: BaseEventLoop.run_in_executor(executor, callback, \*args) |
| 326 | |
| 327 | Arrange for a callback to be called in the specified executor. |
| 328 | |
| 329 | *executor* is a :class:`~concurrent.futures.Executor` instance, |
| 330 | the default executor is used if *executor* is ``None``. |
| 331 | |
| 332 | .. method:: BaseEventLoop.set_default_executor(executor) |
| 333 | |
| 334 | Set the default executor used by :meth:`run_in_executor`. |
| 335 | |
| 336 | |
Victor Stinner | 3e09e32 | 2013-12-03 01:22:06 +0100 | [diff] [blame] | 337 | .. _asyncio-hello-world-callback: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 338 | |
Victor Stinner | 3e09e32 | 2013-12-03 01:22:06 +0100 | [diff] [blame] | 339 | Example: Hello World (callback) |
| 340 | ------------------------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 341 | |
| 342 | Print ``Hello World`` every two seconds, using a callback:: |
| 343 | |
| 344 | import asyncio |
| 345 | |
| 346 | def print_and_repeat(loop): |
| 347 | print('Hello World') |
| 348 | loop.call_later(2, print_and_repeat, loop) |
| 349 | |
| 350 | loop = asyncio.get_event_loop() |
| 351 | print_and_repeat(loop) |
| 352 | loop.run_forever() |
| 353 | |
Victor Stinner | 3e09e32 | 2013-12-03 01:22:06 +0100 | [diff] [blame] | 354 | .. seealso:: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 355 | |
Victor Stinner | 3e09e32 | 2013-12-03 01:22:06 +0100 | [diff] [blame] | 356 | :ref:`Hello World example using a coroutine <asyncio-hello-world-coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 357 | |