blob: 834a4e85c2ff2feebb85efb2283a2ddac903df9c [file] [log] [blame]
R David Murray6a143812013-12-20 14:37:39 -05001.. currentmodule:: asyncio
Victor Stinnerea3183f2013-12-03 01:08:00 +01002
Victor Stinner9592edb2014-02-02 15:03:02 +01003.. _asyncio-event-loop:
Victor Stinnerea3183f2013-12-03 01:08:00 +01004
Victor Stinneraea82292014-07-08 23:42:38 +02005Base Event Loop
6===============
Victor Stinnerea3183f2013-12-03 01:08:00 +01007
lf627d2c82017-07-25 17:03:51 -06008**Source code:** :source:`Lib/asyncio/events.py`
9
Victor Stinnerea3183f2013-12-03 01:08:00 +010010The event loop is the central execution device provided by :mod:`asyncio`.
Zachary Ware5e580da2015-08-27 15:54:39 -050011It provides multiple facilities, including:
Victor Stinnerea3183f2013-12-03 01:08:00 +010012
Eli Benderskyb73c8332014-02-09 06:07:47 -080013* Registering, executing and cancelling delayed calls (timeouts).
Victor Stinnerea3183f2013-12-03 01:08:00 +010014
Victor Stinner9592edb2014-02-02 15:03:02 +010015* Creating client and server :ref:`transports <asyncio-transport>` for various
Eli Benderskyb73c8332014-02-09 06:07:47 -080016 kinds of communication.
Victor Stinnerea3183f2013-12-03 01:08:00 +010017
Eli Bendersky136fea22014-02-09 06:55:58 -080018* Launching subprocesses and the associated :ref:`transports
19 <asyncio-transport>` for communication with an external program.
Victor Stinnerea3183f2013-12-03 01:08:00 +010020
Eli Benderskyb73c8332014-02-09 06:07:47 -080021* Delegating costly function calls to a pool of threads.
Victor Stinnerea3183f2013-12-03 01:08:00 +010022
Victor Stinneraea82292014-07-08 23:42:38 +020023.. class:: BaseEventLoop
Eli Bendersky136fea22014-02-09 06:55:58 -080024
Guido van Rossumf68afd82016-08-08 09:41:21 -070025 This class is an implementation detail. It is a subclass of
26 :class:`AbstractEventLoop` and may be a base class of concrete
27 event loop implementations found in :mod:`asyncio`. It should not
28 be used directly; use :class:`AbstractEventLoop` instead.
29 ``BaseEventLoop`` should not be subclassed by third-party code; the
30 internal interface is not stable.
31
32.. class:: AbstractEventLoop
33
34 Abstract base class of event loops.
Victor Stinnerea3183f2013-12-03 01:08:00 +010035
Victor Stinner83704962015-02-25 14:24:15 +010036 This class is :ref:`not thread safe <asyncio-multithreading>`.
37
Victor Stinnerea3183f2013-12-03 01:08:00 +010038Run an event loop
39-----------------
40
Guido van Rossumf68afd82016-08-08 09:41:21 -070041.. method:: AbstractEventLoop.run_forever()
Victor Stinnerea3183f2013-12-03 01:08:00 +010042
Guido van Rossum41f69f42015-11-19 13:28:47 -080043 Run until :meth:`stop` is called. If :meth:`stop` is called before
44 :meth:`run_forever()` is called, this polls the I/O selector once
45 with a timeout of zero, runs all callbacks scheduled in response to
46 I/O events (and those that were already scheduled), and then exits.
47 If :meth:`stop` is called while :meth:`run_forever` is running,
48 this will run the current batch of callbacks and then exit. Note
49 that callbacks scheduled by callbacks will not run in that case;
50 they will run the next time :meth:`run_forever` is called.
51
Guido van Rossum82f9fea2015-11-19 13:33:34 -080052 .. versionchanged:: 3.5.1
Victor Stinnerea3183f2013-12-03 01:08:00 +010053
Guido van Rossumf68afd82016-08-08 09:41:21 -070054.. method:: AbstractEventLoop.run_until_complete(future)
Victor Stinnerea3183f2013-12-03 01:08:00 +010055
Victor Stinner99c2ab42013-12-03 19:17:25 +010056 Run until the :class:`Future` is done.
Victor Stinnerea3183f2013-12-03 01:08:00 +010057
Victor Stinner530ef2f2014-07-08 12:39:10 +020058 If the argument is a :ref:`coroutine object <coroutine>`, it is wrapped by
Yury Selivanov04356e12015-06-30 22:13:22 -040059 :func:`ensure_future`.
Victor Stinnerea3183f2013-12-03 01:08:00 +010060
61 Return the Future's result, or raise its exception.
62
Guido van Rossumf68afd82016-08-08 09:41:21 -070063.. method:: AbstractEventLoop.is_running()
Victor Stinnerea3183f2013-12-03 01:08:00 +010064
65 Returns running status of event loop.
66
Guido van Rossumf68afd82016-08-08 09:41:21 -070067.. method:: AbstractEventLoop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +010068
69 Stop running the event loop.
70
Guido van Rossum41f69f42015-11-19 13:28:47 -080071 This causes :meth:`run_forever` to exit at the next suitable
72 opportunity (see there for more details).
73
Guido van Rossum82f9fea2015-11-19 13:33:34 -080074 .. versionchanged:: 3.5.1
Victor Stinnerea3183f2013-12-03 01:08:00 +010075
Guido van Rossumf68afd82016-08-08 09:41:21 -070076.. method:: AbstractEventLoop.is_closed()
Victor Stinnerbb2fc5b2014-06-10 10:23:10 +020077
78 Returns ``True`` if the event loop was closed.
79
80 .. versionadded:: 3.4.2
81
Guido van Rossumf68afd82016-08-08 09:41:21 -070082.. method:: AbstractEventLoop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +010083
Guido van Rossum41f69f42015-11-19 13:28:47 -080084 Close the event loop. The loop must not be running. Pending
85 callbacks will be lost.
Victor Stinnerea3183f2013-12-03 01:08:00 +010086
87 This clears the queues and shuts down the executor, but does not wait for
88 the executor to finish.
89
90 This is idempotent and irreversible. No other methods should be called after
91 this one.
92
Yury Selivanov03660042016-12-15 17:36:05 -050093
94.. coroutinemethod:: AbstractEventLoop.shutdown_asyncgens()
95
96 Schedule all currently open :term:`asynchronous generator` objects to
97 close with an :meth:`~agen.aclose()` call. After calling this method,
98 the event loop will issue a warning whenever a new asynchronous generator
99 is iterated. Should be used to finalize all scheduled asynchronous
100 generators reliably. Example::
101
102 try:
103 loop.run_forever()
104 finally:
105 loop.run_until_complete(loop.shutdown_asyncgens())
106 loop.close()
107
108 .. versionadded:: 3.6
109
110
Victor Stinner8464c242014-11-28 13:15:41 +0100111.. _asyncio-pass-keywords:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100112
113Calls
114-----
115
Victor Stinner8464c242014-11-28 13:15:41 +0100116Most :mod:`asyncio` functions don't accept keywords. If you want to pass
117keywords to your callback, use :func:`functools.partial`. For example,
118``loop.call_soon(functools.partial(print, "Hello", flush=True))`` will call
119``print("Hello", flush=True)``.
120
121.. note::
122 :func:`functools.partial` is better than ``lambda`` functions, because
123 :mod:`asyncio` can inspect :func:`functools.partial` object to display
124 parameters in debug mode, whereas ``lambda`` functions have a poor
125 representation.
126
Guido van Rossumf68afd82016-08-08 09:41:21 -0700127.. method:: AbstractEventLoop.call_soon(callback, \*args)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100128
Victor Stinner4d5115c2014-12-15 17:50:55 +0100129 Arrange for a callback to be called as soon as possible. The callback is
130 called after :meth:`call_soon` returns, when control returns to the event
131 loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100132
Serhiy Storchaka4ecfa452016-05-16 09:31:54 +0300133 This operates as a :abbr:`FIFO (first-in, first-out)` queue, callbacks
134 are called in the order in which they are registered. Each callback
135 will be called exactly once.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100136
137 Any positional arguments after the callback will be passed to the
138 callback when it is called.
139
Yury Selivanov1096f762015-06-25 13:49:52 -0400140 An instance of :class:`asyncio.Handle` is returned, which can be
141 used to cancel the callback.
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500142
Victor Stinner8464c242014-11-28 13:15:41 +0100143 :ref:`Use functools.partial to pass keywords to the callback
144 <asyncio-pass-keywords>`.
145
Guido van Rossumf68afd82016-08-08 09:41:21 -0700146.. method:: AbstractEventLoop.call_soon_threadsafe(callback, \*args)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100147
148 Like :meth:`call_soon`, but thread safe.
149
Victor Stinner83704962015-02-25 14:24:15 +0100150 See the :ref:`concurrency and multithreading <asyncio-multithreading>`
151 section of the documentation.
152
Victor Stinnerea3183f2013-12-03 01:08:00 +0100153
Victor Stinner45b27ed2014-02-01 02:36:43 +0100154.. _asyncio-delayed-calls:
155
Victor Stinnerea3183f2013-12-03 01:08:00 +0100156Delayed calls
157-------------
158
159The event loop has its own internal clock for computing timeouts.
160Which clock is used depends on the (platform-specific) event loop
161implementation; ideally it is a monotonic clock. This will generally be
162a different clock than :func:`time.time`.
163
Victor Stinnerfd9d3742014-02-18 09:37:43 +0100164.. note::
165
166 Timeouts (relative *delay* or absolute *when*) should not exceed one day.
167
Victor Stinner45b27ed2014-02-01 02:36:43 +0100168
Guido van Rossumf68afd82016-08-08 09:41:21 -0700169.. method:: AbstractEventLoop.call_later(delay, callback, *args)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100170
171 Arrange for the *callback* to be called after the given *delay*
172 seconds (either an int or float).
173
Yury Selivanov1096f762015-06-25 13:49:52 -0400174 An instance of :class:`asyncio.Handle` is returned, which can be
175 used to cancel the callback.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100176
177 *callback* will be called exactly once per call to :meth:`call_later`.
178 If two callbacks are scheduled for exactly the same time, it is
179 undefined which will be called first.
180
181 The optional positional *args* will be passed to the callback when it
182 is called. If you want the callback to be called with some named
183 arguments, use a closure or :func:`functools.partial`.
184
Victor Stinner8464c242014-11-28 13:15:41 +0100185 :ref:`Use functools.partial to pass keywords to the callback
186 <asyncio-pass-keywords>`.
187
Guido van Rossumf68afd82016-08-08 09:41:21 -0700188.. method:: AbstractEventLoop.call_at(when, callback, *args)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100189
190 Arrange for the *callback* to be called at the given absolute timestamp
Berker Peksagb5563992014-11-07 19:51:07 +0200191 *when* (an int or float), using the same time reference as
Guido van Rossumf68afd82016-08-08 09:41:21 -0700192 :meth:`AbstractEventLoop.time`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100193
194 This method's behavior is the same as :meth:`call_later`.
195
Yury Selivanov1096f762015-06-25 13:49:52 -0400196 An instance of :class:`asyncio.Handle` is returned, which can be
197 used to cancel the callback.
198
Victor Stinner8464c242014-11-28 13:15:41 +0100199 :ref:`Use functools.partial to pass keywords to the callback
200 <asyncio-pass-keywords>`.
201
Guido van Rossumf68afd82016-08-08 09:41:21 -0700202.. method:: AbstractEventLoop.time()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100203
204 Return the current time, as a :class:`float` value, according to the
205 event loop's internal clock.
206
Victor Stinner3e09e322013-12-03 01:22:06 +0100207.. seealso::
208
209 The :func:`asyncio.sleep` function.
210
Victor Stinnerea3183f2013-12-03 01:08:00 +0100211
Yury Selivanov950204d2016-05-16 16:23:00 -0400212Futures
213-------
214
Guido van Rossumf68afd82016-08-08 09:41:21 -0700215.. method:: AbstractEventLoop.create_future()
Yury Selivanov950204d2016-05-16 16:23:00 -0400216
217 Create an :class:`asyncio.Future` object attached to the loop.
218
219 This is a preferred way to create futures in asyncio, as event
220 loop implementations can provide alternative implementations
221 of the Future class (with better performance or instrumentation).
222
223 .. versionadded:: 3.5.2
224
225
Yury Selivanovbb961342015-06-25 11:54:34 -0400226Tasks
227-----
Victor Stinner530ef2f2014-07-08 12:39:10 +0200228
Guido van Rossumf68afd82016-08-08 09:41:21 -0700229.. method:: AbstractEventLoop.create_task(coro)
Victor Stinner530ef2f2014-07-08 12:39:10 +0200230
231 Schedule the execution of a :ref:`coroutine object <coroutine>`: wrap it in
232 a future. Return a :class:`Task` object.
233
234 Third-party event loops can use their own subclass of :class:`Task` for
235 interoperability. In this case, the result type is a subclass of
236 :class:`Task`.
237
Victor Stinner530ef2f2014-07-08 12:39:10 +0200238 .. versionadded:: 3.4.2
239
Guido van Rossumf68afd82016-08-08 09:41:21 -0700240.. method:: AbstractEventLoop.set_task_factory(factory)
Yury Selivanov71854612015-05-11 16:28:27 -0400241
242 Set a task factory that will be used by
Guido van Rossumf68afd82016-08-08 09:41:21 -0700243 :meth:`AbstractEventLoop.create_task`.
Yury Selivanov71854612015-05-11 16:28:27 -0400244
245 If *factory* is ``None`` the default task factory will be set.
246
247 If *factory* is a *callable*, it should have a signature matching
248 ``(loop, coro)``, where *loop* will be a reference to the active
249 event loop, *coro* will be a coroutine object. The callable
250 must return an :class:`asyncio.Future` compatible object.
251
252 .. versionadded:: 3.4.4
253
Guido van Rossumf68afd82016-08-08 09:41:21 -0700254.. method:: AbstractEventLoop.get_task_factory()
Yury Selivanov71854612015-05-11 16:28:27 -0400255
256 Return a task factory, or ``None`` if the default one is in use.
257
258 .. versionadded:: 3.4.4
259
Victor Stinner530ef2f2014-07-08 12:39:10 +0200260
Victor Stinnerea3183f2013-12-03 01:08:00 +0100261Creating connections
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100262--------------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100263
Andrew Svetlov51eb1c62017-12-20 20:24:43 +0200264.. coroutinemethod:: AbstractEventLoop.create_connection(protocol_factory, host=None, port=None, \*, ssl=None, family=0, proto=0, flags=0, sock=None, local_addr=None, server_hostname=None, ssl_handshake_timeout=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100265
266 Create a streaming transport connection to a given Internet *host* and
Victor Stinnera6919aa2014-02-19 13:32:34 +0100267 *port*: socket family :py:data:`~socket.AF_INET` or
268 :py:data:`~socket.AF_INET6` depending on *host* (or *family* if specified),
269 socket type :py:data:`~socket.SOCK_STREAM`. *protocol_factory* must be a
270 callable returning a :ref:`protocol <asyncio-protocol>` instance.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100271
Yury Selivanov19a44f62017-12-14 20:53:26 -0500272 This method will try to establish the connection in the background.
273 When successful, it returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100274
275 The chronological synopsis of the underlying operation is as follows:
276
Victor Stinner9592edb2014-02-02 15:03:02 +0100277 #. The connection is established, and a :ref:`transport <asyncio-transport>`
Victor Stinnerea3183f2013-12-03 01:08:00 +0100278 is created to represent it.
279
280 #. *protocol_factory* is called without arguments and must return a
Victor Stinner9592edb2014-02-02 15:03:02 +0100281 :ref:`protocol <asyncio-protocol>` instance.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100282
283 #. The protocol instance is tied to the transport, and its
284 :meth:`connection_made` method is called.
285
286 #. The coroutine returns successfully with the ``(transport, protocol)``
287 pair.
288
289 The created transport is an implementation-dependent bidirectional stream.
290
291 .. note::
292 *protocol_factory* can be any kind of callable, not necessarily
293 a class. For example, if you want to use a pre-created
294 protocol instance, you can pass ``lambda: my_protocol``.
295
Martin Panterc04fb562016-02-10 05:44:01 +0000296 Options that change how the connection is created:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100297
298 * *ssl*: if given and not false, a SSL/TLS transport is created
299 (by default a plain TCP transport is created). If *ssl* is
300 a :class:`ssl.SSLContext` object, this context is used to create
301 the transport; if *ssl* is :const:`True`, a context with some
302 unspecified default settings is used.
303
Berker Peksag9c1dba22014-09-28 00:00:58 +0300304 .. seealso:: :ref:`SSL/TLS security considerations <ssl-security>`
Antoine Pitrouc5e075f2014-03-22 18:19:11 +0100305
Victor Stinnerea3183f2013-12-03 01:08:00 +0100306 * *server_hostname*, is only for use together with *ssl*,
307 and sets or overrides the hostname that the target server's certificate
308 will be matched against. By default the value of the *host* argument
309 is used. If *host* is empty, there is no default and you must pass a
310 value for *server_hostname*. If *server_hostname* is an empty
311 string, hostname matching is disabled (which is a serious security
312 risk, allowing for man-in-the-middle-attacks).
313
314 * *family*, *proto*, *flags* are the optional address family, protocol
315 and flags to be passed through to getaddrinfo() for *host* resolution.
316 If given, these should all be integers from the corresponding
317 :mod:`socket` module constants.
318
319 * *sock*, if given, should be an existing, already connected
320 :class:`socket.socket` object to be used by the transport.
321 If *sock* is given, none of *host*, *port*, *family*, *proto*, *flags*
322 and *local_addr* should be specified.
323
324 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
325 to bind the socket to locally. The *local_host* and *local_port*
326 are looked up using getaddrinfo(), similarly to *host* and *port*.
327
Neil Aspinallf7686c12017-12-19 19:45:42 +0000328 * *ssl_handshake_timeout* is (for an SSL connection) the time in seconds
329 to wait for the SSL handshake to complete before aborting the connection.
Andrew Svetlov51eb1c62017-12-20 20:24:43 +0200330 ``10.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000331
332 .. versionadded:: 3.7
333
334 The *ssl_handshake_timeout* parameter.
335
Victor Stinner60208a12015-09-15 22:41:52 +0200336 .. versionchanged:: 3.5
337
338 On Windows with :class:`ProactorEventLoop`, SSL/TLS is now supported.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200339
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100340 .. seealso::
341
342 The :func:`open_connection` function can be used to get a pair of
343 (:class:`StreamReader`, :class:`StreamWriter`) instead of a protocol.
344
Victor Stinnerea3183f2013-12-03 01:08:00 +0100345
Guido van Rossumf68afd82016-08-08 09:41:21 -0700346.. coroutinemethod:: AbstractEventLoop.create_datagram_endpoint(protocol_factory, local_addr=None, remote_addr=None, \*, family=0, proto=0, flags=0, reuse_address=None, reuse_port=None, allow_broadcast=None, sock=None)
Victor Stinnera6919aa2014-02-19 13:32:34 +0100347
Quentin Dawansfe4ea9c2017-10-30 14:43:02 +0100348 Create datagram connection: socket family :py:data:`~socket.AF_INET`,
349 :py:data:`~socket.AF_INET6` or :py:data:`~socket.AF_UNIX` depending on
350 *host* (or *family* if specified), socket type
351 :py:data:`~socket.SOCK_DGRAM`. *protocol_factory* must be a
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700352 callable returning a :ref:`protocol <asyncio-protocol>` instance.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100353
Yury Selivanov19a44f62017-12-14 20:53:26 -0500354 This method will try to establish the connection in the background.
355 When successful, the it returns a ``(transport, protocol)`` pair.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100356
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700357 Options changing how the connection is created:
358
359 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
360 to bind the socket to locally. The *local_host* and *local_port*
361 are looked up using :meth:`getaddrinfo`.
362
363 * *remote_addr*, if given, is a ``(remote_host, remote_port)`` tuple used
364 to connect the socket to a remote address. The *remote_host* and
365 *remote_port* are looked up using :meth:`getaddrinfo`.
366
367 * *family*, *proto*, *flags* are the optional address family, protocol
368 and flags to be passed through to :meth:`getaddrinfo` for *host*
369 resolution. If given, these should all be integers from the
370 corresponding :mod:`socket` module constants.
371
372 * *reuse_address* tells the kernel to reuse a local socket in
373 TIME_WAIT state, without waiting for its natural timeout to
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300374 expire. If not specified will automatically be set to ``True`` on
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700375 UNIX.
376
377 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
378 same port as other existing endpoints are bound to, so long as they all
379 set this flag when being created. This option is not supported on Windows
380 and some UNIX's. If the :py:data:`~socket.SO_REUSEPORT` constant is not
381 defined then this capability is unsupported.
382
383 * *allow_broadcast* tells the kernel to allow this endpoint to send
384 messages to the broadcast address.
385
386 * *sock* can optionally be specified in order to use a preexisting,
387 already connected, :class:`socket.socket` object to be used by the
388 transport. If specified, *local_addr* and *remote_addr* should be omitted
389 (must be :const:`None`).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100390
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200391 On Windows with :class:`ProactorEventLoop`, this method is not supported.
392
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200393 See :ref:`UDP echo client protocol <asyncio-udp-echo-client-protocol>` and
394 :ref:`UDP echo server protocol <asyncio-udp-echo-server-protocol>` examples.
395
Victor Stinnera6919aa2014-02-19 13:32:34 +0100396
Andrew Svetlov51eb1c62017-12-20 20:24:43 +0200397.. coroutinemethod:: AbstractEventLoop.create_unix_connection(protocol_factory, path=None, \*, ssl=None, sock=None, server_hostname=None, ssl_handshake_timeout=None)
Victor Stinnera6919aa2014-02-19 13:32:34 +0100398
399 Create UNIX connection: socket family :py:data:`~socket.AF_UNIX`, socket
400 type :py:data:`~socket.SOCK_STREAM`. The :py:data:`~socket.AF_UNIX` socket
401 family is used to communicate between processes on the same machine
402 efficiently.
403
Yury Selivanov19a44f62017-12-14 20:53:26 -0500404 This method will try to establish the connection in the background.
405 When successful, the it returns a ``(transport, protocol)`` pair.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100406
Guido van Rossum9e80eeb2016-11-03 14:17:25 -0700407 *path* is the name of a UNIX domain socket, and is required unless a *sock*
Yury Selivanov423fd362017-11-20 17:26:28 -0500408 parameter is specified. Abstract UNIX sockets, :class:`str`,
409 :class:`bytes`, and :class:`~pathlib.Path` paths are supported.
Guido van Rossum9e80eeb2016-11-03 14:17:25 -0700410
Guido van Rossumf68afd82016-08-08 09:41:21 -0700411 See the :meth:`AbstractEventLoop.create_connection` method for parameters.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100412
413 Availability: UNIX.
414
Neil Aspinallf7686c12017-12-19 19:45:42 +0000415 .. versionadded:: 3.7
416
417 The *ssl_handshake_timeout* parameter.
418
Yury Selivanov423fd362017-11-20 17:26:28 -0500419 .. versionchanged:: 3.7
420
421 The *path* parameter can now be a :class:`~pathlib.Path` object.
422
Victor Stinnera6919aa2014-02-19 13:32:34 +0100423
Victor Stinnerea3183f2013-12-03 01:08:00 +0100424Creating listening connections
425------------------------------
426
Yury Selivanovc9070d02018-01-25 18:08:09 -0500427.. coroutinemethod:: AbstractEventLoop.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, reuse_port=None, ssl_handshake_timeout=None, start_serving=True)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100428
Victor Stinner33f6abe2014-10-12 20:36:04 +0200429 Create a TCP server (socket type :data:`~socket.SOCK_STREAM`) bound to
430 *host* and *port*.
431
432 Return a :class:`Server` object, its :attr:`~Server.sockets` attribute
433 contains created sockets. Use the :meth:`Server.close` method to stop the
434 server: close listening sockets.
435
436 Parameters:
437
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200438 * The *host* parameter can be a string, in that case the TCP server is
439 bound to *host* and *port*. The *host* parameter can also be a sequence
440 of strings and in that case the TCP server is bound to all hosts of the
441 sequence. If *host* is an empty string or ``None``, all interfaces are
442 assumed and a list of multiple sockets will be returned (most likely one
443 for IPv4 and another one for IPv6).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200444
445 * *family* can be set to either :data:`socket.AF_INET` or
446 :data:`~socket.AF_INET6` to force the socket to use IPv4 or IPv6. If not set
447 it will be determined from host (defaults to :data:`socket.AF_UNSPEC`).
448
449 * *flags* is a bitmask for :meth:`getaddrinfo`.
450
451 * *sock* can optionally be specified in order to use a preexisting
452 socket object. If specified, *host* and *port* should be omitted (must be
453 :const:`None`).
454
455 * *backlog* is the maximum number of queued connections passed to
456 :meth:`~socket.socket.listen` (defaults to 100).
457
458 * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over the
459 accepted connections.
460
461 * *reuse_address* tells the kernel to reuse a local socket in
462 TIME_WAIT state, without waiting for its natural timeout to
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300463 expire. If not specified will automatically be set to ``True`` on
Victor Stinner33f6abe2014-10-12 20:36:04 +0200464 UNIX.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100465
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700466 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
467 same port as other existing endpoints are bound to, so long as they all
468 set this flag when being created. This option is not supported on
469 Windows.
470
Neil Aspinallf7686c12017-12-19 19:45:42 +0000471 * *ssl_handshake_timeout* is (for an SSL server) the time in seconds to wait
472 for the SSL handshake to complete before aborting the connection.
Andrew Svetlov51eb1c62017-12-20 20:24:43 +0200473 ``10.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000474
Yury Selivanovc9070d02018-01-25 18:08:09 -0500475 * *start_serving* set to ``True`` (the default) causes the created server
476 to start accepting connections immediately. When set to ``False``,
477 the user should await on :meth:`Server.start_serving` or
478 :meth:`Server.serve_forever` to make the server to start accepting
479 connections.
480
Neil Aspinallf7686c12017-12-19 19:45:42 +0000481 .. versionadded:: 3.7
482
Yury Selivanovc9070d02018-01-25 18:08:09 -0500483 *ssl_handshake_timeout* and *start_serving* parameters.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000484
Victor Stinner60208a12015-09-15 22:41:52 +0200485 .. versionchanged:: 3.5
486
487 On Windows with :class:`ProactorEventLoop`, SSL/TLS is now supported.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200488
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100489 .. seealso::
490
491 The function :func:`start_server` creates a (:class:`StreamReader`,
492 :class:`StreamWriter`) pair and calls back a function with this pair.
493
Victor Stinner7b58a2b2015-09-21 18:41:05 +0200494 .. versionchanged:: 3.5.1
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200495
496 The *host* parameter can now be a sequence of strings.
497
Victor Stinnerea3183f2013-12-03 01:08:00 +0100498
Yury Selivanovc9070d02018-01-25 18:08:09 -0500499.. coroutinemethod:: AbstractEventLoop.create_unix_server(protocol_factory, path=None, \*, sock=None, backlog=100, ssl=None, ssl_handshake_timeout=None, start_serving=True)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100500
Guido van Rossumf68afd82016-08-08 09:41:21 -0700501 Similar to :meth:`AbstractEventLoop.create_server`, but specific to the
Victor Stinnera6919aa2014-02-19 13:32:34 +0100502 socket family :py:data:`~socket.AF_UNIX`.
503
Yury Selivanov423fd362017-11-20 17:26:28 -0500504 *path* is the name of a UNIX domain socket, and is required unless a *sock*
505 parameter is specified. Abstract UNIX sockets, :class:`str`,
506 :class:`bytes`, and :class:`~pathlib.Path` paths are supported.
507
Victor Stinnera6919aa2014-02-19 13:32:34 +0100508 Availability: UNIX.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100509
Neil Aspinallf7686c12017-12-19 19:45:42 +0000510 .. versionadded:: 3.7
511
512 The *ssl_handshake_timeout* parameter.
513
Yury Selivanov423fd362017-11-20 17:26:28 -0500514 .. versionchanged:: 3.7
515
516 The *path* parameter can now be a :class:`~pathlib.Path` object.
517
Andrew Svetlov51eb1c62017-12-20 20:24:43 +0200518.. coroutinemethod:: BaseEventLoop.connect_accepted_socket(protocol_factory, sock, \*, ssl=None, ssl_handshake_timeout=None)
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500519
520 Handle an accepted connection.
521
522 This is used by servers that accept connections outside of
523 asyncio but that use asyncio to handle them.
524
525 Parameters:
526
527 * *sock* is a preexisting socket object returned from an ``accept``
528 call.
529
530 * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over the
531 accepted connections.
532
Neil Aspinallf7686c12017-12-19 19:45:42 +0000533 * *ssl_handshake_timeout* is (for an SSL connection) the time in seconds to
534 wait for the SSL handshake to complete before aborting the connection.
Andrew Svetlov51eb1c62017-12-20 20:24:43 +0200535 ``10.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000536
Yury Selivanov19a44f62017-12-14 20:53:26 -0500537 When completed it returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100538
Neil Aspinallf7686c12017-12-19 19:45:42 +0000539 .. versionadded:: 3.7
540
541 The *ssl_handshake_timeout* parameter.
542
AraHaan431665b2017-11-21 11:06:26 -0500543 .. versionadded:: 3.5.3
544
545
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500546TLS Upgrade
547-----------
548
549.. coroutinemethod:: AbstractEventLoop.start_tls(transport, protocol, sslcontext, \*, server_side=False, server_hostname=None, ssl_handshake_timeout=None)
550
551 Upgrades an existing connection to TLS.
552
553 Returns a new transport instance, that the *protocol* must start using
554 immediately after the *await*. The *transport* instance passed to
555 the *start_tls* method should never be used again.
556
557 Parameters:
558
559 * *transport* and *protocol* instances that methods like
560 :meth:`~AbstractEventLoop.create_server` and
561 :meth:`~AbstractEventLoop.create_connection` return.
562
563 * *sslcontext*: a configured instance of :class:`~ssl.SSLContext`.
564
565 * *server_side* pass ``True`` when a server-side connection is being
566 upgraded (like the one created by :meth:`~AbstractEventLoop.create_server`).
567
568 * *server_hostname*: sets or overrides the host name that the target
569 server's certificate will be matched against.
570
571 * *ssl_handshake_timeout* is (for an SSL connection) the time in seconds to
572 wait for the SSL handshake to complete before aborting the connection.
573 ``10.0`` seconds if ``None`` (default).
574
575 .. versionadded:: 3.7
576
577
Victor Stinnerc1567df2014-02-08 23:22:58 +0100578Watch file descriptors
579----------------------
580
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200581On Windows with :class:`SelectorEventLoop`, only socket handles are supported
582(ex: pipe file descriptors are not supported).
583
584On Windows with :class:`ProactorEventLoop`, these methods are not supported.
585
Guido van Rossumf68afd82016-08-08 09:41:21 -0700586.. method:: AbstractEventLoop.add_reader(fd, callback, \*args)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100587
588 Start watching the file descriptor for read availability and then call the
589 *callback* with specified arguments.
590
Victor Stinner8464c242014-11-28 13:15:41 +0100591 :ref:`Use functools.partial to pass keywords to the callback
592 <asyncio-pass-keywords>`.
593
Guido van Rossumf68afd82016-08-08 09:41:21 -0700594.. method:: AbstractEventLoop.remove_reader(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100595
596 Stop watching the file descriptor for read availability.
597
Guido van Rossumf68afd82016-08-08 09:41:21 -0700598.. method:: AbstractEventLoop.add_writer(fd, callback, \*args)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100599
600 Start watching the file descriptor for write availability and then call the
601 *callback* with specified arguments.
602
Victor Stinner8464c242014-11-28 13:15:41 +0100603 :ref:`Use functools.partial to pass keywords to the callback
604 <asyncio-pass-keywords>`.
605
Guido van Rossumf68afd82016-08-08 09:41:21 -0700606.. method:: AbstractEventLoop.remove_writer(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100607
608 Stop watching the file descriptor for write availability.
609
Victor Stinner04e6df32014-10-11 16:16:27 +0200610The :ref:`watch a file descriptor for read events <asyncio-watch-read-event>`
Guido van Rossumf68afd82016-08-08 09:41:21 -0700611example uses the low-level :meth:`AbstractEventLoop.add_reader` method to register
Victor Stinner04e6df32014-10-11 16:16:27 +0200612the file descriptor of a socket.
613
Victor Stinnerc1567df2014-02-08 23:22:58 +0100614
615Low-level socket operations
616---------------------------
617
Guido van Rossumf68afd82016-08-08 09:41:21 -0700618.. coroutinemethod:: AbstractEventLoop.sock_recv(sock, nbytes)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100619
Yury Selivanov55c50842016-06-08 12:48:15 -0400620 Receive data from the socket. Modeled after blocking
621 :meth:`socket.socket.recv` method.
622
623 The return value is a bytes object
Victor Stinnerc1567df2014-02-08 23:22:58 +0100624 representing the data received. The maximum amount of data to be received
625 at once is specified by *nbytes*.
626
Victor Stinnerd84fd732014-08-26 01:01:59 +0200627 With :class:`SelectorEventLoop` event loop, the socket *sock* must be
628 non-blocking.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200629
Yury Selivanov19a44f62017-12-14 20:53:26 -0500630 .. versionchanged:: 3.7
631 Even though the method was always documented as a coroutine
632 method, before Python 3.7 it returned a :class:`Future`.
633 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100634
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200635.. coroutinemethod:: AbstractEventLoop.sock_recv_into(sock, buf)
636
637 Receive data from the socket. Modeled after blocking
638 :meth:`socket.socket.recv_into` method.
639
640 The received data is written into *buf* (a writable buffer).
641 The return value is the number of bytes written.
642
643 With :class:`SelectorEventLoop` event loop, the socket *sock* must be
644 non-blocking.
645
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200646 .. versionadded:: 3.7
647
Guido van Rossumf68afd82016-08-08 09:41:21 -0700648.. coroutinemethod:: AbstractEventLoop.sock_sendall(sock, data)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100649
Yury Selivanov55c50842016-06-08 12:48:15 -0400650 Send data to the socket. Modeled after blocking
651 :meth:`socket.socket.sendall` method.
652
653 The socket must be connected to a remote socket.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100654 This method continues to send data from *data* until either all data has
655 been sent or an error occurs. ``None`` is returned on success. On error,
656 an exception is raised, and there is no way to determine how much data, if
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500657 any, was successfully processed by the receiving end of the connection.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100658
Victor Stinnerd84fd732014-08-26 01:01:59 +0200659 With :class:`SelectorEventLoop` event loop, the socket *sock* must be
660 non-blocking.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200661
Yury Selivanov19a44f62017-12-14 20:53:26 -0500662 .. versionchanged:: 3.7
663 Even though the method was always documented as a coroutine
664 method, before Python 3.7 it returned an :class:`Future`.
665 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100666
Guido van Rossumf68afd82016-08-08 09:41:21 -0700667.. coroutinemethod:: AbstractEventLoop.sock_connect(sock, address)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100668
Yury Selivanov55c50842016-06-08 12:48:15 -0400669 Connect to a remote socket at *address*. Modeled after
670 blocking :meth:`socket.socket.connect` method.
Victor Stinner1b0580b2014-02-13 09:24:37 +0100671
Victor Stinnerd84fd732014-08-26 01:01:59 +0200672 With :class:`SelectorEventLoop` event loop, the socket *sock* must be
673 non-blocking.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200674
Yury Selivanov55c50842016-06-08 12:48:15 -0400675 .. versionchanged:: 3.5.2
676 ``address`` no longer needs to be resolved. ``sock_connect``
677 will try to check if the *address* is already resolved by calling
678 :func:`socket.inet_pton`. If not,
Guido van Rossumf68afd82016-08-08 09:41:21 -0700679 :meth:`AbstractEventLoop.getaddrinfo` will be used to resolve the
Yury Selivanov55c50842016-06-08 12:48:15 -0400680 *address*.
681
Victor Stinnerc1567df2014-02-08 23:22:58 +0100682 .. seealso::
683
Guido van Rossumf68afd82016-08-08 09:41:21 -0700684 :meth:`AbstractEventLoop.create_connection`
Yury Selivanov55c50842016-06-08 12:48:15 -0400685 and :func:`asyncio.open_connection() <open_connection>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100686
687
Guido van Rossumf68afd82016-08-08 09:41:21 -0700688.. coroutinemethod:: AbstractEventLoop.sock_accept(sock)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100689
Yury Selivanov55c50842016-06-08 12:48:15 -0400690 Accept a connection. Modeled after blocking
691 :meth:`socket.socket.accept`.
692
693 The socket must be bound to an address and listening
Victor Stinnerc1567df2014-02-08 23:22:58 +0100694 for connections. The return value is a pair ``(conn, address)`` where *conn*
695 is a *new* socket object usable to send and receive data on the connection,
696 and *address* is the address bound to the socket on the other end of the
697 connection.
698
Victor Stinnerec2ce092014-07-29 23:12:22 +0200699 The socket *sock* must be non-blocking.
700
Yury Selivanov19a44f62017-12-14 20:53:26 -0500701 .. versionchanged:: 3.7
702 Even though the method was always documented as a coroutine
703 method, before Python 3.7 it returned a :class:`Future`.
704 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100705
706 .. seealso::
707
Guido van Rossumf68afd82016-08-08 09:41:21 -0700708 :meth:`AbstractEventLoop.create_server` and :func:`start_server`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100709
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200710.. coroutinemethod:: AbstractEventLoop.sock_sendfile(sock, file, \
711 offset=0, count=None, \
712 *, fallback=True)
713
714 Send a file using high-performance :mod:`os.sendfile` if possible
715 and return the total number of bytes which were sent.
716
717 Asynchronous version of :meth:`socket.socket.sendfile`.
718
719 *sock* must be non-blocking :class:`~socket.socket` of
720 :const:`socket.SOCK_STREAM` type.
721
722 *file* must be a regular file object opened in binary mode.
723
724 *offset* tells from where to start reading the file. If specified,
725 *count* is the total number of bytes to transmit as opposed to
726 sending the file until EOF is reached. File position is updated on
727 return or also in case of error in which case :meth:`file.tell()
728 <io.IOBase.tell>` can be used to figure out the number of bytes
729 which were sent.
730
731 *fallback* set to ``True`` makes asyncio to manually read and send
732 the file when the platform does not support the sendfile syscall
733 (e.g. Windows or SSL socket on Unix).
734
Andrew Svetlov7464e872018-01-19 20:04:29 +0200735 Raise :exc:`SendfileNotAvailableError` if the system does not support
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200736 *sendfile* syscall and *fallback* is ``False``.
737
738 .. versionadded:: 3.7
739
Victor Stinnerc1567df2014-02-08 23:22:58 +0100740
741Resolve host name
742-----------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100743
Guido van Rossumf68afd82016-08-08 09:41:21 -0700744.. coroutinemethod:: AbstractEventLoop.getaddrinfo(host, port, \*, family=0, type=0, proto=0, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100745
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500746 This method is a :ref:`coroutine <coroutine>`, similar to
747 :meth:`socket.getaddrinfo` function but non-blocking.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100748
Guido van Rossumf68afd82016-08-08 09:41:21 -0700749.. coroutinemethod:: AbstractEventLoop.getnameinfo(sockaddr, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100750
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500751 This method is a :ref:`coroutine <coroutine>`, similar to
752 :meth:`socket.getnameinfo` function but non-blocking.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100753
754
Victor Stinner984600f2014-03-25 09:40:26 +0100755Connect pipes
756-------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100757
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200758On Windows with :class:`SelectorEventLoop`, these methods are not supported.
759Use :class:`ProactorEventLoop` to support pipes on Windows.
760
Guido van Rossumf68afd82016-08-08 09:41:21 -0700761.. coroutinemethod:: AbstractEventLoop.connect_read_pipe(protocol_factory, pipe)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100762
Victor Stinnerd84fd732014-08-26 01:01:59 +0200763 Register read pipe in eventloop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100764
765 *protocol_factory* should instantiate object with :class:`Protocol`
Victor Stinnera5b257a2014-05-29 00:14:03 +0200766 interface. *pipe* is a :term:`file-like object <file object>`.
767 Return pair ``(transport, protocol)``, where *transport* supports the
Victor Stinnerea3183f2013-12-03 01:08:00 +0100768 :class:`ReadTransport` interface.
769
Victor Stinnerd84fd732014-08-26 01:01:59 +0200770 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
771 non-blocking mode.
772
Guido van Rossumf68afd82016-08-08 09:41:21 -0700773.. coroutinemethod:: AbstractEventLoop.connect_write_pipe(protocol_factory, pipe)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100774
775 Register write pipe in eventloop.
776
777 *protocol_factory* should instantiate object with :class:`BaseProtocol`
Victor Stinner2cef3002014-10-23 22:38:46 +0200778 interface. *pipe* is :term:`file-like object <file object>`.
779 Return pair ``(transport, protocol)``, where *transport* supports
Victor Stinnerea3183f2013-12-03 01:08:00 +0100780 :class:`WriteTransport` interface.
781
Victor Stinnerd84fd732014-08-26 01:01:59 +0200782 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
783 non-blocking mode.
784
Victor Stinner08444382014-02-02 22:43:39 +0100785.. seealso::
786
Guido van Rossumf68afd82016-08-08 09:41:21 -0700787 The :meth:`AbstractEventLoop.subprocess_exec` and
788 :meth:`AbstractEventLoop.subprocess_shell` methods.
Victor Stinner08444382014-02-02 22:43:39 +0100789
Victor Stinnerea3183f2013-12-03 01:08:00 +0100790
Victor Stinner8b863482014-01-27 10:07:50 +0100791UNIX signals
792------------
793
794Availability: UNIX only.
795
Guido van Rossumf68afd82016-08-08 09:41:21 -0700796.. method:: AbstractEventLoop.add_signal_handler(signum, callback, \*args)
Victor Stinner8b863482014-01-27 10:07:50 +0100797
798 Add a handler for a signal.
799
800 Raise :exc:`ValueError` if the signal number is invalid or uncatchable.
801 Raise :exc:`RuntimeError` if there is a problem setting up the handler.
802
Victor Stinner8464c242014-11-28 13:15:41 +0100803 :ref:`Use functools.partial to pass keywords to the callback
804 <asyncio-pass-keywords>`.
805
Guido van Rossumf68afd82016-08-08 09:41:21 -0700806.. method:: AbstractEventLoop.remove_signal_handler(sig)
Victor Stinner8b863482014-01-27 10:07:50 +0100807
808 Remove a handler for a signal.
809
810 Return ``True`` if a signal handler was removed, ``False`` if not.
811
812.. seealso::
813
814 The :mod:`signal` module.
815
816
Victor Stinnerea3183f2013-12-03 01:08:00 +0100817Executor
818--------
819
820Call a function in an :class:`~concurrent.futures.Executor` (pool of threads or
821pool of processes). By default, an event loop uses a thread pool executor
822(:class:`~concurrent.futures.ThreadPoolExecutor`).
823
Guido van Rossumf68afd82016-08-08 09:41:21 -0700824.. coroutinemethod:: AbstractEventLoop.run_in_executor(executor, func, \*args)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100825
Andrew Svetlov1c62b522015-10-01 09:48:08 +0300826 Arrange for a *func* to be called in the specified executor.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100827
Larry Hastings3732ed22014-03-15 21:13:56 -0700828 The *executor* argument should be an :class:`~concurrent.futures.Executor`
829 instance. The default executor is used if *executor* is ``None``.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100830
Andrew Svetlov1c62b522015-10-01 09:48:08 +0300831 :ref:`Use functools.partial to pass keywords to the *func*
Victor Stinner8464c242014-11-28 13:15:41 +0100832 <asyncio-pass-keywords>`.
833
Yury Selivanove8a60452016-10-21 17:40:42 -0400834 .. versionchanged:: 3.5.3
835 :meth:`BaseEventLoop.run_in_executor` no longer configures the
836 ``max_workers`` of the thread pool executor it creates, instead
837 leaving it up to the thread pool executor
838 (:class:`~concurrent.futures.ThreadPoolExecutor`) to set the
839 default.
840
Yury Selivanov19a44f62017-12-14 20:53:26 -0500841 .. versionchanged:: 3.7
842 Even though the method was always documented as a coroutine
843 method, before Python 3.7 it returned a :class:`Future`.
844 Since Python 3.7, this is an ``async def`` method.
845
Guido van Rossumf68afd82016-08-08 09:41:21 -0700846.. method:: AbstractEventLoop.set_default_executor(executor)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100847
848 Set the default executor used by :meth:`run_in_executor`.
849
850
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500851Error Handling API
852------------------
853
Martin Panterc04fb562016-02-10 05:44:01 +0000854Allows customizing how exceptions are handled in the event loop.
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500855
Guido van Rossumf68afd82016-08-08 09:41:21 -0700856.. method:: AbstractEventLoop.set_exception_handler(handler)
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500857
858 Set *handler* as the new event loop exception handler.
859
860 If *handler* is ``None``, the default exception handler will
861 be set.
862
863 If *handler* is a callable object, it should have a
864 matching signature to ``(loop, context)``, where ``loop``
865 will be a reference to the active event loop, ``context``
866 will be a ``dict`` object (see :meth:`call_exception_handler`
867 documentation for details about context).
868
Guido van Rossumf68afd82016-08-08 09:41:21 -0700869.. method:: AbstractEventLoop.get_exception_handler()
Yury Selivanov950204d2016-05-16 16:23:00 -0400870
871 Return the exception handler, or ``None`` if the default one
872 is in use.
873
874 .. versionadded:: 3.5.2
875
Guido van Rossumf68afd82016-08-08 09:41:21 -0700876.. method:: AbstractEventLoop.default_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500877
878 Default exception handler.
879
880 This is called when an exception occurs and no exception
881 handler is set, and can be called by a custom exception
882 handler that wants to defer to the default behavior.
883
884 *context* parameter has the same meaning as in
885 :meth:`call_exception_handler`.
886
Guido van Rossumf68afd82016-08-08 09:41:21 -0700887.. method:: AbstractEventLoop.call_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500888
889 Call the current event loop exception handler.
890
891 *context* is a ``dict`` object containing the following keys
892 (new keys may be introduced later):
893
894 * 'message': Error message;
895 * 'exception' (optional): Exception object;
896 * 'future' (optional): :class:`asyncio.Future` instance;
897 * 'handle' (optional): :class:`asyncio.Handle` instance;
898 * 'protocol' (optional): :ref:`Protocol <asyncio-protocol>` instance;
899 * 'transport' (optional): :ref:`Transport <asyncio-transport>` instance;
900 * 'socket' (optional): :class:`socket.socket` instance.
901
902 .. note::
903
904 Note: this method should not be overloaded in subclassed
905 event loops. For any custom exception handling, use
906 :meth:`set_exception_handler()` method.
907
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100908Debug mode
909----------
910
Guido van Rossumf68afd82016-08-08 09:41:21 -0700911.. method:: AbstractEventLoop.get_debug()
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100912
Victor Stinner7b7120e2014-06-23 00:12:14 +0200913 Get the debug mode (:class:`bool`) of the event loop.
914
915 The default value is ``True`` if the environment variable
916 :envvar:`PYTHONASYNCIODEBUG` is set to a non-empty string, ``False``
917 otherwise.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100918
Victor Stinner64d750b2014-06-18 03:25:23 +0200919 .. versionadded:: 3.4.2
920
Guido van Rossumf68afd82016-08-08 09:41:21 -0700921.. method:: AbstractEventLoop.set_debug(enabled: bool)
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100922
923 Set the debug mode of the event loop.
924
Victor Stinner64d750b2014-06-18 03:25:23 +0200925 .. versionadded:: 3.4.2
926
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100927.. seealso::
928
Victor Stinner62511fd2014-06-23 00:36:11 +0200929 The :ref:`debug mode of asyncio <asyncio-debug-mode>`.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100930
Victor Stinner8c462c52014-01-24 18:11:43 +0100931Server
932------
933
Victor Stinner8ebeb032014-07-11 23:47:40 +0200934.. class:: Server
Victor Stinner8c462c52014-01-24 18:11:43 +0100935
Victor Stinner8ebeb032014-07-11 23:47:40 +0200936 Server listening on sockets.
937
Yury Selivanovc9070d02018-01-25 18:08:09 -0500938 Object created by :meth:`AbstractEventLoop.create_server`,
939 :meth:`AbstractEventLoop.create_unix_server`, :func:`start_server`,
940 and :func:`start_unix_server` functions. Don't instantiate the class
941 directly.
942
943 *Server* objects are asynchronous context managers. When used in an
944 ``async with`` statement, it's guaranteed that the Server object is
945 closed and not accepting new connections when the ``async with``
946 statement is completed::
947
948 srv = await loop.create_server(...)
949
950 async with srv:
951 # some code
952
953 # At this point, srv is closed and no longer accepts new connections.
954
955
956 .. versionchanged:: 3.7
957 Server object is an asynchronous context manager since Python 3.7.
Victor Stinner8c462c52014-01-24 18:11:43 +0100958
959 .. method:: close()
960
Victor Stinner4bfb14a2014-07-12 03:20:24 +0200961 Stop serving: close listening sockets and set the :attr:`sockets`
962 attribute to ``None``.
963
Berker Peksag49c9edf2016-01-20 07:14:22 +0200964 The sockets that represent existing incoming client connections are left
965 open.
Victor Stinner8ebeb032014-07-11 23:47:40 +0200966
Berker Peksag49c9edf2016-01-20 07:14:22 +0200967 The server is closed asynchronously, use the :meth:`wait_closed`
968 coroutine to wait until the server is closed.
Victor Stinner8c462c52014-01-24 18:11:43 +0100969
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)1634fc22017-12-30 20:39:32 +0530970 .. method:: get_loop()
971
972 Gives the event loop associated with the server object.
973
974 .. versionadded:: 3.7
975
Yury Selivanovc9070d02018-01-25 18:08:09 -0500976 .. coroutinemethod:: start_serving()
977
978 Start accepting connections.
979
980 This method is idempotent, so it can be called when
981 the server is already being serving.
982
983 The new *start_serving* keyword-only parameter to
984 :meth:`AbstractEventLoop.create_server` and
985 :meth:`asyncio.start_server` allows to create a Server object
986 that is not accepting connections right away. In which case
987 this method, or :meth:`Server.serve_forever` can be used
988 to make the Server object to start accepting connections.
989
990 .. versionadded:: 3.7
991
992 .. coroutinemethod:: serve_forever()
993
994 Start accepting connections until the coroutine is cancelled.
995 Cancellation of ``serve_forever`` task causes the server
996 to be closed.
997
998 This method can be called if the server is already accepting
999 connections. Only one ``serve_forever`` task can exist per
1000 one *Server* object.
1001
1002 Example::
1003
1004 async def client_connected(reader, writer):
1005 # Communicate with the client with
1006 # reader/writer streams. For example:
1007 await reader.readline()
1008
1009 async def main(host, port):
1010 srv = await asyncio.start_server(
1011 client_connected, host, port)
1012 await loop.serve_forever()
1013
1014 asyncio.run(main('127.0.0.1', 0))
1015
1016 .. versionadded:: 3.7
1017
1018 .. method:: is_serving()
1019
1020 Return ``True`` if the server is accepting new connections.
1021
1022 .. versionadded:: 3.7
1023
Victor Stinnerbdd574d2015-02-12 22:49:18 +01001024 .. coroutinemethod:: wait_closed()
Victor Stinner8c462c52014-01-24 18:11:43 +01001025
Victor Stinner8ebeb032014-07-11 23:47:40 +02001026 Wait until the :meth:`close` method completes.
1027
Victor Stinner8ebeb032014-07-11 23:47:40 +02001028 .. attribute:: sockets
1029
1030 List of :class:`socket.socket` objects the server is listening to, or
1031 ``None`` if the server is closed.
Victor Stinner8c462c52014-01-24 18:11:43 +01001032
Yury Selivanovc9070d02018-01-25 18:08:09 -05001033 .. versionchanged:: 3.7
1034 Prior to Python 3.7 ``Server.sockets`` used to return the
1035 internal list of server's sockets directly. In 3.7 a copy
1036 of that list is returned.
1037
Victor Stinner8c462c52014-01-24 18:11:43 +01001038
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001039Handle
1040------
1041
1042.. class:: Handle
1043
Guido van Rossumf68afd82016-08-08 09:41:21 -07001044 A callback wrapper object returned by :func:`AbstractEventLoop.call_soon`,
1045 :func:`AbstractEventLoop.call_soon_threadsafe`, :func:`AbstractEventLoop.call_later`,
1046 and :func:`AbstractEventLoop.call_at`.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001047
1048 .. method:: cancel()
1049
Yury Selivanov1096f762015-06-25 13:49:52 -04001050 Cancel the call. If the callback is already canceled or executed,
1051 this method has no effect.
Victor Stinneraea82292014-07-08 23:42:38 +02001052
Marat Sharafutdinov69cfed12017-11-07 12:06:05 +03001053 .. method:: cancelled()
1054
1055 Return ``True`` if the call was cancelled.
1056
1057 .. versionadded:: 3.7
1058
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001059
Andrew Svetlov7464e872018-01-19 20:04:29 +02001060SendfileNotAvailableError
1061-------------------------
1062
1063
1064.. exception:: SendfileNotAvailableError
1065
1066 Sendfile syscall is not available, subclass of :exc:`RuntimeError`.
1067
1068 Raised if the OS does not support senfile syscall for
1069 given socket or file type.
1070
1071
Victor Stinner6888b962014-10-11 16:15:58 +02001072Event loop examples
Victor Stinnera092a612015-01-09 15:58:41 +01001073-------------------
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001074
Victor Stinner3e09e322013-12-03 01:22:06 +01001075.. _asyncio-hello-world-callback:
Victor Stinnerea3183f2013-12-03 01:08:00 +01001076
Victor Stinner7f314ed2014-10-15 18:49:16 +02001077Hello World with call_soon()
Victor Stinnera092a612015-01-09 15:58:41 +01001078^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +01001079
Guido van Rossumf68afd82016-08-08 09:41:21 -07001080Example using the :meth:`AbstractEventLoop.call_soon` method to schedule a
Victor Stinner7f314ed2014-10-15 18:49:16 +02001081callback. The callback displays ``"Hello World"`` and then stops the event
1082loop::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001083
1084 import asyncio
1085
Victor Stinner7f314ed2014-10-15 18:49:16 +02001086 def hello_world(loop):
Victor Stinnerea3183f2013-12-03 01:08:00 +01001087 print('Hello World')
Victor Stinner7f314ed2014-10-15 18:49:16 +02001088 loop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001089
1090 loop = asyncio.get_event_loop()
Victor Stinner7f314ed2014-10-15 18:49:16 +02001091
1092 # Schedule a call to hello_world()
1093 loop.call_soon(hello_world, loop)
1094
1095 # Blocking call interrupted by loop.stop()
1096 loop.run_forever()
1097 loop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001098
Victor Stinner3e09e322013-12-03 01:22:06 +01001099.. seealso::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001100
Victor Stinner6888b962014-10-11 16:15:58 +02001101 The :ref:`Hello World coroutine <asyncio-hello-world-coroutine>` example
1102 uses a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001103
Victor Stinner8b863482014-01-27 10:07:50 +01001104
Victor Stinner7f314ed2014-10-15 18:49:16 +02001105.. _asyncio-date-callback:
1106
1107Display the current date with call_later()
Victor Stinnera092a612015-01-09 15:58:41 +01001108^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner7f314ed2014-10-15 18:49:16 +02001109
1110Example of callback displaying the current date every second. The callback uses
Guido van Rossumf68afd82016-08-08 09:41:21 -07001111the :meth:`AbstractEventLoop.call_later` method to reschedule itself during 5
Victor Stinner7f314ed2014-10-15 18:49:16 +02001112seconds, and then stops the event loop::
1113
1114 import asyncio
1115 import datetime
1116
1117 def display_date(end_time, loop):
1118 print(datetime.datetime.now())
1119 if (loop.time() + 1.0) < end_time:
1120 loop.call_later(1, display_date, end_time, loop)
1121 else:
1122 loop.stop()
1123
1124 loop = asyncio.get_event_loop()
1125
1126 # Schedule the first call to display_date()
1127 end_time = loop.time() + 5.0
1128 loop.call_soon(display_date, end_time, loop)
1129
1130 # Blocking call interrupted by loop.stop()
1131 loop.run_forever()
1132 loop.close()
1133
1134.. seealso::
1135
1136 The :ref:`coroutine displaying the current date
1137 <asyncio-date-coroutine>` example uses a :ref:`coroutine
1138 <coroutine>`.
1139
1140
Victor Stinner04e6df32014-10-11 16:16:27 +02001141.. _asyncio-watch-read-event:
Victor Stinner8b863482014-01-27 10:07:50 +01001142
Victor Stinner04e6df32014-10-11 16:16:27 +02001143Watch a file descriptor for read events
Victor Stinnera092a612015-01-09 15:58:41 +01001144^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001145
1146Wait until a file descriptor received some data using the
Guido van Rossumf68afd82016-08-08 09:41:21 -07001147:meth:`AbstractEventLoop.add_reader` method and then close the event loop::
Victor Stinner04e6df32014-10-11 16:16:27 +02001148
1149 import asyncio
Victor Stinnerac577d72017-11-28 21:33:20 +01001150 from socket import socketpair
Victor Stinner04e6df32014-10-11 16:16:27 +02001151
1152 # Create a pair of connected file descriptors
Victor Stinnerccd8e342014-10-11 16:30:02 +02001153 rsock, wsock = socketpair()
Victor Stinner04e6df32014-10-11 16:16:27 +02001154 loop = asyncio.get_event_loop()
1155
1156 def reader():
1157 data = rsock.recv(100)
1158 print("Received:", data.decode())
Victor Stinner2cef3002014-10-23 22:38:46 +02001159 # We are done: unregister the file descriptor
Victor Stinner04e6df32014-10-11 16:16:27 +02001160 loop.remove_reader(rsock)
1161 # Stop the event loop
1162 loop.stop()
1163
Victor Stinner2cef3002014-10-23 22:38:46 +02001164 # Register the file descriptor for read event
Victor Stinner04e6df32014-10-11 16:16:27 +02001165 loop.add_reader(rsock, reader)
1166
1167 # Simulate the reception of data from the network
1168 loop.call_soon(wsock.send, 'abc'.encode())
1169
1170 # Run the event loop
1171 loop.run_forever()
1172
1173 # We are done, close sockets and the event loop
1174 rsock.close()
1175 wsock.close()
1176 loop.close()
1177
1178.. seealso::
1179
1180 The :ref:`register an open socket to wait for data using a protocol
1181 <asyncio-register-socket>` example uses a low-level protocol created by the
Guido van Rossumf68afd82016-08-08 09:41:21 -07001182 :meth:`AbstractEventLoop.create_connection` method.
Victor Stinner04e6df32014-10-11 16:16:27 +02001183
1184 The :ref:`register an open socket to wait for data using streams
1185 <asyncio-register-socket-streams>` example uses high-level streams
1186 created by the :func:`open_connection` function in a coroutine.
1187
1188
1189Set signal handlers for SIGINT and SIGTERM
Victor Stinnera092a612015-01-09 15:58:41 +01001190^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001191
1192Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM` using
Guido van Rossumf68afd82016-08-08 09:41:21 -07001193the :meth:`AbstractEventLoop.add_signal_handler` method::
Victor Stinner8b863482014-01-27 10:07:50 +01001194
1195 import asyncio
1196 import functools
1197 import os
1198 import signal
1199
1200 def ask_exit(signame):
1201 print("got signal %s: exit" % signame)
1202 loop.stop()
1203
1204 loop = asyncio.get_event_loop()
1205 for signame in ('SIGINT', 'SIGTERM'):
1206 loop.add_signal_handler(getattr(signal, signame),
1207 functools.partial(ask_exit, signame))
1208
Serhiy Storchaka0424eaf2015-09-12 17:45:25 +03001209 print("Event loop running forever, press Ctrl+C to interrupt.")
Victor Stinner8b863482014-01-27 10:07:50 +01001210 print("pid %s: send SIGINT or SIGTERM to exit." % os.getpid())
Victor Stinner63b21a82014-07-05 15:38:59 +02001211 try:
1212 loop.run_forever()
1213 finally:
1214 loop.close()
Victor Stinner2cef3002014-10-23 22:38:46 +02001215
1216This example only works on UNIX.