blob: f5fd094747b8ad268ce7625a52d9fb0ff6ac92bc [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
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700127.. method:: AbstractEventLoop.call_soon(callback, *args, context=None)
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
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700140 An optional keyword-only *context* argument allows specifying a custom
141 :class:`contextvars.Context` for the *callback* to run in. The current
142 context is used when no *context* is provided.
143
Yury Selivanov1096f762015-06-25 13:49:52 -0400144 An instance of :class:`asyncio.Handle` is returned, which can be
145 used to cancel the callback.
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500146
Victor Stinner8464c242014-11-28 13:15:41 +0100147 :ref:`Use functools.partial to pass keywords to the callback
148 <asyncio-pass-keywords>`.
149
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700150 .. versionchanged:: 3.7
151 The *context* keyword-only parameter was added. See :pep:`567`
152 for more details.
153
154.. method:: AbstractEventLoop.call_soon_threadsafe(callback, *args, context=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100155
156 Like :meth:`call_soon`, but thread safe.
157
Victor Stinner83704962015-02-25 14:24:15 +0100158 See the :ref:`concurrency and multithreading <asyncio-multithreading>`
159 section of the documentation.
160
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700161 .. versionchanged:: 3.7
162 The *context* keyword-only parameter was added. See :pep:`567`
163 for more details.
164
Victor Stinnerea3183f2013-12-03 01:08:00 +0100165
Victor Stinner45b27ed2014-02-01 02:36:43 +0100166.. _asyncio-delayed-calls:
167
Victor Stinnerea3183f2013-12-03 01:08:00 +0100168Delayed calls
169-------------
170
171The event loop has its own internal clock for computing timeouts.
172Which clock is used depends on the (platform-specific) event loop
173implementation; ideally it is a monotonic clock. This will generally be
174a different clock than :func:`time.time`.
175
Victor Stinnerfd9d3742014-02-18 09:37:43 +0100176.. note::
177
178 Timeouts (relative *delay* or absolute *when*) should not exceed one day.
179
Victor Stinner45b27ed2014-02-01 02:36:43 +0100180
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700181.. method:: AbstractEventLoop.call_later(delay, callback, *args, context=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100182
183 Arrange for the *callback* to be called after the given *delay*
184 seconds (either an int or float).
185
Miss Islington (bot)71a0b0e2018-02-01 11:56:03 -0800186 An instance of :class:`asyncio.TimerHandle` is returned, which can be
Yury Selivanov1096f762015-06-25 13:49:52 -0400187 used to cancel the callback.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100188
189 *callback* will be called exactly once per call to :meth:`call_later`.
190 If two callbacks are scheduled for exactly the same time, it is
191 undefined which will be called first.
192
193 The optional positional *args* will be passed to the callback when it
194 is called. If you want the callback to be called with some named
195 arguments, use a closure or :func:`functools.partial`.
196
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700197 An optional keyword-only *context* argument allows specifying a custom
198 :class:`contextvars.Context` for the *callback* to run in. The current
199 context is used when no *context* is provided.
200
Victor Stinner8464c242014-11-28 13:15:41 +0100201 :ref:`Use functools.partial to pass keywords to the callback
202 <asyncio-pass-keywords>`.
203
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700204 .. versionchanged:: 3.7
205 The *context* keyword-only parameter was added. See :pep:`567`
206 for more details.
207
208.. method:: AbstractEventLoop.call_at(when, callback, *args, context=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100209
210 Arrange for the *callback* to be called at the given absolute timestamp
Berker Peksagb5563992014-11-07 19:51:07 +0200211 *when* (an int or float), using the same time reference as
Guido van Rossumf68afd82016-08-08 09:41:21 -0700212 :meth:`AbstractEventLoop.time`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100213
214 This method's behavior is the same as :meth:`call_later`.
215
Miss Islington (bot)71a0b0e2018-02-01 11:56:03 -0800216 An instance of :class:`asyncio.TimerHandle` is returned, which can be
Yury Selivanov1096f762015-06-25 13:49:52 -0400217 used to cancel the callback.
218
Victor Stinner8464c242014-11-28 13:15:41 +0100219 :ref:`Use functools.partial to pass keywords to the callback
220 <asyncio-pass-keywords>`.
221
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700222 .. versionchanged:: 3.7
223 The *context* keyword-only parameter was added. See :pep:`567`
224 for more details.
225
Guido van Rossumf68afd82016-08-08 09:41:21 -0700226.. method:: AbstractEventLoop.time()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100227
228 Return the current time, as a :class:`float` value, according to the
229 event loop's internal clock.
230
Victor Stinner3e09e322013-12-03 01:22:06 +0100231.. seealso::
232
233 The :func:`asyncio.sleep` function.
234
Victor Stinnerea3183f2013-12-03 01:08:00 +0100235
Yury Selivanov950204d2016-05-16 16:23:00 -0400236Futures
237-------
238
Guido van Rossumf68afd82016-08-08 09:41:21 -0700239.. method:: AbstractEventLoop.create_future()
Yury Selivanov950204d2016-05-16 16:23:00 -0400240
241 Create an :class:`asyncio.Future` object attached to the loop.
242
243 This is a preferred way to create futures in asyncio, as event
244 loop implementations can provide alternative implementations
245 of the Future class (with better performance or instrumentation).
246
247 .. versionadded:: 3.5.2
248
249
Yury Selivanovbb961342015-06-25 11:54:34 -0400250Tasks
251-----
Victor Stinner530ef2f2014-07-08 12:39:10 +0200252
Guido van Rossumf68afd82016-08-08 09:41:21 -0700253.. method:: AbstractEventLoop.create_task(coro)
Victor Stinner530ef2f2014-07-08 12:39:10 +0200254
255 Schedule the execution of a :ref:`coroutine object <coroutine>`: wrap it in
256 a future. Return a :class:`Task` object.
257
258 Third-party event loops can use their own subclass of :class:`Task` for
259 interoperability. In this case, the result type is a subclass of
260 :class:`Task`.
261
Victor Stinner530ef2f2014-07-08 12:39:10 +0200262 .. versionadded:: 3.4.2
263
Guido van Rossumf68afd82016-08-08 09:41:21 -0700264.. method:: AbstractEventLoop.set_task_factory(factory)
Yury Selivanov71854612015-05-11 16:28:27 -0400265
266 Set a task factory that will be used by
Guido van Rossumf68afd82016-08-08 09:41:21 -0700267 :meth:`AbstractEventLoop.create_task`.
Yury Selivanov71854612015-05-11 16:28:27 -0400268
269 If *factory* is ``None`` the default task factory will be set.
270
271 If *factory* is a *callable*, it should have a signature matching
272 ``(loop, coro)``, where *loop* will be a reference to the active
273 event loop, *coro* will be a coroutine object. The callable
274 must return an :class:`asyncio.Future` compatible object.
275
276 .. versionadded:: 3.4.4
277
Guido van Rossumf68afd82016-08-08 09:41:21 -0700278.. method:: AbstractEventLoop.get_task_factory()
Yury Selivanov71854612015-05-11 16:28:27 -0400279
280 Return a task factory, or ``None`` if the default one is in use.
281
282 .. versionadded:: 3.4.4
283
Victor Stinner530ef2f2014-07-08 12:39:10 +0200284
Victor Stinnerea3183f2013-12-03 01:08:00 +0100285Creating connections
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100286--------------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100287
Andrew Svetlov51eb1c62017-12-20 20:24:43 +0200288.. 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 +0100289
290 Create a streaming transport connection to a given Internet *host* and
Victor Stinnera6919aa2014-02-19 13:32:34 +0100291 *port*: socket family :py:data:`~socket.AF_INET` or
292 :py:data:`~socket.AF_INET6` depending on *host* (or *family* if specified),
293 socket type :py:data:`~socket.SOCK_STREAM`. *protocol_factory* must be a
294 callable returning a :ref:`protocol <asyncio-protocol>` instance.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100295
Yury Selivanov19a44f62017-12-14 20:53:26 -0500296 This method will try to establish the connection in the background.
297 When successful, it returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100298
299 The chronological synopsis of the underlying operation is as follows:
300
Victor Stinner9592edb2014-02-02 15:03:02 +0100301 #. The connection is established, and a :ref:`transport <asyncio-transport>`
Victor Stinnerea3183f2013-12-03 01:08:00 +0100302 is created to represent it.
303
304 #. *protocol_factory* is called without arguments and must return a
Victor Stinner9592edb2014-02-02 15:03:02 +0100305 :ref:`protocol <asyncio-protocol>` instance.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100306
307 #. The protocol instance is tied to the transport, and its
308 :meth:`connection_made` method is called.
309
310 #. The coroutine returns successfully with the ``(transport, protocol)``
311 pair.
312
313 The created transport is an implementation-dependent bidirectional stream.
314
315 .. note::
316 *protocol_factory* can be any kind of callable, not necessarily
317 a class. For example, if you want to use a pre-created
318 protocol instance, you can pass ``lambda: my_protocol``.
319
Martin Panterc04fb562016-02-10 05:44:01 +0000320 Options that change how the connection is created:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100321
322 * *ssl*: if given and not false, a SSL/TLS transport is created
323 (by default a plain TCP transport is created). If *ssl* is
324 a :class:`ssl.SSLContext` object, this context is used to create
325 the transport; if *ssl* is :const:`True`, a context with some
326 unspecified default settings is used.
327
Berker Peksag9c1dba22014-09-28 00:00:58 +0300328 .. seealso:: :ref:`SSL/TLS security considerations <ssl-security>`
Antoine Pitrouc5e075f2014-03-22 18:19:11 +0100329
Victor Stinnerea3183f2013-12-03 01:08:00 +0100330 * *server_hostname*, is only for use together with *ssl*,
331 and sets or overrides the hostname that the target server's certificate
332 will be matched against. By default the value of the *host* argument
333 is used. If *host* is empty, there is no default and you must pass a
334 value for *server_hostname*. If *server_hostname* is an empty
335 string, hostname matching is disabled (which is a serious security
336 risk, allowing for man-in-the-middle-attacks).
337
338 * *family*, *proto*, *flags* are the optional address family, protocol
339 and flags to be passed through to getaddrinfo() for *host* resolution.
340 If given, these should all be integers from the corresponding
341 :mod:`socket` module constants.
342
343 * *sock*, if given, should be an existing, already connected
344 :class:`socket.socket` object to be used by the transport.
345 If *sock* is given, none of *host*, *port*, *family*, *proto*, *flags*
346 and *local_addr* should be specified.
347
348 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
349 to bind the socket to locally. The *local_host* and *local_port*
350 are looked up using getaddrinfo(), similarly to *host* and *port*.
351
Neil Aspinallf7686c12017-12-19 19:45:42 +0000352 * *ssl_handshake_timeout* is (for an SSL connection) the time in seconds
353 to wait for the SSL handshake to complete before aborting the connection.
Miss Islington (bot)87936d02018-06-04 09:05:46 -0700354 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000355
356 .. versionadded:: 3.7
357
358 The *ssl_handshake_timeout* parameter.
359
Victor Stinner60208a12015-09-15 22:41:52 +0200360 .. versionchanged:: 3.5
361
362 On Windows with :class:`ProactorEventLoop`, SSL/TLS is now supported.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200363
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100364 .. seealso::
365
366 The :func:`open_connection` function can be used to get a pair of
367 (:class:`StreamReader`, :class:`StreamWriter`) instead of a protocol.
368
Victor Stinnerea3183f2013-12-03 01:08:00 +0100369
Guido van Rossumf68afd82016-08-08 09:41:21 -0700370.. 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 +0100371
Quentin Dawansfe4ea9c2017-10-30 14:43:02 +0100372 Create datagram connection: socket family :py:data:`~socket.AF_INET`,
373 :py:data:`~socket.AF_INET6` or :py:data:`~socket.AF_UNIX` depending on
374 *host* (or *family* if specified), socket type
375 :py:data:`~socket.SOCK_DGRAM`. *protocol_factory* must be a
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700376 callable returning a :ref:`protocol <asyncio-protocol>` instance.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100377
Yury Selivanov19a44f62017-12-14 20:53:26 -0500378 This method will try to establish the connection in the background.
379 When successful, the it returns a ``(transport, protocol)`` pair.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100380
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700381 Options changing how the connection is created:
382
383 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
384 to bind the socket to locally. The *local_host* and *local_port*
385 are looked up using :meth:`getaddrinfo`.
386
387 * *remote_addr*, if given, is a ``(remote_host, remote_port)`` tuple used
388 to connect the socket to a remote address. The *remote_host* and
389 *remote_port* are looked up using :meth:`getaddrinfo`.
390
391 * *family*, *proto*, *flags* are the optional address family, protocol
392 and flags to be passed through to :meth:`getaddrinfo` for *host*
393 resolution. If given, these should all be integers from the
394 corresponding :mod:`socket` module constants.
395
396 * *reuse_address* tells the kernel to reuse a local socket in
397 TIME_WAIT state, without waiting for its natural timeout to
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300398 expire. If not specified will automatically be set to ``True`` on
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700399 UNIX.
400
401 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
402 same port as other existing endpoints are bound to, so long as they all
403 set this flag when being created. This option is not supported on Windows
404 and some UNIX's. If the :py:data:`~socket.SO_REUSEPORT` constant is not
405 defined then this capability is unsupported.
406
407 * *allow_broadcast* tells the kernel to allow this endpoint to send
408 messages to the broadcast address.
409
410 * *sock* can optionally be specified in order to use a preexisting,
411 already connected, :class:`socket.socket` object to be used by the
412 transport. If specified, *local_addr* and *remote_addr* should be omitted
413 (must be :const:`None`).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100414
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200415 On Windows with :class:`ProactorEventLoop`, this method is not supported.
416
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200417 See :ref:`UDP echo client protocol <asyncio-udp-echo-client-protocol>` and
418 :ref:`UDP echo server protocol <asyncio-udp-echo-server-protocol>` examples.
419
Miss Islington (bot)c6348cf2018-05-14 13:12:38 -0700420 .. versionchanged:: 3.4.4
421 The *family*, *proto*, *flags*, *reuse_address*, *reuse_port,
422 *allow_broadcast*, and *sock* parameters were added.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100423
Andrew Svetlov51eb1c62017-12-20 20:24:43 +0200424.. 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 +0100425
426 Create UNIX connection: socket family :py:data:`~socket.AF_UNIX`, socket
427 type :py:data:`~socket.SOCK_STREAM`. The :py:data:`~socket.AF_UNIX` socket
428 family is used to communicate between processes on the same machine
429 efficiently.
430
Yury Selivanov19a44f62017-12-14 20:53:26 -0500431 This method will try to establish the connection in the background.
432 When successful, the it returns a ``(transport, protocol)`` pair.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100433
Guido van Rossum9e80eeb2016-11-03 14:17:25 -0700434 *path* is the name of a UNIX domain socket, and is required unless a *sock*
Yury Selivanov423fd362017-11-20 17:26:28 -0500435 parameter is specified. Abstract UNIX sockets, :class:`str`,
436 :class:`bytes`, and :class:`~pathlib.Path` paths are supported.
Guido van Rossum9e80eeb2016-11-03 14:17:25 -0700437
Guido van Rossumf68afd82016-08-08 09:41:21 -0700438 See the :meth:`AbstractEventLoop.create_connection` method for parameters.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100439
440 Availability: UNIX.
441
Neil Aspinallf7686c12017-12-19 19:45:42 +0000442 .. versionadded:: 3.7
443
444 The *ssl_handshake_timeout* parameter.
445
Yury Selivanov423fd362017-11-20 17:26:28 -0500446 .. versionchanged:: 3.7
447
Miss Islington (bot)f4dcf492018-06-08 08:54:31 -0700448 The *path* parameter can now be a :term:`path-like object`.
Yury Selivanov423fd362017-11-20 17:26:28 -0500449
Victor Stinnera6919aa2014-02-19 13:32:34 +0100450
Victor Stinnerea3183f2013-12-03 01:08:00 +0100451Creating listening connections
452------------------------------
453
Yury Selivanovc9070d02018-01-25 18:08:09 -0500454.. 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 +0100455
Victor Stinner33f6abe2014-10-12 20:36:04 +0200456 Create a TCP server (socket type :data:`~socket.SOCK_STREAM`) bound to
457 *host* and *port*.
458
459 Return a :class:`Server` object, its :attr:`~Server.sockets` attribute
460 contains created sockets. Use the :meth:`Server.close` method to stop the
461 server: close listening sockets.
462
463 Parameters:
464
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200465 * The *host* parameter can be a string, in that case the TCP server is
466 bound to *host* and *port*. The *host* parameter can also be a sequence
467 of strings and in that case the TCP server is bound to all hosts of the
468 sequence. If *host* is an empty string or ``None``, all interfaces are
469 assumed and a list of multiple sockets will be returned (most likely one
470 for IPv4 and another one for IPv6).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200471
472 * *family* can be set to either :data:`socket.AF_INET` or
473 :data:`~socket.AF_INET6` to force the socket to use IPv4 or IPv6. If not set
474 it will be determined from host (defaults to :data:`socket.AF_UNSPEC`).
475
476 * *flags* is a bitmask for :meth:`getaddrinfo`.
477
478 * *sock* can optionally be specified in order to use a preexisting
479 socket object. If specified, *host* and *port* should be omitted (must be
480 :const:`None`).
481
482 * *backlog* is the maximum number of queued connections passed to
483 :meth:`~socket.socket.listen` (defaults to 100).
484
485 * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over the
486 accepted connections.
487
488 * *reuse_address* tells the kernel to reuse a local socket in
489 TIME_WAIT state, without waiting for its natural timeout to
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300490 expire. If not specified will automatically be set to ``True`` on
Victor Stinner33f6abe2014-10-12 20:36:04 +0200491 UNIX.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100492
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700493 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
494 same port as other existing endpoints are bound to, so long as they all
495 set this flag when being created. This option is not supported on
496 Windows.
497
Neil Aspinallf7686c12017-12-19 19:45:42 +0000498 * *ssl_handshake_timeout* is (for an SSL server) the time in seconds to wait
499 for the SSL handshake to complete before aborting the connection.
Miss Islington (bot)87936d02018-06-04 09:05:46 -0700500 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000501
Yury Selivanovc9070d02018-01-25 18:08:09 -0500502 * *start_serving* set to ``True`` (the default) causes the created server
503 to start accepting connections immediately. When set to ``False``,
504 the user should await on :meth:`Server.start_serving` or
505 :meth:`Server.serve_forever` to make the server to start accepting
506 connections.
507
Neil Aspinallf7686c12017-12-19 19:45:42 +0000508 .. versionadded:: 3.7
509
Yury Selivanovc9070d02018-01-25 18:08:09 -0500510 *ssl_handshake_timeout* and *start_serving* parameters.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000511
Victor Stinner60208a12015-09-15 22:41:52 +0200512 .. versionchanged:: 3.5
513
514 On Windows with :class:`ProactorEventLoop`, SSL/TLS is now supported.
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200515
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100516 .. seealso::
517
518 The function :func:`start_server` creates a (:class:`StreamReader`,
519 :class:`StreamWriter`) pair and calls back a function with this pair.
520
Victor Stinner7b58a2b2015-09-21 18:41:05 +0200521 .. versionchanged:: 3.5.1
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200522
523 The *host* parameter can now be a sequence of strings.
524
Victor Stinnerea3183f2013-12-03 01:08:00 +0100525
Yury Selivanovc9070d02018-01-25 18:08:09 -0500526.. 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 +0100527
Guido van Rossumf68afd82016-08-08 09:41:21 -0700528 Similar to :meth:`AbstractEventLoop.create_server`, but specific to the
Victor Stinnera6919aa2014-02-19 13:32:34 +0100529 socket family :py:data:`~socket.AF_UNIX`.
530
Yury Selivanov423fd362017-11-20 17:26:28 -0500531 *path* is the name of a UNIX domain socket, and is required unless a *sock*
532 parameter is specified. Abstract UNIX sockets, :class:`str`,
533 :class:`bytes`, and :class:`~pathlib.Path` paths are supported.
534
Victor Stinnera6919aa2014-02-19 13:32:34 +0100535 Availability: UNIX.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100536
Neil Aspinallf7686c12017-12-19 19:45:42 +0000537 .. versionadded:: 3.7
538
Miss Islington (bot)f4dcf492018-06-08 08:54:31 -0700539 The *ssl_handshake_timeout* and *start_serving* parameters.
Neil Aspinallf7686c12017-12-19 19:45:42 +0000540
Yury Selivanov423fd362017-11-20 17:26:28 -0500541 .. versionchanged:: 3.7
542
543 The *path* parameter can now be a :class:`~pathlib.Path` object.
544
Andrew Svetlov51eb1c62017-12-20 20:24:43 +0200545.. coroutinemethod:: BaseEventLoop.connect_accepted_socket(protocol_factory, sock, \*, ssl=None, ssl_handshake_timeout=None)
Yury Selivanov3b3a1412016-11-07 15:35:25 -0500546
547 Handle an accepted connection.
548
549 This is used by servers that accept connections outside of
550 asyncio but that use asyncio to handle them.
551
552 Parameters:
553
554 * *sock* is a preexisting socket object returned from an ``accept``
555 call.
556
557 * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over the
558 accepted connections.
559
Neil Aspinallf7686c12017-12-19 19:45:42 +0000560 * *ssl_handshake_timeout* is (for an SSL connection) the time in seconds to
561 wait for the SSL handshake to complete before aborting the connection.
Miss Islington (bot)87936d02018-06-04 09:05:46 -0700562 ``60.0`` seconds if ``None`` (default).
Neil Aspinallf7686c12017-12-19 19:45:42 +0000563
Yury Selivanov19a44f62017-12-14 20:53:26 -0500564 When completed it returns a ``(transport, protocol)`` pair.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100565
Neil Aspinallf7686c12017-12-19 19:45:42 +0000566 .. versionadded:: 3.7
567
568 The *ssl_handshake_timeout* parameter.
569
AraHaan431665b2017-11-21 11:06:26 -0500570 .. versionadded:: 3.5.3
571
572
Andrew Svetlov7c684072018-01-27 21:22:47 +0200573File Transferring
574-----------------
575
Elvis Pranskevichusee72ac02018-01-27 17:11:10 -0500576.. coroutinemethod:: AbstractEventLoop.sendfile(transport, file, \
Andrew Svetlov7c684072018-01-27 21:22:47 +0200577 offset=0, count=None, \
578 *, fallback=True)
579
580 Send a *file* to *transport*, return the total number of bytes
581 which were sent.
582
583 The method uses high-performance :meth:`os.sendfile` if available.
584
585 *file* must be a regular file object opened in binary mode.
586
587 *offset* tells from where to start reading the file. If specified,
588 *count* is the total number of bytes to transmit as opposed to
589 sending the file until EOF is reached. File position is updated on
590 return or also in case of error in which case :meth:`file.tell()
591 <io.IOBase.tell>` can be used to figure out the number of bytes
592 which were sent.
593
594 *fallback* set to ``True`` makes asyncio to manually read and send
595 the file when the platform does not support the sendfile syscall
596 (e.g. Windows or SSL socket on Unix).
597
598 Raise :exc:`SendfileNotAvailableError` if the system does not support
599 *sendfile* syscall and *fallback* is ``False``.
600
601 .. versionadded:: 3.7
602
603
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500604TLS Upgrade
605-----------
606
607.. coroutinemethod:: AbstractEventLoop.start_tls(transport, protocol, sslcontext, \*, server_side=False, server_hostname=None, ssl_handshake_timeout=None)
608
609 Upgrades an existing connection to TLS.
610
611 Returns a new transport instance, that the *protocol* must start using
612 immediately after the *await*. The *transport* instance passed to
613 the *start_tls* method should never be used again.
614
615 Parameters:
616
617 * *transport* and *protocol* instances that methods like
618 :meth:`~AbstractEventLoop.create_server` and
619 :meth:`~AbstractEventLoop.create_connection` return.
620
621 * *sslcontext*: a configured instance of :class:`~ssl.SSLContext`.
622
623 * *server_side* pass ``True`` when a server-side connection is being
624 upgraded (like the one created by :meth:`~AbstractEventLoop.create_server`).
625
626 * *server_hostname*: sets or overrides the host name that the target
627 server's certificate will be matched against.
628
629 * *ssl_handshake_timeout* is (for an SSL connection) the time in seconds to
630 wait for the SSL handshake to complete before aborting the connection.
Miss Islington (bot)87936d02018-06-04 09:05:46 -0700631 ``60.0`` seconds if ``None`` (default).
Yury Selivanovf111b3d2017-12-30 00:35:36 -0500632
633 .. versionadded:: 3.7
634
635
Victor Stinnerc1567df2014-02-08 23:22:58 +0100636Watch file descriptors
637----------------------
638
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200639On Windows with :class:`SelectorEventLoop`, only socket handles are supported
640(ex: pipe file descriptors are not supported).
641
642On Windows with :class:`ProactorEventLoop`, these methods are not supported.
643
Guido van Rossumf68afd82016-08-08 09:41:21 -0700644.. method:: AbstractEventLoop.add_reader(fd, callback, \*args)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100645
646 Start watching the file descriptor for read availability and then call the
647 *callback* with specified arguments.
648
Victor Stinner8464c242014-11-28 13:15:41 +0100649 :ref:`Use functools.partial to pass keywords to the callback
650 <asyncio-pass-keywords>`.
651
Guido van Rossumf68afd82016-08-08 09:41:21 -0700652.. method:: AbstractEventLoop.remove_reader(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100653
654 Stop watching the file descriptor for read availability.
655
Guido van Rossumf68afd82016-08-08 09:41:21 -0700656.. method:: AbstractEventLoop.add_writer(fd, callback, \*args)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100657
658 Start watching the file descriptor for write availability and then call the
659 *callback* with specified arguments.
660
Victor Stinner8464c242014-11-28 13:15:41 +0100661 :ref:`Use functools.partial to pass keywords to the callback
662 <asyncio-pass-keywords>`.
663
Guido van Rossumf68afd82016-08-08 09:41:21 -0700664.. method:: AbstractEventLoop.remove_writer(fd)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100665
666 Stop watching the file descriptor for write availability.
667
Victor Stinner04e6df32014-10-11 16:16:27 +0200668The :ref:`watch a file descriptor for read events <asyncio-watch-read-event>`
Guido van Rossumf68afd82016-08-08 09:41:21 -0700669example uses the low-level :meth:`AbstractEventLoop.add_reader` method to register
Victor Stinner04e6df32014-10-11 16:16:27 +0200670the file descriptor of a socket.
671
Victor Stinnerc1567df2014-02-08 23:22:58 +0100672
673Low-level socket operations
674---------------------------
675
Guido van Rossumf68afd82016-08-08 09:41:21 -0700676.. coroutinemethod:: AbstractEventLoop.sock_recv(sock, nbytes)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100677
Yury Selivanov55c50842016-06-08 12:48:15 -0400678 Receive data from the socket. Modeled after blocking
679 :meth:`socket.socket.recv` method.
680
681 The return value is a bytes object
Victor Stinnerc1567df2014-02-08 23:22:58 +0100682 representing the data received. The maximum amount of data to be received
683 at once is specified by *nbytes*.
684
Victor Stinnerd84fd732014-08-26 01:01:59 +0200685 With :class:`SelectorEventLoop` event loop, the socket *sock* must be
686 non-blocking.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200687
Yury Selivanov19a44f62017-12-14 20:53:26 -0500688 .. versionchanged:: 3.7
689 Even though the method was always documented as a coroutine
690 method, before Python 3.7 it returned a :class:`Future`.
691 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100692
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200693.. coroutinemethod:: AbstractEventLoop.sock_recv_into(sock, buf)
694
695 Receive data from the socket. Modeled after blocking
696 :meth:`socket.socket.recv_into` method.
697
698 The received data is written into *buf* (a writable buffer).
699 The return value is the number of bytes written.
700
701 With :class:`SelectorEventLoop` event loop, the socket *sock* must be
702 non-blocking.
703
Antoine Pitrou525f40d2017-10-19 21:46:40 +0200704 .. versionadded:: 3.7
705
Guido van Rossumf68afd82016-08-08 09:41:21 -0700706.. coroutinemethod:: AbstractEventLoop.sock_sendall(sock, data)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100707
Yury Selivanov55c50842016-06-08 12:48:15 -0400708 Send data to the socket. Modeled after blocking
709 :meth:`socket.socket.sendall` method.
710
711 The socket must be connected to a remote socket.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100712 This method continues to send data from *data* until either all data has
713 been sent or an error occurs. ``None`` is returned on success. On error,
714 an exception is raised, and there is no way to determine how much data, if
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500715 any, was successfully processed by the receiving end of the connection.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100716
Victor Stinnerd84fd732014-08-26 01:01:59 +0200717 With :class:`SelectorEventLoop` event loop, the socket *sock* must be
718 non-blocking.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200719
Yury Selivanov19a44f62017-12-14 20:53:26 -0500720 .. versionchanged:: 3.7
721 Even though the method was always documented as a coroutine
722 method, before Python 3.7 it returned an :class:`Future`.
723 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100724
Guido van Rossumf68afd82016-08-08 09:41:21 -0700725.. coroutinemethod:: AbstractEventLoop.sock_connect(sock, address)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100726
Yury Selivanov55c50842016-06-08 12:48:15 -0400727 Connect to a remote socket at *address*. Modeled after
728 blocking :meth:`socket.socket.connect` method.
Victor Stinner1b0580b2014-02-13 09:24:37 +0100729
Victor Stinnerd84fd732014-08-26 01:01:59 +0200730 With :class:`SelectorEventLoop` event loop, the socket *sock* must be
731 non-blocking.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200732
Yury Selivanov55c50842016-06-08 12:48:15 -0400733 .. versionchanged:: 3.5.2
734 ``address`` no longer needs to be resolved. ``sock_connect``
735 will try to check if the *address* is already resolved by calling
736 :func:`socket.inet_pton`. If not,
Guido van Rossumf68afd82016-08-08 09:41:21 -0700737 :meth:`AbstractEventLoop.getaddrinfo` will be used to resolve the
Yury Selivanov55c50842016-06-08 12:48:15 -0400738 *address*.
739
Victor Stinnerc1567df2014-02-08 23:22:58 +0100740 .. seealso::
741
Guido van Rossumf68afd82016-08-08 09:41:21 -0700742 :meth:`AbstractEventLoop.create_connection`
Yury Selivanov55c50842016-06-08 12:48:15 -0400743 and :func:`asyncio.open_connection() <open_connection>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100744
745
Guido van Rossumf68afd82016-08-08 09:41:21 -0700746.. coroutinemethod:: AbstractEventLoop.sock_accept(sock)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100747
Yury Selivanov55c50842016-06-08 12:48:15 -0400748 Accept a connection. Modeled after blocking
749 :meth:`socket.socket.accept`.
750
751 The socket must be bound to an address and listening
Victor Stinnerc1567df2014-02-08 23:22:58 +0100752 for connections. The return value is a pair ``(conn, address)`` where *conn*
753 is a *new* socket object usable to send and receive data on the connection,
754 and *address* is the address bound to the socket on the other end of the
755 connection.
756
Victor Stinnerec2ce092014-07-29 23:12:22 +0200757 The socket *sock* must be non-blocking.
758
Yury Selivanov19a44f62017-12-14 20:53:26 -0500759 .. versionchanged:: 3.7
760 Even though the method was always documented as a coroutine
761 method, before Python 3.7 it returned a :class:`Future`.
762 Since Python 3.7, this is an ``async def`` method.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100763
764 .. seealso::
765
Guido van Rossumf68afd82016-08-08 09:41:21 -0700766 :meth:`AbstractEventLoop.create_server` and :func:`start_server`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100767
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200768.. coroutinemethod:: AbstractEventLoop.sock_sendfile(sock, file, \
769 offset=0, count=None, \
770 *, fallback=True)
771
772 Send a file using high-performance :mod:`os.sendfile` if possible
773 and return the total number of bytes which were sent.
774
775 Asynchronous version of :meth:`socket.socket.sendfile`.
776
777 *sock* must be non-blocking :class:`~socket.socket` of
778 :const:`socket.SOCK_STREAM` type.
779
780 *file* must be a regular file object opened in binary mode.
781
782 *offset* tells from where to start reading the file. If specified,
783 *count* is the total number of bytes to transmit as opposed to
784 sending the file until EOF is reached. File position is updated on
785 return or also in case of error in which case :meth:`file.tell()
786 <io.IOBase.tell>` can be used to figure out the number of bytes
787 which were sent.
788
789 *fallback* set to ``True`` makes asyncio to manually read and send
790 the file when the platform does not support the sendfile syscall
791 (e.g. Windows or SSL socket on Unix).
792
Andrew Svetlov7464e872018-01-19 20:04:29 +0200793 Raise :exc:`SendfileNotAvailableError` if the system does not support
Andrew Svetlov6b5a2792018-01-16 19:59:34 +0200794 *sendfile* syscall and *fallback* is ``False``.
795
796 .. versionadded:: 3.7
797
Victor Stinnerc1567df2014-02-08 23:22:58 +0100798
799Resolve host name
800-----------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100801
Guido van Rossumf68afd82016-08-08 09:41:21 -0700802.. coroutinemethod:: AbstractEventLoop.getaddrinfo(host, port, \*, family=0, type=0, proto=0, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100803
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500804 This method is a :ref:`coroutine <coroutine>`, similar to
805 :meth:`socket.getaddrinfo` function but non-blocking.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100806
Guido van Rossumf68afd82016-08-08 09:41:21 -0700807.. coroutinemethod:: AbstractEventLoop.getnameinfo(sockaddr, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100808
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500809 This method is a :ref:`coroutine <coroutine>`, similar to
810 :meth:`socket.getnameinfo` function but non-blocking.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100811
Yury Selivanovbec23722018-01-28 14:09:40 -0500812.. versionchanged:: 3.7
813 Both *getaddrinfo* and *getnameinfo* methods were always documented
814 to return a coroutine, but prior to Python 3.7 they were, in fact,
815 returning :class:`asyncio.Future` objects. Starting with Python 3.7
816 both methods are coroutines.
817
Victor Stinnerea3183f2013-12-03 01:08:00 +0100818
Victor Stinner984600f2014-03-25 09:40:26 +0100819Connect pipes
820-------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100821
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200822On Windows with :class:`SelectorEventLoop`, these methods are not supported.
823Use :class:`ProactorEventLoop` to support pipes on Windows.
824
Guido van Rossumf68afd82016-08-08 09:41:21 -0700825.. coroutinemethod:: AbstractEventLoop.connect_read_pipe(protocol_factory, pipe)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100826
Victor Stinnerd84fd732014-08-26 01:01:59 +0200827 Register read pipe in eventloop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100828
829 *protocol_factory* should instantiate object with :class:`Protocol`
Victor Stinnera5b257a2014-05-29 00:14:03 +0200830 interface. *pipe* is a :term:`file-like object <file object>`.
831 Return pair ``(transport, protocol)``, where *transport* supports the
Victor Stinnerea3183f2013-12-03 01:08:00 +0100832 :class:`ReadTransport` interface.
833
Victor Stinnerd84fd732014-08-26 01:01:59 +0200834 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
835 non-blocking mode.
836
Guido van Rossumf68afd82016-08-08 09:41:21 -0700837.. coroutinemethod:: AbstractEventLoop.connect_write_pipe(protocol_factory, pipe)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100838
839 Register write pipe in eventloop.
840
841 *protocol_factory* should instantiate object with :class:`BaseProtocol`
Victor Stinner2cef3002014-10-23 22:38:46 +0200842 interface. *pipe* is :term:`file-like object <file object>`.
843 Return pair ``(transport, protocol)``, where *transport* supports
Victor Stinnerea3183f2013-12-03 01:08:00 +0100844 :class:`WriteTransport` interface.
845
Victor Stinnerd84fd732014-08-26 01:01:59 +0200846 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
847 non-blocking mode.
848
Victor Stinner08444382014-02-02 22:43:39 +0100849.. seealso::
850
Guido van Rossumf68afd82016-08-08 09:41:21 -0700851 The :meth:`AbstractEventLoop.subprocess_exec` and
852 :meth:`AbstractEventLoop.subprocess_shell` methods.
Victor Stinner08444382014-02-02 22:43:39 +0100853
Victor Stinnerea3183f2013-12-03 01:08:00 +0100854
Victor Stinner8b863482014-01-27 10:07:50 +0100855UNIX signals
856------------
857
858Availability: UNIX only.
859
Guido van Rossumf68afd82016-08-08 09:41:21 -0700860.. method:: AbstractEventLoop.add_signal_handler(signum, callback, \*args)
Victor Stinner8b863482014-01-27 10:07:50 +0100861
862 Add a handler for a signal.
863
864 Raise :exc:`ValueError` if the signal number is invalid or uncatchable.
865 Raise :exc:`RuntimeError` if there is a problem setting up the handler.
866
Victor Stinner8464c242014-11-28 13:15:41 +0100867 :ref:`Use functools.partial to pass keywords to the callback
868 <asyncio-pass-keywords>`.
869
Guido van Rossumf68afd82016-08-08 09:41:21 -0700870.. method:: AbstractEventLoop.remove_signal_handler(sig)
Victor Stinner8b863482014-01-27 10:07:50 +0100871
872 Remove a handler for a signal.
873
874 Return ``True`` if a signal handler was removed, ``False`` if not.
875
876.. seealso::
877
878 The :mod:`signal` module.
879
880
Victor Stinnerea3183f2013-12-03 01:08:00 +0100881Executor
882--------
883
884Call a function in an :class:`~concurrent.futures.Executor` (pool of threads or
885pool of processes). By default, an event loop uses a thread pool executor
886(:class:`~concurrent.futures.ThreadPoolExecutor`).
887
Yury Selivanovbec23722018-01-28 14:09:40 -0500888.. method:: AbstractEventLoop.run_in_executor(executor, func, \*args)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100889
Andrew Svetlov1c62b522015-10-01 09:48:08 +0300890 Arrange for a *func* to be called in the specified executor.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100891
Larry Hastings3732ed22014-03-15 21:13:56 -0700892 The *executor* argument should be an :class:`~concurrent.futures.Executor`
893 instance. The default executor is used if *executor* is ``None``.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100894
Andrew Svetlov1c62b522015-10-01 09:48:08 +0300895 :ref:`Use functools.partial to pass keywords to the *func*
Victor Stinner8464c242014-11-28 13:15:41 +0100896 <asyncio-pass-keywords>`.
897
Yury Selivanovbec23722018-01-28 14:09:40 -0500898 This method returns a :class:`asyncio.Future` object.
899
Yury Selivanove8a60452016-10-21 17:40:42 -0400900 .. versionchanged:: 3.5.3
901 :meth:`BaseEventLoop.run_in_executor` no longer configures the
902 ``max_workers`` of the thread pool executor it creates, instead
903 leaving it up to the thread pool executor
904 (:class:`~concurrent.futures.ThreadPoolExecutor`) to set the
905 default.
906
Guido van Rossumf68afd82016-08-08 09:41:21 -0700907.. method:: AbstractEventLoop.set_default_executor(executor)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100908
909 Set the default executor used by :meth:`run_in_executor`.
910
911
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500912Error Handling API
913------------------
914
Martin Panterc04fb562016-02-10 05:44:01 +0000915Allows customizing how exceptions are handled in the event loop.
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500916
Guido van Rossumf68afd82016-08-08 09:41:21 -0700917.. method:: AbstractEventLoop.set_exception_handler(handler)
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500918
919 Set *handler* as the new event loop exception handler.
920
921 If *handler* is ``None``, the default exception handler will
922 be set.
923
924 If *handler* is a callable object, it should have a
925 matching signature to ``(loop, context)``, where ``loop``
926 will be a reference to the active event loop, ``context``
927 will be a ``dict`` object (see :meth:`call_exception_handler`
928 documentation for details about context).
929
Guido van Rossumf68afd82016-08-08 09:41:21 -0700930.. method:: AbstractEventLoop.get_exception_handler()
Yury Selivanov950204d2016-05-16 16:23:00 -0400931
932 Return the exception handler, or ``None`` if the default one
933 is in use.
934
935 .. versionadded:: 3.5.2
936
Guido van Rossumf68afd82016-08-08 09:41:21 -0700937.. method:: AbstractEventLoop.default_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500938
939 Default exception handler.
940
941 This is called when an exception occurs and no exception
942 handler is set, and can be called by a custom exception
943 handler that wants to defer to the default behavior.
944
945 *context* parameter has the same meaning as in
946 :meth:`call_exception_handler`.
947
Guido van Rossumf68afd82016-08-08 09:41:21 -0700948.. method:: AbstractEventLoop.call_exception_handler(context)
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500949
950 Call the current event loop exception handler.
951
952 *context* is a ``dict`` object containing the following keys
953 (new keys may be introduced later):
954
955 * 'message': Error message;
956 * 'exception' (optional): Exception object;
957 * 'future' (optional): :class:`asyncio.Future` instance;
958 * 'handle' (optional): :class:`asyncio.Handle` instance;
959 * 'protocol' (optional): :ref:`Protocol <asyncio-protocol>` instance;
960 * 'transport' (optional): :ref:`Transport <asyncio-transport>` instance;
961 * 'socket' (optional): :class:`socket.socket` instance.
962
963 .. note::
964
965 Note: this method should not be overloaded in subclassed
966 event loops. For any custom exception handling, use
967 :meth:`set_exception_handler()` method.
968
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100969Debug mode
970----------
971
Guido van Rossumf68afd82016-08-08 09:41:21 -0700972.. method:: AbstractEventLoop.get_debug()
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100973
Victor Stinner7b7120e2014-06-23 00:12:14 +0200974 Get the debug mode (:class:`bool`) of the event loop.
975
976 The default value is ``True`` if the environment variable
977 :envvar:`PYTHONASYNCIODEBUG` is set to a non-empty string, ``False``
978 otherwise.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100979
Victor Stinner64d750b2014-06-18 03:25:23 +0200980 .. versionadded:: 3.4.2
981
Guido van Rossumf68afd82016-08-08 09:41:21 -0700982.. method:: AbstractEventLoop.set_debug(enabled: bool)
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100983
984 Set the debug mode of the event loop.
985
Victor Stinner64d750b2014-06-18 03:25:23 +0200986 .. versionadded:: 3.4.2
987
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100988.. seealso::
989
Victor Stinner62511fd2014-06-23 00:36:11 +0200990 The :ref:`debug mode of asyncio <asyncio-debug-mode>`.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100991
Victor Stinner8c462c52014-01-24 18:11:43 +0100992Server
993------
994
Victor Stinner8ebeb032014-07-11 23:47:40 +0200995.. class:: Server
Victor Stinner8c462c52014-01-24 18:11:43 +0100996
Victor Stinner8ebeb032014-07-11 23:47:40 +0200997 Server listening on sockets.
998
Yury Selivanovc9070d02018-01-25 18:08:09 -0500999 Object created by :meth:`AbstractEventLoop.create_server`,
1000 :meth:`AbstractEventLoop.create_unix_server`, :func:`start_server`,
1001 and :func:`start_unix_server` functions. Don't instantiate the class
1002 directly.
1003
1004 *Server* objects are asynchronous context managers. When used in an
1005 ``async with`` statement, it's guaranteed that the Server object is
1006 closed and not accepting new connections when the ``async with``
1007 statement is completed::
1008
1009 srv = await loop.create_server(...)
1010
1011 async with srv:
1012 # some code
1013
1014 # At this point, srv is closed and no longer accepts new connections.
1015
1016
1017 .. versionchanged:: 3.7
1018 Server object is an asynchronous context manager since Python 3.7.
Victor Stinner8c462c52014-01-24 18:11:43 +01001019
1020 .. method:: close()
1021
Victor Stinner4bfb14a2014-07-12 03:20:24 +02001022 Stop serving: close listening sockets and set the :attr:`sockets`
1023 attribute to ``None``.
1024
Berker Peksag49c9edf2016-01-20 07:14:22 +02001025 The sockets that represent existing incoming client connections are left
1026 open.
Victor Stinner8ebeb032014-07-11 23:47:40 +02001027
Berker Peksag49c9edf2016-01-20 07:14:22 +02001028 The server is closed asynchronously, use the :meth:`wait_closed`
1029 coroutine to wait until the server is closed.
Victor Stinner8c462c52014-01-24 18:11:43 +01001030
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)1634fc22017-12-30 20:39:32 +05301031 .. method:: get_loop()
1032
1033 Gives the event loop associated with the server object.
1034
1035 .. versionadded:: 3.7
1036
Yury Selivanovc9070d02018-01-25 18:08:09 -05001037 .. coroutinemethod:: start_serving()
1038
1039 Start accepting connections.
1040
1041 This method is idempotent, so it can be called when
1042 the server is already being serving.
1043
1044 The new *start_serving* keyword-only parameter to
1045 :meth:`AbstractEventLoop.create_server` and
1046 :meth:`asyncio.start_server` allows to create a Server object
1047 that is not accepting connections right away. In which case
1048 this method, or :meth:`Server.serve_forever` can be used
1049 to make the Server object to start accepting connections.
1050
1051 .. versionadded:: 3.7
1052
1053 .. coroutinemethod:: serve_forever()
1054
1055 Start accepting connections until the coroutine is cancelled.
1056 Cancellation of ``serve_forever`` task causes the server
1057 to be closed.
1058
1059 This method can be called if the server is already accepting
1060 connections. Only one ``serve_forever`` task can exist per
1061 one *Server* object.
1062
1063 Example::
1064
1065 async def client_connected(reader, writer):
1066 # Communicate with the client with
1067 # reader/writer streams. For example:
1068 await reader.readline()
1069
1070 async def main(host, port):
1071 srv = await asyncio.start_server(
1072 client_connected, host, port)
Miss Islington (bot)aeb5d732018-02-17 10:02:46 -08001073 await srv.serve_forever()
Yury Selivanovc9070d02018-01-25 18:08:09 -05001074
1075 asyncio.run(main('127.0.0.1', 0))
1076
1077 .. versionadded:: 3.7
1078
1079 .. method:: is_serving()
1080
1081 Return ``True`` if the server is accepting new connections.
1082
1083 .. versionadded:: 3.7
1084
Victor Stinnerbdd574d2015-02-12 22:49:18 +01001085 .. coroutinemethod:: wait_closed()
Victor Stinner8c462c52014-01-24 18:11:43 +01001086
Victor Stinner8ebeb032014-07-11 23:47:40 +02001087 Wait until the :meth:`close` method completes.
1088
Victor Stinner8ebeb032014-07-11 23:47:40 +02001089 .. attribute:: sockets
1090
1091 List of :class:`socket.socket` objects the server is listening to, or
1092 ``None`` if the server is closed.
Victor Stinner8c462c52014-01-24 18:11:43 +01001093
Yury Selivanovc9070d02018-01-25 18:08:09 -05001094 .. versionchanged:: 3.7
1095 Prior to Python 3.7 ``Server.sockets`` used to return the
1096 internal list of server's sockets directly. In 3.7 a copy
1097 of that list is returned.
1098
Victor Stinner8c462c52014-01-24 18:11:43 +01001099
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001100Handle
1101------
1102
1103.. class:: Handle
1104
Guido van Rossumf68afd82016-08-08 09:41:21 -07001105 A callback wrapper object returned by :func:`AbstractEventLoop.call_soon`,
Miss Islington (bot)71a0b0e2018-02-01 11:56:03 -08001106 :func:`AbstractEventLoop.call_soon_threadsafe`.
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001107
1108 .. method:: cancel()
1109
Yury Selivanov1096f762015-06-25 13:49:52 -04001110 Cancel the call. If the callback is already canceled or executed,
1111 this method has no effect.
Victor Stinneraea82292014-07-08 23:42:38 +02001112
Marat Sharafutdinov69cfed12017-11-07 12:06:05 +03001113 .. method:: cancelled()
1114
1115 Return ``True`` if the call was cancelled.
1116
1117 .. versionadded:: 3.7
1118
Miss Islington (bot)71a0b0e2018-02-01 11:56:03 -08001119.. class:: TimerHandle
1120
1121 A callback wrapper object returned by :func:`AbstractEventLoop.call_later`,
1122 and :func:`AbstractEventLoop.call_at`.
1123
1124 The class is inherited from :class:`Handle`.
1125
1126 .. method:: when()
1127
1128 Return a scheduled callback time as :class:`float` seconds.
1129
1130 The time is an absolute timestamp, using the same time
1131 reference as :meth:`AbstractEventLoop.time`.
1132
1133 .. versionadded:: 3.7
1134
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001135
Andrew Svetlov7464e872018-01-19 20:04:29 +02001136SendfileNotAvailableError
1137-------------------------
1138
1139
1140.. exception:: SendfileNotAvailableError
1141
1142 Sendfile syscall is not available, subclass of :exc:`RuntimeError`.
1143
Miss Islington (bot)211c0db2018-03-27 18:34:15 -07001144 Raised if the OS does not support sendfile syscall for
Andrew Svetlov7464e872018-01-19 20:04:29 +02001145 given socket or file type.
1146
1147
Victor Stinner6888b962014-10-11 16:15:58 +02001148Event loop examples
Victor Stinnera092a612015-01-09 15:58:41 +01001149-------------------
Yury Selivanov43ee1c12014-02-19 20:58:44 -05001150
Victor Stinner3e09e322013-12-03 01:22:06 +01001151.. _asyncio-hello-world-callback:
Victor Stinnerea3183f2013-12-03 01:08:00 +01001152
Victor Stinner7f314ed2014-10-15 18:49:16 +02001153Hello World with call_soon()
Victor Stinnera092a612015-01-09 15:58:41 +01001154^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +01001155
Guido van Rossumf68afd82016-08-08 09:41:21 -07001156Example using the :meth:`AbstractEventLoop.call_soon` method to schedule a
Victor Stinner7f314ed2014-10-15 18:49:16 +02001157callback. The callback displays ``"Hello World"`` and then stops the event
1158loop::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001159
1160 import asyncio
1161
Victor Stinner7f314ed2014-10-15 18:49:16 +02001162 def hello_world(loop):
Victor Stinnerea3183f2013-12-03 01:08:00 +01001163 print('Hello World')
Victor Stinner7f314ed2014-10-15 18:49:16 +02001164 loop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001165
1166 loop = asyncio.get_event_loop()
Victor Stinner7f314ed2014-10-15 18:49:16 +02001167
1168 # Schedule a call to hello_world()
1169 loop.call_soon(hello_world, loop)
1170
1171 # Blocking call interrupted by loop.stop()
1172 loop.run_forever()
1173 loop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +01001174
Victor Stinner3e09e322013-12-03 01:22:06 +01001175.. seealso::
Victor Stinnerea3183f2013-12-03 01:08:00 +01001176
Victor Stinner6888b962014-10-11 16:15:58 +02001177 The :ref:`Hello World coroutine <asyncio-hello-world-coroutine>` example
1178 uses a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +01001179
Victor Stinner8b863482014-01-27 10:07:50 +01001180
Victor Stinner7f314ed2014-10-15 18:49:16 +02001181.. _asyncio-date-callback:
1182
1183Display the current date with call_later()
Victor Stinnera092a612015-01-09 15:58:41 +01001184^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner7f314ed2014-10-15 18:49:16 +02001185
1186Example of callback displaying the current date every second. The callback uses
Guido van Rossumf68afd82016-08-08 09:41:21 -07001187the :meth:`AbstractEventLoop.call_later` method to reschedule itself during 5
Victor Stinner7f314ed2014-10-15 18:49:16 +02001188seconds, and then stops the event loop::
1189
1190 import asyncio
1191 import datetime
1192
1193 def display_date(end_time, loop):
1194 print(datetime.datetime.now())
1195 if (loop.time() + 1.0) < end_time:
1196 loop.call_later(1, display_date, end_time, loop)
1197 else:
1198 loop.stop()
1199
1200 loop = asyncio.get_event_loop()
1201
1202 # Schedule the first call to display_date()
1203 end_time = loop.time() + 5.0
1204 loop.call_soon(display_date, end_time, loop)
1205
1206 # Blocking call interrupted by loop.stop()
1207 loop.run_forever()
1208 loop.close()
1209
1210.. seealso::
1211
1212 The :ref:`coroutine displaying the current date
1213 <asyncio-date-coroutine>` example uses a :ref:`coroutine
1214 <coroutine>`.
1215
1216
Victor Stinner04e6df32014-10-11 16:16:27 +02001217.. _asyncio-watch-read-event:
Victor Stinner8b863482014-01-27 10:07:50 +01001218
Victor Stinner04e6df32014-10-11 16:16:27 +02001219Watch a file descriptor for read events
Victor Stinnera092a612015-01-09 15:58:41 +01001220^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001221
1222Wait until a file descriptor received some data using the
Guido van Rossumf68afd82016-08-08 09:41:21 -07001223:meth:`AbstractEventLoop.add_reader` method and then close the event loop::
Victor Stinner04e6df32014-10-11 16:16:27 +02001224
1225 import asyncio
Victor Stinnerac577d72017-11-28 21:33:20 +01001226 from socket import socketpair
Victor Stinner04e6df32014-10-11 16:16:27 +02001227
1228 # Create a pair of connected file descriptors
Victor Stinnerccd8e342014-10-11 16:30:02 +02001229 rsock, wsock = socketpair()
Victor Stinner04e6df32014-10-11 16:16:27 +02001230 loop = asyncio.get_event_loop()
1231
1232 def reader():
1233 data = rsock.recv(100)
1234 print("Received:", data.decode())
Victor Stinner2cef3002014-10-23 22:38:46 +02001235 # We are done: unregister the file descriptor
Victor Stinner04e6df32014-10-11 16:16:27 +02001236 loop.remove_reader(rsock)
1237 # Stop the event loop
1238 loop.stop()
1239
Victor Stinner2cef3002014-10-23 22:38:46 +02001240 # Register the file descriptor for read event
Victor Stinner04e6df32014-10-11 16:16:27 +02001241 loop.add_reader(rsock, reader)
1242
1243 # Simulate the reception of data from the network
1244 loop.call_soon(wsock.send, 'abc'.encode())
1245
1246 # Run the event loop
1247 loop.run_forever()
1248
1249 # We are done, close sockets and the event loop
1250 rsock.close()
1251 wsock.close()
1252 loop.close()
1253
1254.. seealso::
1255
1256 The :ref:`register an open socket to wait for data using a protocol
1257 <asyncio-register-socket>` example uses a low-level protocol created by the
Guido van Rossumf68afd82016-08-08 09:41:21 -07001258 :meth:`AbstractEventLoop.create_connection` method.
Victor Stinner04e6df32014-10-11 16:16:27 +02001259
1260 The :ref:`register an open socket to wait for data using streams
1261 <asyncio-register-socket-streams>` example uses high-level streams
1262 created by the :func:`open_connection` function in a coroutine.
1263
1264
1265Set signal handlers for SIGINT and SIGTERM
Victor Stinnera092a612015-01-09 15:58:41 +01001266^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +02001267
1268Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM` using
Guido van Rossumf68afd82016-08-08 09:41:21 -07001269the :meth:`AbstractEventLoop.add_signal_handler` method::
Victor Stinner8b863482014-01-27 10:07:50 +01001270
1271 import asyncio
1272 import functools
1273 import os
1274 import signal
1275
1276 def ask_exit(signame):
1277 print("got signal %s: exit" % signame)
1278 loop.stop()
1279
1280 loop = asyncio.get_event_loop()
1281 for signame in ('SIGINT', 'SIGTERM'):
1282 loop.add_signal_handler(getattr(signal, signame),
1283 functools.partial(ask_exit, signame))
1284
Serhiy Storchaka0424eaf2015-09-12 17:45:25 +03001285 print("Event loop running forever, press Ctrl+C to interrupt.")
Victor Stinner8b863482014-01-27 10:07:50 +01001286 print("pid %s: send SIGINT or SIGTERM to exit." % os.getpid())
Victor Stinner63b21a82014-07-05 15:38:59 +02001287 try:
1288 loop.run_forever()
1289 finally:
1290 loop.close()
Victor Stinner2cef3002014-10-23 22:38:46 +02001291
1292This example only works on UNIX.