blob: d992b0011dc6607500bbb68eeca6dfd8a3ea1745 [file] [log] [blame]
R David Murray6a143812013-12-20 14:37:39 -05001.. currentmodule:: asyncio
Victor Stinnerea3183f2013-12-03 01:08:00 +01002
Yury Selivanov3faaa882018-09-14 13:32:07 -07003
4====================
5Coroutines and Tasks
Victor Stinnerea3183f2013-12-03 01:08:00 +01006====================
7
Yury Selivanov3faaa882018-09-14 13:32:07 -07008This section outlines high-level asyncio APIs to work with coroutines
9and Tasks.
lf627d2c82017-07-25 17:03:51 -060010
Yury Selivanov3faaa882018-09-14 13:32:07 -070011.. contents::
12 :depth: 1
13 :local:
14
lf627d2c82017-07-25 17:03:51 -060015
Victor Stinnerea3183f2013-12-03 01:08:00 +010016.. _coroutine:
17
18Coroutines
Yury Selivanov3faaa882018-09-14 13:32:07 -070019==========
Victor Stinnerea3183f2013-12-03 01:08:00 +010020
Kyle Stanleyf9000642019-10-10 19:18:46 -040021:term:`Coroutines <coroutine>` declared with the async/await syntax is the
22preferred way of writing asyncio applications. For example, the following
23snippet of code (requires Python 3.7+) prints "hello", waits 1 second,
Yury Selivanovb042cf12018-09-18 02:47:54 -040024and then prints "world"::
Victor Stinnerea3183f2013-12-03 01:08:00 +010025
Yury Selivanov3faaa882018-09-14 13:32:07 -070026 >>> import asyncio
Victor Stinnerea3183f2013-12-03 01:08:00 +010027
Yury Selivanov3faaa882018-09-14 13:32:07 -070028 >>> async def main():
29 ... print('hello')
30 ... await asyncio.sleep(1)
31 ... print('world')
Victor Stinnerea3183f2013-12-03 01:08:00 +010032
Yury Selivanov3faaa882018-09-14 13:32:07 -070033 >>> asyncio.run(main())
34 hello
35 world
Victor Stinnerea3183f2013-12-03 01:08:00 +010036
Yury Selivanov3faaa882018-09-14 13:32:07 -070037Note that simply calling a coroutine will not schedule it to
38be executed::
Victor Stinnerea3183f2013-12-03 01:08:00 +010039
Yury Selivanov3faaa882018-09-14 13:32:07 -070040 >>> main()
41 <coroutine object main at 0x1053bb7c8>
Victor Stinnerea3183f2013-12-03 01:08:00 +010042
Boštjan Mejak1d5bdef2019-05-19 11:01:36 +020043To actually run a coroutine, asyncio provides three main mechanisms:
Victor Stinnerea3183f2013-12-03 01:08:00 +010044
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040045* The :func:`asyncio.run` function to run the top-level
Yury Selivanov3faaa882018-09-14 13:32:07 -070046 entry point "main()" function (see the above example.)
Victor Stinnerea3183f2013-12-03 01:08:00 +010047
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040048* Awaiting on a coroutine. The following snippet of code will
Yury Selivanov3faaa882018-09-14 13:32:07 -070049 print "hello" after waiting for 1 second, and then print "world"
50 after waiting for *another* 2 seconds::
Victor Stinnerea3183f2013-12-03 01:08:00 +010051
Yury Selivanov3faaa882018-09-14 13:32:07 -070052 import asyncio
53 import time
Victor Stinnerea3183f2013-12-03 01:08:00 +010054
Yury Selivanov3faaa882018-09-14 13:32:07 -070055 async def say_after(delay, what):
56 await asyncio.sleep(delay)
57 print(what)
58
59 async def main():
Mariatta9f43fbb2018-10-24 15:37:12 -070060 print(f"started at {time.strftime('%X')}")
Yury Selivanov3faaa882018-09-14 13:32:07 -070061
62 await say_after(1, 'hello')
63 await say_after(2, 'world')
64
Mariatta9f43fbb2018-10-24 15:37:12 -070065 print(f"finished at {time.strftime('%X')}")
Yury Selivanov3faaa882018-09-14 13:32:07 -070066
67 asyncio.run(main())
68
69 Expected output::
70
71 started at 17:13:52
72 hello
73 world
74 finished at 17:13:55
75
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040076* The :func:`asyncio.create_task` function to run coroutines
Yury Selivanov3faaa882018-09-14 13:32:07 -070077 concurrently as asyncio :class:`Tasks <Task>`.
78
Danny Hermes7bfbda42018-09-17 21:49:21 -070079 Let's modify the above example and run two ``say_after`` coroutines
Yury Selivanov3faaa882018-09-14 13:32:07 -070080 *concurrently*::
81
82 async def main():
83 task1 = asyncio.create_task(
84 say_after(1, 'hello'))
85
86 task2 = asyncio.create_task(
87 say_after(2, 'world'))
88
Mariatta9f43fbb2018-10-24 15:37:12 -070089 print(f"started at {time.strftime('%X')}")
Yury Selivanov3faaa882018-09-14 13:32:07 -070090
91 # Wait until both tasks are completed (should take
92 # around 2 seconds.)
93 await task1
94 await task2
95
Mariatta9f43fbb2018-10-24 15:37:12 -070096 print(f"finished at {time.strftime('%X')}")
Yury Selivanov3faaa882018-09-14 13:32:07 -070097
98 Note that expected output now shows that the snippet runs
99 1 second faster than before::
100
101 started at 17:14:32
102 hello
103 world
104 finished at 17:14:34
105
Yury Selivanov47150392018-09-18 17:55:44 -0400106
107.. _asyncio-awaitables:
108
109Awaitables
110==========
111
Yury Selivanove247b462018-09-20 12:43:59 -0400112We say that an object is an **awaitable** object if it can be used
113in an :keyword:`await` expression. Many asyncio APIs are designed to
114accept awaitables.
115
116There are three main types of *awaitable* objects:
117**coroutines**, **Tasks**, and **Futures**.
Yury Selivanov47150392018-09-18 17:55:44 -0400118
119
Yury Selivanove247b462018-09-20 12:43:59 -0400120.. rubric:: Coroutines
Yury Selivanov47150392018-09-18 17:55:44 -0400121
Yury Selivanove247b462018-09-20 12:43:59 -0400122Python coroutines are *awaitables* and therefore can be awaited from
123other coroutines::
124
125 import asyncio
Yury Selivanov47150392018-09-18 17:55:44 -0400126
127 async def nested():
128 return 42
129
130 async def main():
Yury Selivanove247b462018-09-20 12:43:59 -0400131 # Nothing happens if we just call "nested()".
Yury Selivanovdb1a80e2018-09-21 16:23:15 -0400132 # A coroutine object is created but not awaited,
133 # so it *won't run at all*.
Yury Selivanove247b462018-09-20 12:43:59 -0400134 nested()
135
136 # Let's do it differently now and await it:
137 print(await nested()) # will print "42".
138
139 asyncio.run(main())
140
141.. important::
142
143 In this documentation the term "coroutine" can be used for
144 two closely related concepts:
145
146 * a *coroutine function*: an :keyword:`async def` function;
147
148 * a *coroutine object*: an object returned by calling a
149 *coroutine function*.
150
151asyncio also supports legacy :ref:`generator-based
152<asyncio_generator_based_coro>` coroutines.
153
154
155.. rubric:: Tasks
Yury Selivanov47150392018-09-18 17:55:44 -0400156
157*Tasks* are used to schedule coroutines *concurrently*.
Yury Selivanov47150392018-09-18 17:55:44 -0400158
Yury Selivanove247b462018-09-20 12:43:59 -0400159When a coroutine is wrapped into a *Task* with functions like
160:func:`asyncio.create_task` the coroutine is automatically
161scheduled to run soon::
Yury Selivanov3faaa882018-09-14 13:32:07 -0700162
Yury Selivanove247b462018-09-20 12:43:59 -0400163 import asyncio
Yury Selivanov3faaa882018-09-14 13:32:07 -0700164
Yury Selivanove247b462018-09-20 12:43:59 -0400165 async def nested():
166 return 42
167
168 async def main():
169 # Schedule nested() to run soon concurrently
170 # with "main()".
171 task = asyncio.create_task(nested())
172
173 # "task" can now be used to cancel "nested()", or
174 # can simply be awaited to wait until it is complete:
175 await task
176
177 asyncio.run(main())
Victor Stinner337e03f2014-08-11 01:11:13 +0200178
Victor Stinnerea3183f2013-12-03 01:08:00 +0100179
Yury Selivanov47150392018-09-18 17:55:44 -0400180.. rubric:: Futures
181
Yury Selivanove247b462018-09-20 12:43:59 -0400182A :class:`Future` is a special **low-level** awaitable object that
183represents an **eventual result** of an asynchronous operation.
Yury Selivanov47150392018-09-18 17:55:44 -0400184
Yury Selivanove247b462018-09-20 12:43:59 -0400185When a Future object is *awaited* it means that the coroutine will
186wait until the Future is resolved in some other place.
187
Yury Selivanov47150392018-09-18 17:55:44 -0400188Future objects in asyncio are needed to allow callback-based code
189to be used with async/await.
190
Yury Selivanove247b462018-09-20 12:43:59 -0400191Normally **there is no need** to create Future objects at the
Yury Selivanov47150392018-09-18 17:55:44 -0400192application level code.
193
194Future objects, sometimes exposed by libraries and some asyncio
Yury Selivanove247b462018-09-20 12:43:59 -0400195APIs, can be awaited::
Yury Selivanov47150392018-09-18 17:55:44 -0400196
197 async def main():
198 await function_that_returns_a_future_object()
199
200 # this is also valid:
201 await asyncio.gather(
202 function_that_returns_a_future_object(),
203 some_python_coroutine()
204 )
205
Yury Selivanove247b462018-09-20 12:43:59 -0400206A good example of a low-level function that returns a Future object
207is :meth:`loop.run_in_executor`.
208
Yury Selivanov47150392018-09-18 17:55:44 -0400209
Yury Selivanov3faaa882018-09-14 13:32:07 -0700210Running an asyncio Program
211==========================
Victor Stinnerea3183f2013-12-03 01:08:00 +0100212
Elvis Pranskevichus63536bd2018-05-19 23:15:06 -0400213.. function:: run(coro, \*, debug=False)
Yury Selivanov02a0a192017-12-14 09:42:21 -0500214
Kyle Stanleye4070132019-09-30 20:12:21 -0400215 Execute the :term:`coroutine` *coro* and return the result.
216
Yury Selivanov02a0a192017-12-14 09:42:21 -0500217 This function runs the passed coroutine, taking care of
Kyle Stanley9fdc64c2019-09-19 08:47:22 -0400218 managing the asyncio event loop, *finalizing asynchronous
219 generators*, and closing the threadpool.
Yury Selivanov02a0a192017-12-14 09:42:21 -0500220
221 This function cannot be called when another asyncio event loop is
222 running in the same thread.
223
Yury Selivanov3faaa882018-09-14 13:32:07 -0700224 If *debug* is ``True``, the event loop will be run in debug mode.
Yury Selivanov02a0a192017-12-14 09:42:21 -0500225
226 This function always creates a new event loop and closes it at
227 the end. It should be used as a main entry point for asyncio
228 programs, and should ideally only be called once.
229
Emmanuel Arias17deb162019-09-25 05:53:49 -0300230 Example::
231
232 async def main():
233 await asyncio.sleep(1)
234 print('hello')
235
236 asyncio.run(main())
237
Yury Selivanov02a0a192017-12-14 09:42:21 -0500238 .. versionadded:: 3.7
239
Kyle Stanley9fdc64c2019-09-19 08:47:22 -0400240 .. versionchanged:: 3.9
241 Updated to use :meth:`loop.shutdown_default_executor`.
Yury Selivanov02a0a192017-12-14 09:42:21 -0500242
Kyle Stanleyf9000642019-10-10 19:18:46 -0400243 .. note::
244 The source code for ``asyncio.run()`` can be found in
245 :source:`Lib/asyncio/runners.py`.
246
Yury Selivanov3faaa882018-09-14 13:32:07 -0700247Creating Tasks
248==============
Victor Stinnerea3183f2013-12-03 01:08:00 +0100249
Alex Grönholmcca4eec2018-08-09 00:06:47 +0300250.. function:: create_task(coro, \*, name=None)
Andrew Svetlovf74ef452017-12-15 07:04:38 +0200251
Yury Selivanove247b462018-09-20 12:43:59 -0400252 Wrap the *coro* :ref:`coroutine <coroutine>` into a :class:`Task`
253 and schedule its execution. Return the Task object.
Alex Grönholmcca4eec2018-08-09 00:06:47 +0300254
255 If *name* is not ``None``, it is set as the name of the task using
256 :meth:`Task.set_name`.
Andrew Svetlovf74ef452017-12-15 07:04:38 +0200257
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400258 The task is executed in the loop returned by :func:`get_running_loop`,
Andrew Svetlovf74ef452017-12-15 07:04:38 +0200259 :exc:`RuntimeError` is raised if there is no running loop in
260 current thread.
261
Yury Selivanov47150392018-09-18 17:55:44 -0400262 This function has been **added in Python 3.7**. Prior to
263 Python 3.7, the low-level :func:`asyncio.ensure_future` function
264 can be used instead::
265
266 async def coro():
267 ...
268
269 # In Python 3.7+
270 task = asyncio.create_task(coro())
271 ...
272
273 # This works in all Python versions but is less readable
274 task = asyncio.ensure_future(coro())
275 ...
276
Andrew Svetlovf74ef452017-12-15 07:04:38 +0200277 .. versionadded:: 3.7
278
Alex Grönholmcca4eec2018-08-09 00:06:47 +0300279 .. versionchanged:: 3.8
280 Added the ``name`` parameter.
281
Victor Stinnerea3183f2013-12-03 01:08:00 +0100282
Yury Selivanov3faaa882018-09-14 13:32:07 -0700283Sleeping
284========
Andrew Svetlovf1240162016-01-11 14:40:35 +0200285
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100286.. coroutinefunction:: sleep(delay, result=None, \*, loop=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100287
Yury Selivanov3faaa882018-09-14 13:32:07 -0700288 Block for *delay* seconds.
289
290 If *result* is provided, it is returned to the caller
Eli Bendersky2d26af82014-01-20 06:59:23 -0800291 when the coroutine completes.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100292
Hrvoje Nikšićcd602b82018-10-01 12:09:38 +0200293 ``sleep()`` always suspends the current task, allowing other tasks
294 to run.
295
Matthias Bussonnierd0ebf132019-05-20 23:20:10 -0700296 .. deprecated-removed:: 3.8 3.10
297 The *loop* parameter.
Yury Selivanov47150392018-09-18 17:55:44 -0400298
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700299 .. _asyncio_example_sleep:
Victor Stinner45b27ed2014-02-01 02:36:43 +0100300
Yury Selivanov3faaa882018-09-14 13:32:07 -0700301 Example of coroutine displaying the current date every second
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400302 for 5 seconds::
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100303
Yury Selivanov3faaa882018-09-14 13:32:07 -0700304 import asyncio
305 import datetime
Victor Stinnerea3183f2013-12-03 01:08:00 +0100306
Yury Selivanov3faaa882018-09-14 13:32:07 -0700307 async def display_date():
308 loop = asyncio.get_running_loop()
309 end_time = loop.time() + 5.0
310 while True:
311 print(datetime.datetime.now())
312 if (loop.time() + 1.0) >= end_time:
313 break
314 await asyncio.sleep(1)
315
316 asyncio.run(display_date())
317
318
319Running Tasks Concurrently
320==========================
321
Yury Selivanove247b462018-09-20 12:43:59 -0400322.. awaitablefunction:: gather(\*aws, loop=None, return_exceptions=False)
Yury Selivanov3faaa882018-09-14 13:32:07 -0700323
Yury Selivanove247b462018-09-20 12:43:59 -0400324 Run :ref:`awaitable objects <asyncio-awaitables>` in the *aws*
Yury Selivanov47150392018-09-18 17:55:44 -0400325 sequence *concurrently*.
Yury Selivanov3faaa882018-09-14 13:32:07 -0700326
Yury Selivanove247b462018-09-20 12:43:59 -0400327 If any awaitable in *aws* is a coroutine, it is automatically
Yury Selivanov47150392018-09-18 17:55:44 -0400328 scheduled as a Task.
Yury Selivanov3faaa882018-09-14 13:32:07 -0700329
Yury Selivanov47150392018-09-18 17:55:44 -0400330 If all awaitables are completed successfully, the result is an
331 aggregate list of returned values. The order of result values
Yury Selivanove247b462018-09-20 12:43:59 -0400332 corresponds to the order of awaitables in *aws*.
Yury Selivanov3faaa882018-09-14 13:32:07 -0700333
Yury Selivanovdb1a80e2018-09-21 16:23:15 -0400334 If *return_exceptions* is ``False`` (default), the first
335 raised exception is immediately propagated to the task that
336 awaits on ``gather()``. Other awaitables in the *aws* sequence
337 **won't be cancelled** and will continue to run.
338
Yury Selivanov47150392018-09-18 17:55:44 -0400339 If *return_exceptions* is ``True``, exceptions are treated the
340 same as successful results, and aggregated in the result list.
Yury Selivanov3faaa882018-09-14 13:32:07 -0700341
Yury Selivanovdb1a80e2018-09-21 16:23:15 -0400342 If ``gather()`` is *cancelled*, all submitted awaitables
Yury Selivanov3faaa882018-09-14 13:32:07 -0700343 (that have not completed yet) are also *cancelled*.
344
Yury Selivanove247b462018-09-20 12:43:59 -0400345 If any Task or Future from the *aws* sequence is *cancelled*, it is
Yury Selivanov47150392018-09-18 17:55:44 -0400346 treated as if it raised :exc:`CancelledError` -- the ``gather()``
347 call is **not** cancelled in this case. This is to prevent the
348 cancellation of one submitted Task/Future to cause other
349 Tasks/Futures to be cancelled.
Yury Selivanov3faaa882018-09-14 13:32:07 -0700350
Andrew Svetlova4888792019-09-12 15:40:40 +0300351 .. deprecated-removed:: 3.8 3.10
352 The *loop* parameter.
353
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700354 .. _asyncio_example_gather:
355
Yury Selivanov3faaa882018-09-14 13:32:07 -0700356 Example::
357
358 import asyncio
359
360 async def factorial(name, number):
361 f = 1
362 for i in range(2, number + 1):
363 print(f"Task {name}: Compute factorial({i})...")
364 await asyncio.sleep(1)
365 f *= i
366 print(f"Task {name}: factorial({number}) = {f}")
367
368 async def main():
Yury Selivanov47150392018-09-18 17:55:44 -0400369 # Schedule three calls *concurrently*:
Yury Selivanov3faaa882018-09-14 13:32:07 -0700370 await asyncio.gather(
371 factorial("A", 2),
372 factorial("B", 3),
373 factorial("C", 4),
Miguel Ángel García9c53fa62018-09-18 08:01:26 +0200374 )
Yury Selivanov3faaa882018-09-14 13:32:07 -0700375
376 asyncio.run(main())
377
378 # Expected output:
379 #
380 # Task A: Compute factorial(2)...
381 # Task B: Compute factorial(2)...
382 # Task C: Compute factorial(2)...
383 # Task A: factorial(2) = 2
384 # Task B: Compute factorial(3)...
385 # Task C: Compute factorial(3)...
386 # Task B: factorial(3) = 6
387 # Task C: Compute factorial(4)...
388 # Task C: factorial(4) = 24
389
Yury Selivanov47150392018-09-18 17:55:44 -0400390 .. versionchanged:: 3.7
391 If the *gather* itself is cancelled, the cancellation is
392 propagated regardless of *return_exceptions*.
393
Yury Selivanov3faaa882018-09-14 13:32:07 -0700394
Yury Selivanovdb1a80e2018-09-21 16:23:15 -0400395Shielding From Cancellation
396===========================
Yury Selivanov3faaa882018-09-14 13:32:07 -0700397
Yury Selivanove247b462018-09-20 12:43:59 -0400398.. awaitablefunction:: shield(aw, \*, loop=None)
Yury Selivanov3faaa882018-09-14 13:32:07 -0700399
Yury Selivanov47150392018-09-18 17:55:44 -0400400 Protect an :ref:`awaitable object <asyncio-awaitables>`
401 from being :meth:`cancelled <Task.cancel>`.
Yury Selivanov3faaa882018-09-14 13:32:07 -0700402
Yury Selivanovdb1a80e2018-09-21 16:23:15 -0400403 If *aw* is a coroutine it is automatically scheduled as a Task.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100404
405 The statement::
406
Andrew Svetlov88743422017-12-11 17:35:49 +0200407 res = await shield(something())
Victor Stinnerea3183f2013-12-03 01:08:00 +0100408
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400409 is equivalent to::
Victor Stinnerea3183f2013-12-03 01:08:00 +0100410
Andrew Svetlov88743422017-12-11 17:35:49 +0200411 res = await something()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100412
Yury Selivanov3faaa882018-09-14 13:32:07 -0700413 *except* that if the coroutine containing it is cancelled, the
414 Task running in ``something()`` is not cancelled. From the point
415 of view of ``something()``, the cancellation did not happen.
416 Although its caller is still cancelled, so the "await" expression
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400417 still raises a :exc:`CancelledError`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100418
Yury Selivanov3faaa882018-09-14 13:32:07 -0700419 If ``something()`` is cancelled by other means (i.e. from within
420 itself) that would also cancel ``shield()``.
421
422 If it is desired to completely ignore cancellation (not recommended)
423 the ``shield()`` function should be combined with a try/except
424 clause, as follows::
Victor Stinnerea3183f2013-12-03 01:08:00 +0100425
426 try:
Andrew Svetlov88743422017-12-11 17:35:49 +0200427 res = await shield(something())
Victor Stinnerea3183f2013-12-03 01:08:00 +0100428 except CancelledError:
429 res = None
430
Andrew Svetlova4888792019-09-12 15:40:40 +0300431 .. deprecated-removed:: 3.8 3.10
432 The *loop* parameter.
433
Yury Selivanov950204d2016-05-16 16:23:00 -0400434
Yury Selivanov3faaa882018-09-14 13:32:07 -0700435Timeouts
436========
437
Yury Selivanove247b462018-09-20 12:43:59 -0400438.. coroutinefunction:: wait_for(aw, timeout, \*, loop=None)
Yury Selivanov3faaa882018-09-14 13:32:07 -0700439
Yury Selivanove247b462018-09-20 12:43:59 -0400440 Wait for the *aw* :ref:`awaitable <asyncio-awaitables>`
Yury Selivanov47150392018-09-18 17:55:44 -0400441 to complete with a timeout.
Yury Selivanov3faaa882018-09-14 13:32:07 -0700442
Yury Selivanove247b462018-09-20 12:43:59 -0400443 If *aw* is a coroutine it is automatically scheduled as a Task.
Yury Selivanov3faaa882018-09-14 13:32:07 -0700444
445 *timeout* can either be ``None`` or a float or int number of seconds
446 to wait for. If *timeout* is ``None``, block until the future
447 completes.
448
449 If a timeout occurs, it cancels the task and raises
450 :exc:`asyncio.TimeoutError`.
451
Yury Selivanov47150392018-09-18 17:55:44 -0400452 To avoid the task :meth:`cancellation <Task.cancel>`,
453 wrap it in :func:`shield`.
Yury Selivanov3faaa882018-09-14 13:32:07 -0700454
455 The function will wait until the future is actually cancelled,
456 so the total wait time may exceed the *timeout*.
457
Yury Selivanove247b462018-09-20 12:43:59 -0400458 If the wait is cancelled, the future *aw* is also cancelled.
Yury Selivanov3faaa882018-09-14 13:32:07 -0700459
Matthias Bussonnierd0ebf132019-05-20 23:20:10 -0700460 .. deprecated-removed:: 3.8 3.10
461 The *loop* parameter.
Yury Selivanov47150392018-09-18 17:55:44 -0400462
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700463 .. _asyncio_example_waitfor:
464
Yury Selivanov3faaa882018-09-14 13:32:07 -0700465 Example::
466
467 async def eternity():
468 # Sleep for one hour
469 await asyncio.sleep(3600)
470 print('yay!')
471
472 async def main():
473 # Wait for at most 1 second
474 try:
475 await asyncio.wait_for(eternity(), timeout=1.0)
476 except asyncio.TimeoutError:
477 print('timeout!')
478
479 asyncio.run(main())
480
481 # Expected output:
482 #
483 # timeout!
484
485 .. versionchanged:: 3.7
Yury Selivanove247b462018-09-20 12:43:59 -0400486 When *aw* is cancelled due to a timeout, ``wait_for`` waits
487 for *aw* to be cancelled. Previously, it raised
Yury Selivanov3faaa882018-09-14 13:32:07 -0700488 :exc:`asyncio.TimeoutError` immediately.
489
490
491Waiting Primitives
492==================
493
Yury Selivanove247b462018-09-20 12:43:59 -0400494.. coroutinefunction:: wait(aws, \*, loop=None, timeout=None,\
Andrew Svetlovf1240162016-01-11 14:40:35 +0200495 return_when=ALL_COMPLETED)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100496
Yury Selivanove247b462018-09-20 12:43:59 -0400497 Run :ref:`awaitable objects <asyncio-awaitables>` in the *aws*
Yury Selivanov996859a2018-09-25 14:51:21 -0400498 set concurrently and block until the condition specified
Yury Selivanov47150392018-09-18 17:55:44 -0400499 by *return_when*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100500
Yury Selivanov3faaa882018-09-14 13:32:07 -0700501 Returns two sets of Tasks/Futures: ``(done, pending)``.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100502
Yury Selivanov996859a2018-09-25 14:51:21 -0400503 Usage::
504
505 done, pending = await asyncio.wait(aws)
506
Yury Selivanov3faaa882018-09-14 13:32:07 -0700507 *timeout* (a float or int), if specified, can be used to control
508 the maximum number of seconds to wait before returning.
509
510 Note that this function does not raise :exc:`asyncio.TimeoutError`.
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400511 Futures or Tasks that aren't done when the timeout occurs are simply
Yury Selivanov3faaa882018-09-14 13:32:07 -0700512 returned in the second set.
513
514 *return_when* indicates when this function should return. It must
515 be one of the following constants:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100516
517 .. tabularcolumns:: |l|L|
518
519 +-----------------------------+----------------------------------------+
520 | Constant | Description |
521 +=============================+========================================+
522 | :const:`FIRST_COMPLETED` | The function will return when any |
523 | | future finishes or is cancelled. |
524 +-----------------------------+----------------------------------------+
525 | :const:`FIRST_EXCEPTION` | The function will return when any |
526 | | future finishes by raising an |
527 | | exception. If no future raises an |
528 | | exception then it is equivalent to |
529 | | :const:`ALL_COMPLETED`. |
530 +-----------------------------+----------------------------------------+
531 | :const:`ALL_COMPLETED` | The function will return when all |
532 | | futures finish or are cancelled. |
533 +-----------------------------+----------------------------------------+
534
Yury Selivanov3faaa882018-09-14 13:32:07 -0700535 Unlike :func:`~asyncio.wait_for`, ``wait()`` does not cancel the
536 futures when a timeout occurs.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100537
Andrew Svetlova4888792019-09-12 15:40:40 +0300538 .. deprecated:: 3.8
539
540 If any awaitable in *aws* is a coroutine, it is automatically
541 scheduled as a Task. Passing coroutines objects to
542 ``wait()`` directly is deprecated as it leads to
543 :ref:`confusing behavior <asyncio_example_wait_coroutine>`.
544
545 .. deprecated-removed:: 3.8 3.10
546
547 The *loop* parameter.
548
Yury Selivanov996859a2018-09-25 14:51:21 -0400549 .. _asyncio_example_wait_coroutine:
550 .. note::
Victor Stinnerea3183f2013-12-03 01:08:00 +0100551
Yury Selivanov996859a2018-09-25 14:51:21 -0400552 ``wait()`` schedules coroutines as Tasks automatically and later
553 returns those implicitly created Task objects in ``(done, pending)``
554 sets. Therefore the following code won't work as expected::
555
556 async def foo():
557 return 42
558
559 coro = foo()
560 done, pending = await asyncio.wait({coro})
561
562 if coro in done:
563 # This branch will never be run!
564
565 Here is how the above snippet can be fixed::
566
567 async def foo():
568 return 42
569
570 task = asyncio.create_task(foo())
571 done, pending = await asyncio.wait({task})
572
573 if task in done:
574 # Everything will work as expected now.
575
Matthias Bussonnierd0ebf132019-05-20 23:20:10 -0700576 .. deprecated:: 3.8
577
Yury Selivanov996859a2018-09-25 14:51:21 -0400578 Passing coroutine objects to ``wait()`` directly is
579 deprecated.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100580
Victor Stinnerea3183f2013-12-03 01:08:00 +0100581
Yury Selivanove247b462018-09-20 12:43:59 -0400582.. function:: as_completed(aws, \*, loop=None, timeout=None)
Yury Selivanov3faaa882018-09-14 13:32:07 -0700583
Yury Selivanove247b462018-09-20 12:43:59 -0400584 Run :ref:`awaitable objects <asyncio-awaitables>` in the *aws*
Yury Selivanov47150392018-09-18 17:55:44 -0400585 set concurrently. Return an iterator of :class:`Future` objects.
586 Each Future object returned represents the earliest result
587 from the set of the remaining awaitables.
Yury Selivanov3faaa882018-09-14 13:32:07 -0700588
589 Raises :exc:`asyncio.TimeoutError` if the timeout occurs before
590 all Futures are done.
591
Andrew Svetlova4888792019-09-12 15:40:40 +0300592 .. deprecated-removed:: 3.8 3.10
593 The *loop* parameter.
594
Yury Selivanov3faaa882018-09-14 13:32:07 -0700595 Example::
596
Yury Selivanove247b462018-09-20 12:43:59 -0400597 for f in as_completed(aws):
Yury Selivanov47150392018-09-18 17:55:44 -0400598 earliest_result = await f
Yury Selivanov3faaa882018-09-14 13:32:07 -0700599 # ...
Victor Stinnerea3183f2013-12-03 01:08:00 +0100600
Victor Stinner3e09e322013-12-03 01:22:06 +0100601
Yury Selivanov3faaa882018-09-14 13:32:07 -0700602Scheduling From Other Threads
603=============================
Victor Stinner1ad5afc2014-01-30 00:18:50 +0100604
Yury Selivanov3faaa882018-09-14 13:32:07 -0700605.. function:: run_coroutine_threadsafe(coro, loop)
Victor Stinner1ad5afc2014-01-30 00:18:50 +0100606
Yury Selivanov3faaa882018-09-14 13:32:07 -0700607 Submit a coroutine to the given event loop. Thread-safe.
Victor Stinner1ad5afc2014-01-30 00:18:50 +0100608
Yury Selivanov47150392018-09-18 17:55:44 -0400609 Return a :class:`concurrent.futures.Future` to wait for the result
610 from another OS thread.
Victor Stinner1ad5afc2014-01-30 00:18:50 +0100611
Yury Selivanov3faaa882018-09-14 13:32:07 -0700612 This function is meant to be called from a different OS thread
613 than the one where the event loop is running. Example::
Victor Stinner72dcb0a2015-04-03 17:08:19 +0200614
Yury Selivanov3faaa882018-09-14 13:32:07 -0700615 # Create a coroutine
616 coro = asyncio.sleep(1, result=3)
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500617
Yury Selivanov3faaa882018-09-14 13:32:07 -0700618 # Submit the coroutine to a given loop
619 future = asyncio.run_coroutine_threadsafe(coro, loop)
Victor Stinner1ad5afc2014-01-30 00:18:50 +0100620
Yury Selivanov3faaa882018-09-14 13:32:07 -0700621 # Wait for the result with an optional timeout argument
622 assert future.result(timeout) == 3
623
624 If an exception is raised in the coroutine, the returned Future
625 will be notified. It can also be used to cancel the task in
626 the event loop::
627
628 try:
629 result = future.result(timeout)
630 except asyncio.TimeoutError:
631 print('The coroutine took too long, cancelling the task...')
632 future.cancel()
633 except Exception as exc:
Mariatta9f43fbb2018-10-24 15:37:12 -0700634 print(f'The coroutine raised an exception: {exc!r}')
Yury Selivanov3faaa882018-09-14 13:32:07 -0700635 else:
Mariatta9f43fbb2018-10-24 15:37:12 -0700636 print(f'The coroutine returned: {result!r}')
Yury Selivanov3faaa882018-09-14 13:32:07 -0700637
638 See the :ref:`concurrency and multithreading <asyncio-multithreading>`
639 section of the documentation.
640
Vaibhav Gupta3a810762018-12-26 20:17:17 +0530641 Unlike other asyncio functions this function requires the *loop*
Yury Selivanov3faaa882018-09-14 13:32:07 -0700642 argument to be passed explicitly.
643
644 .. versionadded:: 3.5.1
645
646
647Introspection
648=============
649
650
651.. function:: current_task(loop=None)
652
653 Return the currently running :class:`Task` instance, or ``None`` if
654 no task is running.
655
656 If *loop* is ``None`` :func:`get_running_loop` is used to get
657 the current loop.
658
659 .. versionadded:: 3.7
660
661
662.. function:: all_tasks(loop=None)
663
664 Return a set of not yet finished :class:`Task` objects run by
665 the loop.
666
667 If *loop* is ``None``, :func:`get_running_loop` is used for getting
668 current loop.
669
670 .. versionadded:: 3.7
671
672
673Task Object
674===========
675
676.. class:: Task(coro, \*, loop=None, name=None)
677
Yury Selivanovdb1a80e2018-09-21 16:23:15 -0400678 A :class:`Future-like <Future>` object that runs a Python
Yury Selivanov3faaa882018-09-14 13:32:07 -0700679 :ref:`coroutine <coroutine>`. Not thread-safe.
680
681 Tasks are used to run coroutines in event loops.
682 If a coroutine awaits on a Future, the Task suspends
683 the execution of the coroutine and waits for the completion
684 of the Future. When the Future is *done*, the execution of
685 the wrapped coroutine resumes.
686
687 Event loops use cooperative scheduling: an event loop runs
688 one Task at a time. While a Task awaits for the completion of a
689 Future, the event loop runs other Tasks, callbacks, or performs
690 IO operations.
691
692 Use the high-level :func:`asyncio.create_task` function to create
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400693 Tasks, or the low-level :meth:`loop.create_task` or
694 :func:`ensure_future` functions. Manual instantiation of Tasks
695 is discouraged.
Yury Selivanov3faaa882018-09-14 13:32:07 -0700696
697 To cancel a running Task use the :meth:`cancel` method. Calling it
698 will cause the Task to throw a :exc:`CancelledError` exception into
699 the wrapped coroutine. If a coroutine is awaiting on a Future
700 object during cancellation, the Future object will be cancelled.
701
702 :meth:`cancelled` can be used to check if the Task was cancelled.
703 The method returns ``True`` if the wrapped coroutine did not
704 suppress the :exc:`CancelledError` exception and was actually
705 cancelled.
706
707 :class:`asyncio.Task` inherits from :class:`Future` all of its
708 APIs except :meth:`Future.set_result` and
709 :meth:`Future.set_exception`.
710
711 Tasks support the :mod:`contextvars` module. When a Task
712 is created it copies the current context and later runs its
713 coroutine in the copied context.
Elvis Pranskevichuse2b340a2018-05-29 17:31:01 -0400714
715 .. versionchanged:: 3.7
Yury Selivanov3faaa882018-09-14 13:32:07 -0700716 Added support for the :mod:`contextvars` module.
717
718 .. versionchanged:: 3.8
719 Added the ``name`` parameter.
720
Andrew Svetlova4888792019-09-12 15:40:40 +0300721 .. deprecated-removed:: 3.8 3.10
722 The *loop* parameter.
723
Yury Selivanov3faaa882018-09-14 13:32:07 -0700724 .. method:: cancel()
725
726 Request the Task to be cancelled.
727
728 This arranges for a :exc:`CancelledError` exception to be thrown
729 into the wrapped coroutine on the next cycle of the event loop.
730
731 The coroutine then has a chance to clean up or even deny the
732 request by suppressing the exception with a :keyword:`try` ...
733 ... ``except CancelledError`` ... :keyword:`finally` block.
734 Therefore, unlike :meth:`Future.cancel`, :meth:`Task.cancel` does
735 not guarantee that the Task will be cancelled, although
736 suppressing cancellation completely is not common and is actively
737 discouraged.
738
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700739 .. _asyncio_example_task_cancel:
740
Yury Selivanov3faaa882018-09-14 13:32:07 -0700741 The following example illustrates how coroutines can intercept
742 the cancellation request::
743
744 async def cancel_me():
745 print('cancel_me(): before sleep')
746
747 try:
748 # Wait for 1 hour
749 await asyncio.sleep(3600)
750 except asyncio.CancelledError:
751 print('cancel_me(): cancel sleep')
752 raise
753 finally:
754 print('cancel_me(): after sleep')
755
756 async def main():
757 # Create a "cancel_me" Task
758 task = asyncio.create_task(cancel_me())
759
760 # Wait for 1 second
761 await asyncio.sleep(1)
762
763 task.cancel()
764 try:
765 await task
766 except asyncio.CancelledError:
767 print("main(): cancel_me is cancelled now")
768
769 asyncio.run(main())
770
771 # Expected output:
772 #
773 # cancel_me(): before sleep
774 # cancel_me(): cancel sleep
775 # cancel_me(): after sleep
776 # main(): cancel_me is cancelled now
777
778 .. method:: cancelled()
779
780 Return ``True`` if the Task is *cancelled*.
781
782 The Task is *cancelled* when the cancellation was requested with
783 :meth:`cancel` and the wrapped coroutine propagated the
784 :exc:`CancelledError` exception thrown into it.
785
786 .. method:: done()
787
788 Return ``True`` if the Task is *done*.
789
790 A Task is *done* when the wrapped coroutine either returned
791 a value, raised an exception, or the Task was cancelled.
792
Yury Selivanove247b462018-09-20 12:43:59 -0400793 .. method:: result()
794
795 Return the result of the Task.
796
797 If the Task is *done*, the result of the wrapped coroutine
798 is returned (or if the coroutine raised an exception, that
799 exception is re-raised.)
800
801 If the Task has been *cancelled*, this method raises
802 a :exc:`CancelledError` exception.
803
804 If the Task's result isn't yet available, this method raises
805 a :exc:`InvalidStateError` exception.
806
807 .. method:: exception()
808
809 Return the exception of the Task.
810
811 If the wrapped coroutine raised an exception that exception
812 is returned. If the wrapped coroutine returned normally
813 this method returns ``None``.
814
815 If the Task has been *cancelled*, this method raises a
816 :exc:`CancelledError` exception.
817
818 If the Task isn't *done* yet, this method raises an
819 :exc:`InvalidStateError` exception.
820
821 .. method:: add_done_callback(callback, *, context=None)
822
823 Add a callback to be run when the Task is *done*.
824
825 This method should only be used in low-level callback-based code.
826
827 See the documentation of :meth:`Future.add_done_callback`
828 for more details.
829
830 .. method:: remove_done_callback(callback)
831
832 Remove *callback* from the callbacks list.
833
834 This method should only be used in low-level callback-based code.
835
836 See the documentation of :meth:`Future.remove_done_callback`
837 for more details.
838
Yury Selivanov3faaa882018-09-14 13:32:07 -0700839 .. method:: get_stack(\*, limit=None)
840
841 Return the list of stack frames for this Task.
842
843 If the wrapped coroutine is not done, this returns the stack
844 where it is suspended. If the coroutine has completed
845 successfully or was cancelled, this returns an empty list.
846 If the coroutine was terminated by an exception, this returns
847 the list of traceback frames.
848
849 The frames are always ordered from oldest to newest.
850
851 Only one stack frame is returned for a suspended coroutine.
852
853 The optional *limit* argument sets the maximum number of frames
854 to return; by default all available frames are returned.
855 The ordering of the returned list differs depending on whether
856 a stack or a traceback is returned: the newest frames of a
857 stack are returned, but the oldest frames of a traceback are
858 returned. (This matches the behavior of the traceback module.)
859
860 .. method:: print_stack(\*, limit=None, file=None)
861
862 Print the stack or traceback for this Task.
863
864 This produces output similar to that of the traceback module
865 for the frames retrieved by :meth:`get_stack`.
866
867 The *limit* argument is passed to :meth:`get_stack` directly.
868
869 The *file* argument is an I/O stream to which the output
870 is written; by default output is written to :data:`sys.stderr`.
871
Alex Grönholm98ef9202019-05-30 18:30:09 +0300872 .. method:: get_coro()
873
874 Return the coroutine object wrapped by the :class:`Task`.
875
876 .. versionadded:: 3.8
877
Yury Selivanov3faaa882018-09-14 13:32:07 -0700878 .. method:: get_name()
879
880 Return the name of the Task.
881
882 If no name has been explicitly assigned to the Task, the default
883 asyncio Task implementation generates a default name during
884 instantiation.
885
886 .. versionadded:: 3.8
887
888 .. method:: set_name(value)
889
890 Set the name of the Task.
891
892 The *value* argument can be any object, which is then
893 converted to a string.
894
895 In the default Task implementation, the name will be visible
896 in the :func:`repr` output of a task object.
897
898 .. versionadded:: 3.8
899
900 .. classmethod:: all_tasks(loop=None)
901
902 Return a set of all tasks for an event loop.
903
904 By default all tasks for the current event loop are returned.
905 If *loop* is ``None``, the :func:`get_event_loop` function
906 is used to get the current loop.
907
Matthias Bussonnierd0ebf132019-05-20 23:20:10 -0700908 .. deprecated-removed:: 3.7 3.9
909
910 Do not call this as a task method. Use the :func:`asyncio.all_tasks`
911 function instead.
Yury Selivanov3faaa882018-09-14 13:32:07 -0700912
913 .. classmethod:: current_task(loop=None)
914
915 Return the currently running task or ``None``.
916
917 If *loop* is ``None``, the :func:`get_event_loop` function
918 is used to get the current loop.
919
Matthias Bussonnierd0ebf132019-05-20 23:20:10 -0700920 .. deprecated-removed:: 3.7 3.9
921
922 Do not call this as a task method. Use the
923 :func:`asyncio.current_task` function instead.
Yury Selivanov3faaa882018-09-14 13:32:07 -0700924
925
926.. _asyncio_generator_based_coro:
927
928Generator-based Coroutines
929==========================
930
931.. note::
932
933 Support for generator-based coroutines is **deprecated** and
Yury Selivanovfad6af22018-09-25 17:44:52 -0400934 is scheduled for removal in Python 3.10.
Yury Selivanov3faaa882018-09-14 13:32:07 -0700935
936Generator-based coroutines predate async/await syntax. They are
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400937Python generators that use ``yield from`` expressions to await
Yury Selivanov3faaa882018-09-14 13:32:07 -0700938on Futures and other coroutines.
939
940Generator-based coroutines should be decorated with
941:func:`@asyncio.coroutine <asyncio.coroutine>`, although this is not
942enforced.
943
944
945.. decorator:: coroutine
946
947 Decorator to mark generator-based coroutines.
948
949 This decorator enables legacy generator-based coroutines to be
950 compatible with async/await code::
951
952 @asyncio.coroutine
953 def old_style_coroutine():
954 yield from asyncio.sleep(1)
955
956 async def main():
957 await old_style_coroutine()
958
Yury Selivanov3faaa882018-09-14 13:32:07 -0700959 This decorator should not be used for :keyword:`async def`
960 coroutines.
961
Andrew Svetlov68b34a72019-05-16 17:52:10 +0300962 .. deprecated-removed:: 3.8 3.10
963
964 Use :keyword:`async def` instead.
965
Yury Selivanov3faaa882018-09-14 13:32:07 -0700966.. function:: iscoroutine(obj)
967
968 Return ``True`` if *obj* is a :ref:`coroutine object <coroutine>`.
969
970 This method is different from :func:`inspect.iscoroutine` because
Yury Selivanov59ee5b12018-09-27 15:48:30 -0400971 it returns ``True`` for generator-based coroutines.
Yury Selivanov3faaa882018-09-14 13:32:07 -0700972
973.. function:: iscoroutinefunction(func)
974
975 Return ``True`` if *func* is a :ref:`coroutine function
976 <coroutine>`.
977
978 This method is different from :func:`inspect.iscoroutinefunction`
979 because it returns ``True`` for generator-based coroutine functions
980 decorated with :func:`@coroutine <coroutine>`.