blob: 4892333d8cee410237aee64a281c88394056eead [file] [log] [blame]
R David Murray6a143812013-12-20 14:37:39 -05001.. currentmodule:: asyncio
Victor Stinnerea3183f2013-12-03 01:08:00 +01002
3Tasks and coroutines
4====================
5
lf627d2c82017-07-25 17:03:51 -06006**Source code:** :source:`Lib/asyncio/tasks.py`
7
8**Source code:** :source:`Lib/asyncio/coroutines.py`
9
Victor Stinnerea3183f2013-12-03 01:08:00 +010010.. _coroutine:
11
12Coroutines
13----------
14
Yury Selivanov66f88282015-06-24 11:04:15 -040015Coroutines used with :mod:`asyncio` may be implemented using the
16:keyword:`async def` statement, or by using :term:`generators <generator>`.
17The :keyword:`async def` type of coroutine was added in Python 3.5, and
18is recommended if there is no need to support older Python versions.
Victor Stinnerea3183f2013-12-03 01:08:00 +010019
Yury Selivanov66f88282015-06-24 11:04:15 -040020Generator-based coroutines should be decorated with :func:`@asyncio.coroutine
21<asyncio.coroutine>`, although this is not strictly enforced.
22The decorator enables compatibility with :keyword:`async def` coroutines,
23and also serves as documentation. Generator-based
24coroutines use the ``yield from`` syntax introduced in :pep:`380`,
Victor Stinnerea3183f2013-12-03 01:08:00 +010025instead of the original ``yield`` syntax.
26
27The word "coroutine", like the word "generator", is used for two
28different (though related) concepts:
29
Yury Selivanov66f88282015-06-24 11:04:15 -040030- The function that defines a coroutine
31 (a function definition using :keyword:`async def` or
Victor Stinner59759ff2014-01-16 19:30:21 +010032 decorated with ``@asyncio.coroutine``). If disambiguation is needed
Victor Stinner1ad5afc2014-01-30 00:18:50 +010033 we will call this a *coroutine function* (:func:`iscoroutinefunction`
34 returns ``True``).
Victor Stinnerea3183f2013-12-03 01:08:00 +010035
36- The object obtained by calling a coroutine function. This object
37 represents a computation or an I/O operation (usually a combination)
38 that will complete eventually. If disambiguation is needed we will
Victor Stinner1ad5afc2014-01-30 00:18:50 +010039 call it a *coroutine object* (:func:`iscoroutine` returns ``True``).
Victor Stinnerea3183f2013-12-03 01:08:00 +010040
41Things a coroutine can do:
42
Yury Selivanov66f88282015-06-24 11:04:15 -040043- ``result = await future`` or ``result = yield from future`` --
44 suspends the coroutine until the
Victor Stinnerea3183f2013-12-03 01:08:00 +010045 future is done, then returns the future's result, or raises an
46 exception, which will be propagated. (If the future is cancelled,
47 it will raise a ``CancelledError`` exception.) Note that tasks are
48 futures, and everything said about futures also applies to tasks.
49
Yury Selivanov66f88282015-06-24 11:04:15 -040050- ``result = await coroutine`` or ``result = yield from coroutine`` --
51 wait for another coroutine to
Victor Stinnerea3183f2013-12-03 01:08:00 +010052 produce a result (or raise an exception, which will be propagated).
53 The ``coroutine`` expression must be a *call* to another coroutine.
54
55- ``return expression`` -- produce a result to the coroutine that is
Yury Selivanov66f88282015-06-24 11:04:15 -040056 waiting for this one using :keyword:`await` or ``yield from``.
Victor Stinnerea3183f2013-12-03 01:08:00 +010057
58- ``raise exception`` -- raise an exception in the coroutine that is
Yury Selivanov66f88282015-06-24 11:04:15 -040059 waiting for this one using :keyword:`await` or ``yield from``.
Victor Stinnerea3183f2013-12-03 01:08:00 +010060
Yury Selivanov66f88282015-06-24 11:04:15 -040061Calling a coroutine does not start its code running --
62the coroutine object returned by the call doesn't do anything until you
63schedule its execution. There are two basic ways to start it running:
64call ``await coroutine`` or ``yield from coroutine`` from another coroutine
Victor Stinner530ef2f2014-07-08 12:39:10 +020065(assuming the other coroutine is already running!), or schedule its execution
Guido van Rossumf68afd82016-08-08 09:41:21 -070066using the :func:`ensure_future` function or the :meth:`AbstractEventLoop.create_task`
Victor Stinner337e03f2014-08-11 01:11:13 +020067method.
68
Victor Stinnerea3183f2013-12-03 01:08:00 +010069
70Coroutines (and tasks) can only run when the event loop is running.
71
Victor Stinnerdb39a0d2014-01-16 18:58:01 +010072.. decorator:: coroutine
73
Yury Selivanov66f88282015-06-24 11:04:15 -040074 Decorator to mark generator-based coroutines. This enables
75 the generator use :keyword:`!yield from` to call :keyword:`async
76 def` coroutines, and also enables the generator to be called by
77 :keyword:`async def` coroutines, for instance using an
78 :keyword:`await` expression.
Victor Stinnerdb39a0d2014-01-16 18:58:01 +010079
Yury Selivanov66f88282015-06-24 11:04:15 -040080 There is no need to decorate :keyword:`async def` coroutines themselves.
81
82 If the generator is not yielded from before it is destroyed, an error
Victor Stinnerdb39a0d2014-01-16 18:58:01 +010083 message is logged. See :ref:`Detect coroutines never scheduled
84 <asyncio-coroutine-not-scheduled>`.
85
Yury Selivanov37f15bc2014-02-20 16:20:44 -050086.. note::
87
88 In this documentation, some methods are documented as coroutines,
89 even if they are plain Python functions returning a :class:`Future`.
90 This is intentional to have a freedom of tweaking the implementation
91 of these functions in the future. If such a function is needed to be
Yury Selivanov04356e12015-06-30 22:13:22 -040092 used in a callback-style code, wrap its result with :func:`ensure_future`.
Yury Selivanov37f15bc2014-02-20 16:20:44 -050093
Victor Stinnerea3183f2013-12-03 01:08:00 +010094
Elvis Pranskevichus15f3d0c2018-05-19 23:39:45 -040095.. function:: run(coro, \*, debug=False)
Yury Selivanov02a0a192017-12-14 09:42:21 -050096
97 This function runs the passed coroutine, taking care of
98 managing the asyncio event loop and finalizing asynchronous
99 generators.
100
101 This function cannot be called when another asyncio event loop is
102 running in the same thread.
103
104 If debug is True, the event loop will be run in debug mode.
105
106 This function always creates a new event loop and closes it at
107 the end. It should be used as a main entry point for asyncio
108 programs, and should ideally only be called once.
109
110 .. versionadded:: 3.7
Miss Islington (bot)f34ade32018-05-17 12:10:56 -0700111 **Important:** this has been been added to asyncio in Python 3.7
112 on a :term:`provisional basis <provisional api>`.
Yury Selivanov02a0a192017-12-14 09:42:21 -0500113
114
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100115.. _asyncio-hello-world-coroutine:
116
Victor Stinner7f314ed2014-10-15 18:49:16 +0200117Example: Hello World coroutine
118^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100119
Victor Stinner7f314ed2014-10-15 18:49:16 +0200120Example of coroutine displaying ``"Hello World"``::
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100121
122 import asyncio
123
Yury Selivanov66f88282015-06-24 11:04:15 -0400124 async def hello_world():
Victor Stinner7f314ed2014-10-15 18:49:16 +0200125 print("Hello World!")
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100126
Yury Selivanov02a0a192017-12-14 09:42:21 -0500127 asyncio.run(hello_world())
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100128
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100129.. seealso::
130
Victor Stinner7f314ed2014-10-15 18:49:16 +0200131 The :ref:`Hello World with call_soon() <asyncio-hello-world-callback>`
Guido van Rossumf68afd82016-08-08 09:41:21 -0700132 example uses the :meth:`AbstractEventLoop.call_soon` method to schedule a
Victor Stinner7f314ed2014-10-15 18:49:16 +0200133 callback.
134
135
136.. _asyncio-date-coroutine:
137
138Example: Coroutine displaying the current date
139^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
140
141Example of coroutine displaying the current date every second during 5 seconds
142using the :meth:`sleep` function::
143
144 import asyncio
145 import datetime
146
Yury Selivanov02a0a192017-12-14 09:42:21 -0500147 async def display_date():
148 loop = asyncio.get_running_loop()
Yury Selivanov66f88282015-06-24 11:04:15 -0400149 end_time = loop.time() + 5.0
150 while True:
151 print(datetime.datetime.now())
152 if (loop.time() + 1.0) >= end_time:
153 break
154 await asyncio.sleep(1)
155
Yury Selivanov02a0a192017-12-14 09:42:21 -0500156 asyncio.run(display_date())
Yury Selivanov66f88282015-06-24 11:04:15 -0400157
Victor Stinner7f314ed2014-10-15 18:49:16 +0200158.. seealso::
159
160 The :ref:`display the current date with call_later()
161 <asyncio-date-callback>` example uses a callback with the
Guido van Rossumf68afd82016-08-08 09:41:21 -0700162 :meth:`AbstractEventLoop.call_later` method.
Victor Stinner7f314ed2014-10-15 18:49:16 +0200163
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100164
165Example: Chain coroutines
166^^^^^^^^^^^^^^^^^^^^^^^^^
167
168Example chaining coroutines::
169
170 import asyncio
171
Yury Selivanov66f88282015-06-24 11:04:15 -0400172 async def compute(x, y):
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100173 print("Compute %s + %s ..." % (x, y))
Yury Selivanov66f88282015-06-24 11:04:15 -0400174 await asyncio.sleep(1.0)
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100175 return x + y
176
Yury Selivanov66f88282015-06-24 11:04:15 -0400177 async def print_sum(x, y):
178 result = await compute(x, y)
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100179 print("%s + %s = %s" % (x, y, result))
180
181 loop = asyncio.get_event_loop()
182 loop.run_until_complete(print_sum(1, 2))
Victor Stinnerf40c6632014-01-28 23:32:40 +0100183 loop.close()
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100184
185``compute()`` is chained to ``print_sum()``: ``print_sum()`` coroutine waits
Brian Curtina1afeec2014-02-08 18:36:14 -0600186until ``compute()`` is completed before returning its result.
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100187
Victor Stinner1c4b8922013-12-12 12:35:17 +0100188Sequence diagram of the example:
189
190.. image:: tulip_coro.png
191 :align: center
192
Guido van Rossumf68afd82016-08-08 09:41:21 -0700193The "Task" is created by the :meth:`AbstractEventLoop.run_until_complete` method
Victor Stinner59759ff2014-01-16 19:30:21 +0100194when it gets a coroutine object instead of a task.
Victor Stinner86e139a2013-12-13 12:51:24 +0100195
196The diagram shows the control flow, it does not describe exactly how things
197work internally. For example, the sleep coroutine creates an internal future
Guido van Rossumf68afd82016-08-08 09:41:21 -0700198which uses :meth:`AbstractEventLoop.call_later` to wake up the task in 1 second.
Victor Stinner1c4b8922013-12-12 12:35:17 +0100199
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100200
Victor Stinner99c2ab42013-12-03 19:17:25 +0100201InvalidStateError
202-----------------
203
204.. exception:: InvalidStateError
205
206 The operation is not allowed in this state.
207
208
Victor Stinner28d0ae482014-05-29 00:04:57 +0200209TimeoutError
210------------
211
212.. exception:: TimeoutError
213
214 The operation exceeded the given deadline.
215
216.. note::
217
218 This exception is different from the builtin :exc:`TimeoutError` exception!
219
220
Victor Stinner99c2ab42013-12-03 19:17:25 +0100221Future
222------
223
224.. class:: Future(\*, loop=None)
225
226 This class is *almost* compatible with :class:`concurrent.futures.Future`.
227
228 Differences:
229
230 - :meth:`result` and :meth:`exception` do not take a timeout argument and
231 raise an exception when the future isn't done yet.
232
233 - Callbacks registered with :meth:`add_done_callback` are always called
Antoine Pitrou22b11282017-11-07 17:03:28 +0100234 via the event loop's :meth:`~AbstractEventLoop.call_soon`.
Victor Stinner99c2ab42013-12-03 19:17:25 +0100235
236 - This class is not compatible with the :func:`~concurrent.futures.wait` and
237 :func:`~concurrent.futures.as_completed` functions in the
238 :mod:`concurrent.futures` package.
239
Victor Stinner83704962015-02-25 14:24:15 +0100240 This class is :ref:`not thread safe <asyncio-multithreading>`.
241
Victor Stinner99c2ab42013-12-03 19:17:25 +0100242 .. method:: cancel()
243
244 Cancel the future and schedule callbacks.
245
246 If the future is already done or cancelled, return ``False``. Otherwise,
247 change the future's state to cancelled, schedule the callbacks and return
248 ``True``.
249
250 .. method:: cancelled()
251
252 Return ``True`` if the future was cancelled.
253
254 .. method:: done()
255
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300256 Return ``True`` if the future is done.
Victor Stinner99c2ab42013-12-03 19:17:25 +0100257
258 Done means either that a result / exception are available, or that the
259 future was cancelled.
260
261 .. method:: result()
262
263 Return the result this future represents.
264
265 If the future has been cancelled, raises :exc:`CancelledError`. If the
266 future's result isn't yet available, raises :exc:`InvalidStateError`. If
267 the future is done and has an exception set, this exception is raised.
268
269 .. method:: exception()
270
271 Return the exception that was set on this future.
272
273 The exception (or ``None`` if no exception was set) is returned only if
274 the future is done. If the future has been cancelled, raises
275 :exc:`CancelledError`. If the future isn't done yet, raises
276 :exc:`InvalidStateError`.
277
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700278 .. method:: add_done_callback(callback, *, context=None)
Victor Stinner99c2ab42013-12-03 19:17:25 +0100279
280 Add a callback to be run when the future becomes done.
281
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700282 The *callback* is called with a single argument - the future object. If the
Victor Stinner99c2ab42013-12-03 19:17:25 +0100283 future is already done when this is called, the callback is scheduled
Guido van Rossumf68afd82016-08-08 09:41:21 -0700284 with :meth:`~AbstractEventLoop.call_soon`.
Victor Stinner99c2ab42013-12-03 19:17:25 +0100285
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700286 An optional keyword-only *context* argument allows specifying a custom
287 :class:`contextvars.Context` for the *callback* to run in. The current
288 context is used when no *context* is provided.
289
Victor Stinner8464c242014-11-28 13:15:41 +0100290 :ref:`Use functools.partial to pass parameters to the callback
291 <asyncio-pass-keywords>`. For example,
292 ``fut.add_done_callback(functools.partial(print, "Future:",
293 flush=True))`` will call ``print("Future:", fut, flush=True)``.
294
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700295 .. versionchanged:: 3.7
296 The *context* keyword-only parameter was added. See :pep:`567`
297 for more details.
298
Victor Stinner99c2ab42013-12-03 19:17:25 +0100299 .. method:: remove_done_callback(fn)
300
301 Remove all instances of a callback from the "call when done" list.
302
303 Returns the number of callbacks removed.
304
305 .. method:: set_result(result)
306
307 Mark the future done and set its result.
308
309 If the future is already done when this method is called, raises
310 :exc:`InvalidStateError`.
311
312 .. method:: set_exception(exception)
313
314 Mark the future done and set an exception.
315
316 If the future is already done when this method is called, raises
317 :exc:`InvalidStateError`.
318
Yury Selivanovca9b36c2017-12-23 15:04:15 -0500319 .. method:: get_loop()
320
321 Return the event loop the future object is bound to.
322
323 .. versionadded:: 3.7
324
Victor Stinner99c2ab42013-12-03 19:17:25 +0100325
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100326Example: Future with run_until_complete()
327^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
328
Victor Stinner59759ff2014-01-16 19:30:21 +0100329Example combining a :class:`Future` and a :ref:`coroutine function
330<coroutine>`::
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100331
332 import asyncio
333
Berker Peksagf5928672017-02-07 11:27:09 +0300334 async def slow_operation(future):
335 await asyncio.sleep(1)
Victor Stinner04e05da2014-02-17 10:54:30 +0100336 future.set_result('Future is done!')
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100337
338 loop = asyncio.get_event_loop()
339 future = asyncio.Future()
Yury Selivanovd7e19bb2015-05-11 16:33:41 -0400340 asyncio.ensure_future(slow_operation(future))
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100341 loop.run_until_complete(future)
342 print(future.result())
Victor Stinnerf40c6632014-01-28 23:32:40 +0100343 loop.close()
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100344
Terry Jan Reedyc935a952014-07-24 02:33:14 -0400345The coroutine function is responsible for the computation (which takes 1 second)
Victor Stinner59759ff2014-01-16 19:30:21 +0100346and it stores the result into the future. The
Guido van Rossumf68afd82016-08-08 09:41:21 -0700347:meth:`~AbstractEventLoop.run_until_complete` method waits for the completion of
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100348the future.
349
350.. note::
Guido van Rossumf68afd82016-08-08 09:41:21 -0700351 The :meth:`~AbstractEventLoop.run_until_complete` method uses internally the
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100352 :meth:`~Future.add_done_callback` method to be notified when the future is
353 done.
354
355
356Example: Future with run_forever()
357^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
358
359The previous example can be written differently using the
360:meth:`Future.add_done_callback` method to describe explicitly the control
361flow::
362
363 import asyncio
364
Berker Peksagf5928672017-02-07 11:27:09 +0300365 async def slow_operation(future):
366 await asyncio.sleep(1)
Victor Stinner04e05da2014-02-17 10:54:30 +0100367 future.set_result('Future is done!')
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100368
369 def got_result(future):
370 print(future.result())
371 loop.stop()
372
373 loop = asyncio.get_event_loop()
374 future = asyncio.Future()
Yury Selivanovd7e19bb2015-05-11 16:33:41 -0400375 asyncio.ensure_future(slow_operation(future))
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100376 future.add_done_callback(got_result)
Victor Stinner04e05da2014-02-17 10:54:30 +0100377 try:
378 loop.run_forever()
379 finally:
380 loop.close()
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100381
Victor Stinner039f7032014-12-02 17:52:45 +0100382In this example, the future is used to link ``slow_operation()`` to
383``got_result()``: when ``slow_operation()`` is done, ``got_result()`` is called
384with the result.
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100385
386
Victor Stinnerea3183f2013-12-03 01:08:00 +0100387Task
388----
389
Andrew Svetlovf74ef452017-12-15 07:04:38 +0200390.. function:: create_task(coro)
391
392 Wrap a :ref:`coroutine <coroutine>` *coro* into a task and schedule
393 its execution. Return the task object.
394
395 The task is executed in :func:`get_running_loop` context,
396 :exc:`RuntimeError` is raised if there is no running loop in
397 current thread.
398
399 .. versionadded:: 3.7
400
Victor Stinnerea3183f2013-12-03 01:08:00 +0100401.. class:: Task(coro, \*, loop=None)
402
Andrew Svetlovf74ef452017-12-15 07:04:38 +0200403 A unit for concurrent running of :ref:`coroutines <coroutine>`,
404 subclass of :class:`Future`.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200405
R David Murray22dd8332014-09-24 11:09:09 -0400406 A task is responsible for executing a coroutine object in an event loop. If
Victor Stinner530ef2f2014-07-08 12:39:10 +0200407 the wrapped coroutine yields from a future, the task suspends the execution
Berker Peksag002b0a72016-10-04 20:45:47 +0300408 of the wrapped coroutine and waits for the completion of the future. When
Victor Stinner530ef2f2014-07-08 12:39:10 +0200409 the future is done, the execution of the wrapped coroutine restarts with the
410 result or the exception of the future.
411
412 Event loops use cooperative scheduling: an event loop only runs one task at
R David Murray22dd8332014-09-24 11:09:09 -0400413 a time. Other tasks may run in parallel if other event loops are
Victor Stinner530ef2f2014-07-08 12:39:10 +0200414 running in different threads. While a task waits for the completion of a
415 future, the event loop executes a new task.
416
luzpaza5293b42017-11-05 07:37:50 -0600417 The cancellation of a task is different from the cancellation of a
Andrew Svetlovf1240162016-01-11 14:40:35 +0200418 future. Calling :meth:`cancel` will throw a
419 :exc:`~concurrent.futures.CancelledError` to the wrapped
420 coroutine. :meth:`~Future.cancelled` only returns ``True`` if the
Victor Stinner530ef2f2014-07-08 12:39:10 +0200421 wrapped coroutine did not catch the
422 :exc:`~concurrent.futures.CancelledError` exception, or raised a
423 :exc:`~concurrent.futures.CancelledError` exception.
424
425 If a pending task is destroyed, the execution of its wrapped :ref:`coroutine
426 <coroutine>` did not complete. It is probably a bug and a warning is
427 logged: see :ref:`Pending task destroyed <asyncio-pending-task-destroyed>`.
428
Andrew Svetlovf74ef452017-12-15 07:04:38 +0200429 Don't directly create :class:`Task` instances: use the :func:`create_task`
Guido van Rossumf68afd82016-08-08 09:41:21 -0700430 function or the :meth:`AbstractEventLoop.create_task` method.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100431
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700432 Tasks support the :mod:`contextvars` module. When a Task
433 is created it copies the current context and later runs its coroutine
434 in the copied context. See :pep:`567` for more details.
435
Victor Stinner83704962015-02-25 14:24:15 +0100436 This class is :ref:`not thread safe <asyncio-multithreading>`.
437
Miss Islington (bot)2fc443c2018-05-23 10:59:17 -0700438 .. versionchanged:: 3.7
439 Added support for the :mod:`contextvars` module.
440
Victor Stinnerea3183f2013-12-03 01:08:00 +0100441 .. classmethod:: all_tasks(loop=None)
442
443 Return a set of all tasks for an event loop.
444
445 By default all tasks for the current event loop are returned.
446
Victor Stinner742520b2013-12-10 12:14:50 +0100447 .. classmethod:: current_task(loop=None)
448
449 Return the currently running task in an event loop or ``None``.
450
451 By default the current task for the current event loop is returned.
452
453 ``None`` is returned when called not in the context of a :class:`Task`.
454
Victor Stinner8d213572014-06-02 23:06:46 +0200455 .. method:: cancel()
456
R David Murray22dd8332014-09-24 11:09:09 -0400457 Request that this task cancel itself.
Victor Stinner8d213572014-06-02 23:06:46 +0200458
459 This arranges for a :exc:`~concurrent.futures.CancelledError` to be
460 thrown into the wrapped coroutine on the next cycle through the event
461 loop. The coroutine then has a chance to clean up or even deny the
462 request using try/except/finally.
463
R David Murray22dd8332014-09-24 11:09:09 -0400464 Unlike :meth:`Future.cancel`, this does not guarantee that the task
Victor Stinner8d213572014-06-02 23:06:46 +0200465 will be cancelled: the exception might be caught and acted upon, delaying
R David Murray22dd8332014-09-24 11:09:09 -0400466 cancellation of the task or preventing cancellation completely. The task
467 may also return a value or raise a different exception.
Victor Stinner8d213572014-06-02 23:06:46 +0200468
469 Immediately after this method is called, :meth:`~Future.cancelled` will
470 not return ``True`` (unless the task was already cancelled). A task will
471 be marked as cancelled when the wrapped coroutine terminates with a
472 :exc:`~concurrent.futures.CancelledError` exception (even if
473 :meth:`cancel` was not called).
474
475 .. method:: get_stack(\*, limit=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100476
477 Return the list of stack frames for this task's coroutine.
478
Andrew Svetlovf1240162016-01-11 14:40:35 +0200479 If the coroutine is not done, this returns the stack where it is
480 suspended. If the coroutine has completed successfully or was
481 cancelled, this returns an empty list. If the coroutine was
482 terminated by an exception, this returns the list of traceback
483 frames.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100484
485 The frames are always ordered from oldest to newest.
486
Brian Curtina1afeec2014-02-08 18:36:14 -0600487 The optional limit gives the maximum number of frames to return; by
Victor Stinnerea3183f2013-12-03 01:08:00 +0100488 default all available frames are returned. Its meaning differs depending
489 on whether a stack or a traceback is returned: the newest frames of a
490 stack are returned, but the oldest frames of a traceback are returned.
491 (This matches the behavior of the traceback module.)
492
493 For reasons beyond our control, only one stack frame is returned for a
494 suspended coroutine.
495
496 .. method:: print_stack(\*, limit=None, file=None)
497
498 Print the stack or traceback for this task's coroutine.
499
500 This produces output similar to that of the traceback module, for the
501 frames retrieved by get_stack(). The limit argument is passed to
502 get_stack(). The file argument is an I/O stream to which the output
R David Murray22dd8332014-09-24 11:09:09 -0400503 is written; by default output is written to sys.stderr.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100504
505
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100506Example: Parallel execution of tasks
507^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
508
509Example executing 3 tasks (A, B, C) in parallel::
510
511 import asyncio
512
Berker Peksagd5adb632017-02-01 22:37:16 +0300513 async def factorial(name, number):
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100514 f = 1
Victor Stinner34f29462013-12-10 02:51:05 +0100515 for i in range(2, number+1):
516 print("Task %s: Compute factorial(%s)..." % (name, i))
Berker Peksagd5adb632017-02-01 22:37:16 +0300517 await asyncio.sleep(1)
Victor Stinner34f29462013-12-10 02:51:05 +0100518 f *= i
519 print("Task %s: factorial(%s) = %s" % (name, number, f))
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100520
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100521 loop = asyncio.get_event_loop()
Berker Peksagd5adb632017-02-01 22:37:16 +0300522 loop.run_until_complete(asyncio.gather(
523 factorial("A", 2),
524 factorial("B", 3),
525 factorial("C", 4),
526 ))
Victor Stinnerf40c6632014-01-28 23:32:40 +0100527 loop.close()
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100528
529Output::
530
Victor Stinner34f29462013-12-10 02:51:05 +0100531 Task A: Compute factorial(2)...
532 Task B: Compute factorial(2)...
533 Task C: Compute factorial(2)...
534 Task A: factorial(2) = 2
535 Task B: Compute factorial(3)...
536 Task C: Compute factorial(3)...
537 Task B: factorial(3) = 6
538 Task C: Compute factorial(4)...
539 Task C: factorial(4) = 24
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100540
Victor Stinner34f29462013-12-10 02:51:05 +0100541A task is automatically scheduled for execution when it is created. The event
Victor Stinnerb69d62d2013-12-10 02:09:46 +0100542loop stops when all tasks are done.
543
544
Victor Stinnerea3183f2013-12-03 01:08:00 +0100545Task functions
546--------------
547
Eli Bendersky029981b2014-01-20 07:02:22 -0800548.. note::
549
Martin Panterc04fb562016-02-10 05:44:01 +0000550 In the functions below, the optional *loop* argument allows explicitly setting
Eli Bendersky029981b2014-01-20 07:02:22 -0800551 the event loop object used by the underlying task or coroutine. If it's
552 not provided, the default event loop is used.
553
Andrew Svetlov44d1a592017-12-16 21:58:38 +0200554
Miss Islington (bot)ee28cd52018-03-12 12:12:05 -0700555.. function:: current_task(loop=None)
Andrew Svetlov44d1a592017-12-16 21:58:38 +0200556
557 Return the current running :class:`Task` instance or ``None``, if
558 no task is running.
559
560 If *loop* is ``None`` :func:`get_running_loop` is used to get
561 the current loop.
562
563 .. versionadded:: 3.7
564
565
Miss Islington (bot)ee28cd52018-03-12 12:12:05 -0700566.. function:: all_tasks(loop=None)
Andrew Svetlov44d1a592017-12-16 21:58:38 +0200567
568 Return a set of :class:`Task` objects created for the loop.
569
570 If *loop* is ``None`` :func:`get_event_loop` is used for getting
571 current loop.
572
573 .. versionadded:: 3.7
574
575
Victor Stinner99c2ab42013-12-03 19:17:25 +0100576.. function:: as_completed(fs, \*, loop=None, timeout=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100577
Victor Stinner99c2ab42013-12-03 19:17:25 +0100578 Return an iterator whose values, when waited for, are :class:`Future`
579 instances.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100580
Victor Stinner28d0ae482014-05-29 00:04:57 +0200581 Raises :exc:`asyncio.TimeoutError` if the timeout occurs before all Futures
582 are done.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100583
584 Example::
585
586 for f in as_completed(fs):
Andrew Svetlov88743422017-12-11 17:35:49 +0200587 result = await f # The 'await' may raise
Victor Stinnerea3183f2013-12-03 01:08:00 +0100588 # Use result
589
590 .. note::
591
592 The futures ``f`` are not necessarily members of fs.
593
Yury Selivanovd7e19bb2015-05-11 16:33:41 -0400594.. function:: ensure_future(coro_or_future, \*, loop=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100595
Victor Stinner980dd842014-10-12 21:36:17 +0200596 Schedule the execution of a :ref:`coroutine object <coroutine>`: wrap it in
597 a future. Return a :class:`Task` object.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100598
Victor Stinner99c2ab42013-12-03 19:17:25 +0100599 If the argument is a :class:`Future`, it is returned directly.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100600
Yury Selivanovd7e19bb2015-05-11 16:33:41 -0400601 .. versionadded:: 3.4.4
602
Yury Selivanove319ab02015-12-15 00:45:24 -0500603 .. versionchanged:: 3.5.1
604 The function accepts any :term:`awaitable` object.
605
Andrew Svetlovf74ef452017-12-15 07:04:38 +0200606 .. note::
607
608 :func:`create_task` (added in Python 3.7) is the preferable way
609 for spawning new tasks.
610
Victor Stinner337e03f2014-08-11 01:11:13 +0200611 .. seealso::
612
Andrew Svetlovf74ef452017-12-15 07:04:38 +0200613 The :func:`create_task` function and
614 :meth:`AbstractEventLoop.create_task` method.
Victor Stinner337e03f2014-08-11 01:11:13 +0200615
adisbladis824f6872017-06-09 14:28:59 +0800616.. function:: wrap_future(future, \*, loop=None)
617
618 Wrap a :class:`concurrent.futures.Future` object in a :class:`Future`
619 object.
620
Victor Stinner99c2ab42013-12-03 19:17:25 +0100621.. function:: gather(\*coros_or_futures, loop=None, return_exceptions=False)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100622
Victor Stinner59759ff2014-01-16 19:30:21 +0100623 Return a future aggregating results from the given coroutine objects or
624 futures.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100625
626 All futures must share the same event loop. If all the tasks are done
627 successfully, the returned future's result is the list of results (in the
628 order of the original sequence, not necessarily the order of results
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300629 arrival). If *return_exceptions* is true, exceptions in the tasks are
Victor Stinnerea3183f2013-12-03 01:08:00 +0100630 treated the same as successful results, and gathered in the result list;
631 otherwise, the first raised exception will be immediately propagated to the
632 returned future.
633
634 Cancellation: if the outer Future is cancelled, all children (that have not
635 completed yet) are also cancelled. If any child is cancelled, this is
636 treated as if it raised :exc:`~concurrent.futures.CancelledError` -- the
637 outer Future is *not* cancelled in this case. (This is to prevent the
638 cancellation of one child to cause other children to be cancelled.)
639
Victor Stinner1ad5afc2014-01-30 00:18:50 +0100640.. function:: iscoroutine(obj)
641
Yury Selivanov66f88282015-06-24 11:04:15 -0400642 Return ``True`` if *obj* is a :ref:`coroutine object <coroutine>`,
643 which may be based on a generator or an :keyword:`async def` coroutine.
Victor Stinner1ad5afc2014-01-30 00:18:50 +0100644
Yury Selivanov66f88282015-06-24 11:04:15 -0400645.. function:: iscoroutinefunction(func)
Victor Stinner1ad5afc2014-01-30 00:18:50 +0100646
Yury Selivanov66f88282015-06-24 11:04:15 -0400647 Return ``True`` if *func* is determined to be a :ref:`coroutine function
648 <coroutine>`, which may be a decorated generator function or an
649 :keyword:`async def` function.
Victor Stinner1ad5afc2014-01-30 00:18:50 +0100650
Andrew Svetlovf1240162016-01-11 14:40:35 +0200651.. function:: run_coroutine_threadsafe(coro, loop)
652
653 Submit a :ref:`coroutine object <coroutine>` to a given event loop.
654
655 Return a :class:`concurrent.futures.Future` to access the result.
656
657 This function is meant to be called from a different thread than the one
658 where the event loop is running. Usage::
659
660 # Create a coroutine
661 coro = asyncio.sleep(1, result=3)
662 # Submit the coroutine to a given loop
663 future = asyncio.run_coroutine_threadsafe(coro, loop)
664 # Wait for the result with an optional timeout argument
665 assert future.result(timeout) == 3
666
667 If an exception is raised in the coroutine, the returned future will be
668 notified. It can also be used to cancel the task in the event loop::
669
670 try:
671 result = future.result(timeout)
672 except asyncio.TimeoutError:
673 print('The coroutine took too long, cancelling the task...')
674 future.cancel()
675 except Exception as exc:
676 print('The coroutine raised an exception: {!r}'.format(exc))
677 else:
678 print('The coroutine returned: {!r}'.format(result))
679
680 See the :ref:`concurrency and multithreading <asyncio-multithreading>`
681 section of the documentation.
682
683 .. note::
684
Andrew Svetlov3af81f22016-01-11 14:45:25 +0200685 Unlike other functions from the module,
686 :func:`run_coroutine_threadsafe` requires the *loop* argument to
Zachary Waref9aff922016-06-01 00:01:10 -0500687 be passed explicitly.
Andrew Svetlovf1240162016-01-11 14:40:35 +0200688
Andrew Svetlovea471342016-01-11 15:41:43 +0200689 .. versionadded:: 3.5.1
Andrew Svetlovf1240162016-01-11 14:40:35 +0200690
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100691.. coroutinefunction:: sleep(delay, result=None, \*, loop=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100692
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500693 Create a :ref:`coroutine <coroutine>` that completes after a given
Eli Bendersky2d26af82014-01-20 06:59:23 -0800694 time (in seconds). If *result* is provided, it is produced to the caller
695 when the coroutine completes.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100696
Victor Stinner45b27ed2014-02-01 02:36:43 +0100697 The resolution of the sleep depends on the :ref:`granularity of the event
698 loop <asyncio-delayed-calls>`.
699
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100700 This function is a :ref:`coroutine <coroutine>`.
701
Miss Islington (bot)0f7cf7e2018-03-03 13:22:49 -0800702.. coroutinefunction:: shield(arg, \*, loop=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100703
704 Wait for a future, shielding it from cancellation.
705
706 The statement::
707
Andrew Svetlov88743422017-12-11 17:35:49 +0200708 res = await shield(something())
Victor Stinnerea3183f2013-12-03 01:08:00 +0100709
710 is exactly equivalent to the statement::
711
Andrew Svetlov88743422017-12-11 17:35:49 +0200712 res = await something()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100713
714 *except* that if the coroutine containing it is cancelled, the task running
715 in ``something()`` is not cancelled. From the point of view of
716 ``something()``, the cancellation did not happen. But its caller is still
717 cancelled, so the yield-from expression still raises
718 :exc:`~concurrent.futures.CancelledError`. Note: If ``something()`` is
719 cancelled by other means this will still cancel ``shield()``.
720
721 If you want to completely ignore cancellation (not recommended) you can
722 combine ``shield()`` with a try/except clause, as follows::
723
724 try:
Andrew Svetlov88743422017-12-11 17:35:49 +0200725 res = await shield(something())
Victor Stinnerea3183f2013-12-03 01:08:00 +0100726 except CancelledError:
727 res = None
728
Yury Selivanov950204d2016-05-16 16:23:00 -0400729
Andrew Svetlovf1240162016-01-11 14:40:35 +0200730.. coroutinefunction:: wait(futures, \*, loop=None, timeout=None,\
731 return_when=ALL_COMPLETED)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100732
Victor Stinner59759ff2014-01-16 19:30:21 +0100733 Wait for the Futures and coroutine objects given by the sequence *futures*
734 to complete. Coroutines will be wrapped in Tasks. Returns two sets of
Victor Stinner99c2ab42013-12-03 19:17:25 +0100735 :class:`Future`: (done, pending).
Victor Stinnerea3183f2013-12-03 01:08:00 +0100736
Victor Stinnerdb74d982014-06-10 11:16:05 +0200737 The sequence *futures* must not be empty.
738
Victor Stinnerea3183f2013-12-03 01:08:00 +0100739 *timeout* can be used to control the maximum number of seconds to wait before
740 returning. *timeout* can be an int or float. If *timeout* is not specified
741 or ``None``, there is no limit to the wait time.
742
743 *return_when* indicates when this function should return. It must be one of
Victor Stinner933a8c82013-12-03 01:59:38 +0100744 the following constants of the :mod:`concurrent.futures` module:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100745
746 .. tabularcolumns:: |l|L|
747
748 +-----------------------------+----------------------------------------+
749 | Constant | Description |
750 +=============================+========================================+
751 | :const:`FIRST_COMPLETED` | The function will return when any |
752 | | future finishes or is cancelled. |
753 +-----------------------------+----------------------------------------+
754 | :const:`FIRST_EXCEPTION` | The function will return when any |
755 | | future finishes by raising an |
756 | | exception. If no future raises an |
757 | | exception then it is equivalent to |
758 | | :const:`ALL_COMPLETED`. |
759 +-----------------------------+----------------------------------------+
760 | :const:`ALL_COMPLETED` | The function will return when all |
761 | | futures finish or are cancelled. |
762 +-----------------------------+----------------------------------------+
763
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500764 This function is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100765
766 Usage::
767
Andrew Svetlov88743422017-12-11 17:35:49 +0200768 done, pending = await asyncio.wait(fs)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100769
770 .. note::
771
Victor Stinner28d0ae482014-05-29 00:04:57 +0200772 This does not raise :exc:`asyncio.TimeoutError`! Futures that aren't done
773 when the timeout occurs are returned in the second set.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100774
Victor Stinner3e09e322013-12-03 01:22:06 +0100775
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100776.. coroutinefunction:: wait_for(fut, timeout, \*, loop=None)
Victor Stinner1ad5afc2014-01-30 00:18:50 +0100777
778 Wait for the single :class:`Future` or :ref:`coroutine object <coroutine>`
Victor Stinner530ef2f2014-07-08 12:39:10 +0200779 to complete with timeout. If *timeout* is ``None``, block until the future
Victor Stinner1ad5afc2014-01-30 00:18:50 +0100780 completes.
781
Victor Stinner337e03f2014-08-11 01:11:13 +0200782 Coroutine will be wrapped in :class:`Task`.
Victor Stinner1ad5afc2014-01-30 00:18:50 +0100783
784 Returns result of the Future or coroutine. When a timeout occurs, it
Victor Stinner28d0ae482014-05-29 00:04:57 +0200785 cancels the task and raises :exc:`asyncio.TimeoutError`. To avoid the task
Victor Stinner1ad5afc2014-01-30 00:18:50 +0100786 cancellation, wrap it in :func:`shield`.
787
Victor Stinner72dcb0a2015-04-03 17:08:19 +0200788 If the wait is cancelled, the future *fut* is also cancelled.
789
Victor Stinner530ef2f2014-07-08 12:39:10 +0200790 This function is a :ref:`coroutine <coroutine>`, usage::
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500791
Andrew Svetlov88743422017-12-11 17:35:49 +0200792 result = await asyncio.wait_for(fut, 60.0)
Victor Stinner1ad5afc2014-01-30 00:18:50 +0100793
Victor Stinner72dcb0a2015-04-03 17:08:19 +0200794 .. versionchanged:: 3.4.3
795 If the wait is cancelled, the future *fut* is now also cancelled.