1.. currentmodule:: asyncio
2
3
4====================
5Coroutines and Tasks
6====================
7
8This section outlines high-level asyncio APIs to work with coroutines
9and Tasks.
10
11.. contents::
12   :depth: 1
13   :local:
14
15
16.. _coroutine:
17
18Coroutines
19==========
20
21**Source code:** :source:`Lib/asyncio/coroutines.py`
22
23----------------------------------------------------
24
25:term:`Coroutines <coroutine>` declared with the async/await syntax is the
26preferred way of writing asyncio applications.  For example, the following
27snippet of code prints "hello", waits 1 second,
28and then prints "world"::
29
30    >>> import asyncio
31
32    >>> async def main():
33    ...     print('hello')
34    ...     await asyncio.sleep(1)
35    ...     print('world')
36
37    >>> asyncio.run(main())
38    hello
39    world
40
41Note that simply calling a coroutine will not schedule it to
42be executed::
43
44    >>> main()
45    <coroutine object main at 0x1053bb7c8>
46
47To actually run a coroutine, asyncio provides the following mechanisms:
48
49* The :func:`asyncio.run` function to run the top-level
50  entry point "main()" function (see the above example.)
51
52* Awaiting on a coroutine.  The following snippet of code will
53  print "hello" after waiting for 1 second, and then print "world"
54  after waiting for *another* 2 seconds::
55
56      import asyncio
57      import time
58
59      async def say_after(delay, what):
60          await asyncio.sleep(delay)
61          print(what)
62
63      async def main():
64          print(f"started at {time.strftime('%X')}")
65
66          await say_after(1, 'hello')
67          await say_after(2, 'world')
68
69          print(f"finished at {time.strftime('%X')}")
70
71      asyncio.run(main())
72
73  Expected output::
74
75      started at 17:13:52
76      hello
77      world
78      finished at 17:13:55
79
80* The :func:`asyncio.create_task` function to run coroutines
81  concurrently as asyncio :class:`Tasks <Task>`.
82
83  Let's modify the above example and run two ``say_after`` coroutines
84  *concurrently*::
85
86      async def main():
87          task1 = asyncio.create_task(
88              say_after(1, 'hello'))
89
90          task2 = asyncio.create_task(
91              say_after(2, 'world'))
92
93          print(f"started at {time.strftime('%X')}")
94
95          # Wait until both tasks are completed (should take
96          # around 2 seconds.)
97          await task1
98          await task2
99
100          print(f"finished at {time.strftime('%X')}")
101
102  Note that expected output now shows that the snippet runs
103  1 second faster than before::
104
105      started at 17:14:32
106      hello
107      world
108      finished at 17:14:34
109
110* The :class:`asyncio.TaskGroup` class provides a more modern
111  alternative to :func:`create_task`.
112  Using this API, the last example becomes::
113
114      async def main():
115          async with asyncio.TaskGroup() as tg:
116              task1 = tg.create_task(
117                  say_after(1, 'hello'))
118
119              task2 = tg.create_task(
120                  say_after(2, 'world'))
121
122              print(f"started at {time.strftime('%X')}")
123
124          # The await is implicit when the context manager exits.
125
126          print(f"finished at {time.strftime('%X')}")
127
128  The timing and output should be the same as for the previous version.
129
130  .. versionadded:: 3.11
131     :class:`asyncio.TaskGroup`.
132
133
134.. _asyncio-awaitables:
135
136Awaitables
137==========
138
139We say that an object is an **awaitable** object if it can be used
140in an :keyword:`await` expression.  Many asyncio APIs are designed to
141accept awaitables.
142
143There are three main types of *awaitable* objects:
144**coroutines**, **Tasks**, and **Futures**.
145
146
147.. rubric:: Coroutines
148
149Python coroutines are *awaitables* and therefore can be awaited from
150other coroutines::
151
152    import asyncio
153
154    async def nested():
155        return 42
156
157    async def main():
158        # Nothing happens if we just call "nested()".
159        # A coroutine object is created but not awaited,
160        # so it *won't run at all*.
161        nested()
162
163        # Let's do it differently now and await it:
164        print(await nested())  # will print "42".
165
166    asyncio.run(main())
167
168.. important::
169
170   In this documentation the term "coroutine" can be used for
171   two closely related concepts:
172
173   * a *coroutine function*: an :keyword:`async def` function;
174
175   * a *coroutine object*: an object returned by calling a
176     *coroutine function*.
177
178
179.. rubric:: Tasks
180
181*Tasks* are used to schedule coroutines *concurrently*.
182
183When a coroutine is wrapped into a *Task* with functions like
184:func:`asyncio.create_task` the coroutine is automatically
185scheduled to run soon::
186
187    import asyncio
188
189    async def nested():
190        return 42
191
192    async def main():
193        # Schedule nested() to run soon concurrently
194        # with "main()".
195        task = asyncio.create_task(nested())
196
197        # "task" can now be used to cancel "nested()", or
198        # can simply be awaited to wait until it is complete:
199        await task
200
201    asyncio.run(main())
202
203
204.. rubric:: Futures
205
206A :class:`Future` is a special **low-level** awaitable object that
207represents an **eventual result** of an asynchronous operation.
208
209When a Future object is *awaited* it means that the coroutine will
210wait until the Future is resolved in some other place.
211
212Future objects in asyncio are needed to allow callback-based code
213to be used with async/await.
214
215Normally **there is no need** to create Future objects at the
216application level code.
217
218Future objects, sometimes exposed by libraries and some asyncio
219APIs, can be awaited::
220
221    async def main():
222        await function_that_returns_a_future_object()
223
224        # this is also valid:
225        await asyncio.gather(
226            function_that_returns_a_future_object(),
227            some_python_coroutine()
228        )
229
230A good example of a low-level function that returns a Future object
231is :meth:`loop.run_in_executor`.
232
233
234Creating Tasks
235==============
236
237**Source code:** :source:`Lib/asyncio/tasks.py`
238
239-----------------------------------------------
240
241.. function:: create_task(coro, *, name=None, context=None)
242
243   Wrap the *coro* :ref:`coroutine <coroutine>` into a :class:`Task`
244   and schedule its execution.  Return the Task object.
245
246   If *name* is not ``None``, it is set as the name of the task using
247   :meth:`Task.set_name`.
248
249   An optional keyword-only *context* argument allows specifying a
250   custom :class:`contextvars.Context` for the *coro* to run in.
251   The current context copy is created when no *context* is provided.
252
253   The task is executed in the loop returned by :func:`get_running_loop`,
254   :exc:`RuntimeError` is raised if there is no running loop in
255   current thread.
256
257   .. note::
258
259      :meth:`asyncio.TaskGroup.create_task` is a newer alternative
260      that allows for convenient waiting for a group of related tasks.
261
262   .. important::
263
264      Save a reference to the result of this function, to avoid
265      a task disappearing mid-execution. The event loop only keeps
266      weak references to tasks. A task that isn't referenced elsewhere
267      may get garbage collected at any time, even before it's done.
268      For reliable "fire-and-forget" background tasks, gather them in
269      a collection::
270
271          background_tasks = set()
272
273          for i in range(10):
274              task = asyncio.create_task(some_coro(param=i))
275
276              # Add task to the set. This creates a strong reference.
277              background_tasks.add(task)
278
279              # To prevent keeping references to finished tasks forever,
280              # make each task remove its own reference from the set after
281              # completion:
282              task.add_done_callback(background_tasks.discard)
283
284   .. versionadded:: 3.7
285
286   .. versionchanged:: 3.8
287      Added the *name* parameter.
288
289   .. versionchanged:: 3.11
290      Added the *context* parameter.
291
292
293Task Cancellation
294=================
295
296Tasks can easily and safely be cancelled.
297When a task is cancelled, :exc:`asyncio.CancelledError` will be raised
298in the task at the next opportunity.
299
300It is recommended that coroutines use ``try/finally`` blocks to robustly
301perform clean-up logic. In case :exc:`asyncio.CancelledError`
302is explicitly caught, it should generally be propagated when
303clean-up is complete. :exc:`asyncio.CancelledError` directly subclasses
304:exc:`BaseException` so most code will not need to be aware of it.
305
306The asyncio components that enable structured concurrency, like
307:class:`asyncio.TaskGroup` and :func:`asyncio.timeout`,
308are implemented using cancellation internally and might misbehave if
309a coroutine swallows :exc:`asyncio.CancelledError`. Similarly, user code
310should not generally call :meth:`uncancel <asyncio.Task.uncancel>`.
311However, in cases when suppressing :exc:`asyncio.CancelledError` is
312truly desired, it is necessary to also call ``uncancel()`` to completely
313remove the cancellation state.
314
315.. _taskgroups:
316
317Task Groups
318===========
319
320Task groups combine a task creation API with a convenient
321and reliable way to wait for all tasks in the group to finish.
322
323.. class:: TaskGroup()
324
325   An :ref:`asynchronous context manager <async-context-managers>`
326   holding a group of tasks.
327   Tasks can be added to the group using :meth:`create_task`.
328   All tasks are awaited when the context manager exits.
329
330   .. versionadded:: 3.11
331
332   .. method:: create_task(coro, *, name=None, context=None)
333
334      Create a task in this task group.
335      The signature matches that of :func:`asyncio.create_task`.
336
337Example::
338
339    async def main():
340        async with asyncio.TaskGroup() as tg:
341            task1 = tg.create_task(some_coro(...))
342            task2 = tg.create_task(another_coro(...))
343        print("Both tasks have completed now.")
344
345The ``async with`` statement will wait for all tasks in the group to finish.
346While waiting, new tasks may still be added to the group
347(for example, by passing ``tg`` into one of the coroutines
348and calling ``tg.create_task()`` in that coroutine).
349Once the last task has finished and the ``async with`` block is exited,
350no new tasks may be added to the group.
351
352The first time any of the tasks belonging to the group fails
353with an exception other than :exc:`asyncio.CancelledError`,
354the remaining tasks in the group are cancelled.
355No further tasks can then be added to the group.
356At this point, if the body of the ``async with`` statement is still active
357(i.e., :meth:`~object.__aexit__` hasn't been called yet),
358the task directly containing the ``async with`` statement is also cancelled.
359The resulting :exc:`asyncio.CancelledError` will interrupt an ``await``,
360but it will not bubble out of the containing ``async with`` statement.
361
362Once all tasks have finished, if any tasks have failed
363with an exception other than :exc:`asyncio.CancelledError`,
364those exceptions are combined in an
365:exc:`ExceptionGroup` or :exc:`BaseExceptionGroup`
366(as appropriate; see their documentation)
367which is then raised.
368
369Two base exceptions are treated specially:
370If any task fails with :exc:`KeyboardInterrupt` or :exc:`SystemExit`,
371the task group still cancels the remaining tasks and waits for them,
372but then the initial :exc:`KeyboardInterrupt` or :exc:`SystemExit`
373is re-raised instead of :exc:`ExceptionGroup` or :exc:`BaseExceptionGroup`.
374
375If the body of the ``async with`` statement exits with an exception
376(so :meth:`~object.__aexit__` is called with an exception set),
377this is treated the same as if one of the tasks failed:
378the remaining tasks are cancelled and then waited for,
379and non-cancellation exceptions are grouped into an
380exception group and raised.
381The exception passed into :meth:`~object.__aexit__`,
382unless it is :exc:`asyncio.CancelledError`,
383is also included in the exception group.
384The same special case is made for
385:exc:`KeyboardInterrupt` and :exc:`SystemExit` as in the previous paragraph.
386
387
388Sleeping
389========
390
391.. coroutinefunction:: sleep(delay, result=None)
392
393   Block for *delay* seconds.
394
395   If *result* is provided, it is returned to the caller
396   when the coroutine completes.
397
398   ``sleep()`` always suspends the current task, allowing other tasks
399   to run.
400
401   Setting the delay to 0 provides an optimized path to allow other
402   tasks to run. This can be used by long-running functions to avoid
403   blocking the event loop for the full duration of the function call.
404
405   .. _asyncio_example_sleep:
406
407   Example of coroutine displaying the current date every second
408   for 5 seconds::
409
410    import asyncio
411    import datetime
412
413    async def display_date():
414        loop = asyncio.get_running_loop()
415        end_time = loop.time() + 5.0
416        while True:
417            print(datetime.datetime.now())
418            if (loop.time() + 1.0) >= end_time:
419                break
420            await asyncio.sleep(1)
421
422    asyncio.run(display_date())
423
424
425   .. versionchanged:: 3.10
426      Removed the *loop* parameter.
427
428
429Running Tasks Concurrently
430==========================
431
432.. awaitablefunction:: gather(*aws, return_exceptions=False)
433
434   Run :ref:`awaitable objects <asyncio-awaitables>` in the *aws*
435   sequence *concurrently*.
436
437   If any awaitable in *aws* is a coroutine, it is automatically
438   scheduled as a Task.
439
440   If all awaitables are completed successfully, the result is an
441   aggregate list of returned values.  The order of result values
442   corresponds to the order of awaitables in *aws*.
443
444   If *return_exceptions* is ``False`` (default), the first
445   raised exception is immediately propagated to the task that
446   awaits on ``gather()``.  Other awaitables in the *aws* sequence
447   **won't be cancelled** and will continue to run.
448
449   If *return_exceptions* is ``True``, exceptions are treated the
450   same as successful results, and aggregated in the result list.
451
452   If ``gather()`` is *cancelled*, all submitted awaitables
453   (that have not completed yet) are also *cancelled*.
454
455   If any Task or Future from the *aws* sequence is *cancelled*, it is
456   treated as if it raised :exc:`CancelledError` -- the ``gather()``
457   call is **not** cancelled in this case.  This is to prevent the
458   cancellation of one submitted Task/Future to cause other
459   Tasks/Futures to be cancelled.
460
461   .. note::
462      A more modern way to create and run tasks concurrently and
463      wait for their completion is :class:`asyncio.TaskGroup`.
464
465   .. _asyncio_example_gather:
466
467   Example::
468
469      import asyncio
470
471      async def factorial(name, number):
472          f = 1
473          for i in range(2, number + 1):
474              print(f"Task {name}: Compute factorial({number}), currently i={i}...")
475              await asyncio.sleep(1)
476              f *= i
477          print(f"Task {name}: factorial({number}) = {f}")
478          return f
479
480      async def main():
481          # Schedule three calls *concurrently*:
482          L = await asyncio.gather(
483              factorial("A", 2),
484              factorial("B", 3),
485              factorial("C", 4),
486          )
487          print(L)
488
489      asyncio.run(main())
490
491      # Expected output:
492      #
493      #     Task A: Compute factorial(2), currently i=2...
494      #     Task B: Compute factorial(3), currently i=2...
495      #     Task C: Compute factorial(4), currently i=2...
496      #     Task A: factorial(2) = 2
497      #     Task B: Compute factorial(3), currently i=3...
498      #     Task C: Compute factorial(4), currently i=3...
499      #     Task B: factorial(3) = 6
500      #     Task C: Compute factorial(4), currently i=4...
501      #     Task C: factorial(4) = 24
502      #     [2, 6, 24]
503
504   .. note::
505      If *return_exceptions* is False, cancelling gather() after it
506      has been marked done won't cancel any submitted awaitables.
507      For instance, gather can be marked done after propagating an
508      exception to the caller, therefore, calling ``gather.cancel()``
509      after catching an exception (raised by one of the awaitables) from
510      gather won't cancel any other awaitables.
511
512   .. versionchanged:: 3.7
513      If the *gather* itself is cancelled, the cancellation is
514      propagated regardless of *return_exceptions*.
515
516   .. versionchanged:: 3.10
517      Removed the *loop* parameter.
518
519   .. deprecated:: 3.10
520      Deprecation warning is emitted if no positional arguments are provided
521      or not all positional arguments are Future-like objects
522      and there is no running event loop.
523
524
525Shielding From Cancellation
526===========================
527
528.. awaitablefunction:: shield(aw)
529
530   Protect an :ref:`awaitable object <asyncio-awaitables>`
531   from being :meth:`cancelled <Task.cancel>`.
532
533   If *aw* is a coroutine it is automatically scheduled as a Task.
534
535   The statement::
536
537       task = asyncio.create_task(something())
538       res = await shield(task)
539
540   is equivalent to::
541
542       res = await something()
543
544   *except* that if the coroutine containing it is cancelled, the
545   Task running in ``something()`` is not cancelled.  From the point
546   of view of ``something()``, the cancellation did not happen.
547   Although its caller is still cancelled, so the "await" expression
548   still raises a :exc:`CancelledError`.
549
550   If ``something()`` is cancelled by other means (i.e. from within
551   itself) that would also cancel ``shield()``.
552
553   If it is desired to completely ignore cancellation (not recommended)
554   the ``shield()`` function should be combined with a try/except
555   clause, as follows::
556
557       task = asyncio.create_task(something())
558       try:
559           res = await shield(task)
560       except CancelledError:
561           res = None
562
563   .. important::
564
565      Save a reference to tasks passed to this function, to avoid
566      a task disappearing mid-execution. The event loop only keeps
567      weak references to tasks. A task that isn't referenced elsewhere
568      may get garbage collected at any time, even before it's done.
569
570   .. versionchanged:: 3.10
571      Removed the *loop* parameter.
572
573   .. deprecated:: 3.10
574      Deprecation warning is emitted if *aw* is not Future-like object
575      and there is no running event loop.
576
577
578Timeouts
579========
580
581.. coroutinefunction:: timeout(delay)
582
583    An :ref:`asynchronous context manager <async-context-managers>`
584    that can be used to limit the amount of time spent waiting on
585    something.
586
587    *delay* can either be ``None``, or a float/int number of
588    seconds to wait. If *delay* is ``None``, no time limit will
589    be applied; this can be useful if the delay is unknown when
590    the context manager is created.
591
592    In either case, the context manager can be rescheduled after
593    creation using :meth:`Timeout.reschedule`.
594
595    Example::
596
597        async def main():
598            async with asyncio.timeout(10):
599                await long_running_task()
600
601    If ``long_running_task`` takes more than 10 seconds to complete,
602    the context manager will cancel the current task and handle
603    the resulting :exc:`asyncio.CancelledError` internally, transforming it
604    into an :exc:`asyncio.TimeoutError` which can be caught and handled.
605
606    .. note::
607
608      The :func:`asyncio.timeout` context manager is what transforms
609      the :exc:`asyncio.CancelledError` into an :exc:`asyncio.TimeoutError`,
610      which means the :exc:`asyncio.TimeoutError` can only be caught
611      *outside* of the context manager.
612
613    Example of catching :exc:`asyncio.TimeoutError`::
614
615        async def main():
616            try:
617                async with asyncio.timeout(10):
618                    await long_running_task()
619            except TimeoutError:
620                print("The long operation timed out, but we've handled it.")
621
622            print("This statement will run regardless.")
623
624    The context manager produced by :func:`asyncio.timeout` can be
625    rescheduled to a different deadline and inspected.
626
627    .. class:: Timeout(when)
628
629       An :ref:`asynchronous context manager <async-context-managers>`
630       for cancelling overdue coroutines.
631
632       ``when`` should be an absolute time at which the context should time out,
633       as measured by the event loop's clock:
634
635       - If ``when`` is ``None``, the timeout will never trigger.
636       - If ``when < loop.time()``, the timeout will trigger on the next
637         iteration of the event loop.
638
639        .. method:: when() -> float | None
640
641           Return the current deadline, or ``None`` if the current
642           deadline is not set.
643
644        .. method:: reschedule(when: float | None)
645
646            Reschedule the timeout.
647
648        .. method:: expired() -> bool
649
650           Return whether the context manager has exceeded its deadline
651           (expired).
652
653    Example::
654
655        async def main():
656            try:
657                # We do not know the timeout when starting, so we pass ``None``.
658                async with asyncio.timeout(None) as cm:
659                    # We know the timeout now, so we reschedule it.
660                    new_deadline = get_running_loop().time() + 10
661                    cm.reschedule(new_deadline)
662
663                    await long_running_task()
664            except TimeoutError:
665                pass
666
667            if cm.expired():
668                print("Looks like we haven't finished on time.")
669
670    Timeout context managers can be safely nested.
671
672    .. versionadded:: 3.11
673
674.. coroutinefunction:: timeout_at(when)
675
676   Similar to :func:`asyncio.timeout`, except *when* is the absolute time
677   to stop waiting, or ``None``.
678
679   Example::
680
681      async def main():
682          loop = get_running_loop()
683          deadline = loop.time() + 20
684          try:
685              async with asyncio.timeout_at(deadline):
686                  await long_running_task()
687          except TimeoutError:
688              print("The long operation timed out, but we've handled it.")
689
690          print("This statement will run regardless.")
691
692   .. versionadded:: 3.11
693
694.. coroutinefunction:: wait_for(aw, timeout)
695
696   Wait for the *aw* :ref:`awaitable <asyncio-awaitables>`
697   to complete with a timeout.
698
699   If *aw* is a coroutine it is automatically scheduled as a Task.
700
701   *timeout* can either be ``None`` or a float or int number of seconds
702   to wait for.  If *timeout* is ``None``, block until the future
703   completes.
704
705   If a timeout occurs, it cancels the task and raises
706   :exc:`TimeoutError`.
707
708   To avoid the task :meth:`cancellation <Task.cancel>`,
709   wrap it in :func:`shield`.
710
711   The function will wait until the future is actually cancelled,
712   so the total wait time may exceed the *timeout*. If an exception
713   happens during cancellation, it is propagated.
714
715   If the wait is cancelled, the future *aw* is also cancelled.
716
717   .. versionchanged:: 3.10
718      Removed the *loop* parameter.
719
720   .. _asyncio_example_waitfor:
721
722   Example::
723
724       async def eternity():
725           # Sleep for one hour
726           await asyncio.sleep(3600)
727           print('yay!')
728
729       async def main():
730           # Wait for at most 1 second
731           try:
732               await asyncio.wait_for(eternity(), timeout=1.0)
733           except TimeoutError:
734               print('timeout!')
735
736       asyncio.run(main())
737
738       # Expected output:
739       #
740       #     timeout!
741
742   .. versionchanged:: 3.7
743      When *aw* is cancelled due to a timeout, ``wait_for`` waits
744      for *aw* to be cancelled.  Previously, it raised
745      :exc:`TimeoutError` immediately.
746
747   .. versionchanged:: 3.10
748      Removed the *loop* parameter.
749
750
751Waiting Primitives
752==================
753
754.. coroutinefunction:: wait(aws, *, timeout=None, return_when=ALL_COMPLETED)
755
756   Run :class:`~asyncio.Future` and :class:`~asyncio.Task` instances in the *aws*
757   iterable concurrently and block until the condition specified
758   by *return_when*.
759
760   The *aws* iterable must not be empty and generators yielding tasks are not accepted.
761
762   Returns two sets of Tasks/Futures: ``(done, pending)``.
763
764   Usage::
765
766        done, pending = await asyncio.wait(aws)
767
768   *timeout* (a float or int), if specified, can be used to control
769   the maximum number of seconds to wait before returning.
770
771   Note that this function does not raise :exc:`TimeoutError`.
772   Futures or Tasks that aren't done when the timeout occurs are simply
773   returned in the second set.
774
775   *return_when* indicates when this function should return.  It must
776   be one of the following constants:
777
778   .. tabularcolumns:: |l|L|
779
780   +-----------------------------+----------------------------------------+
781   | Constant                    | Description                            |
782   +=============================+========================================+
783   | :const:`FIRST_COMPLETED`    | The function will return when any      |
784   |                             | future finishes or is cancelled.       |
785   +-----------------------------+----------------------------------------+
786   | :const:`FIRST_EXCEPTION`    | The function will return when any      |
787   |                             | future finishes by raising an          |
788   |                             | exception.  If no future raises an     |
789   |                             | exception then it is equivalent to     |
790   |                             | :const:`ALL_COMPLETED`.                |
791   +-----------------------------+----------------------------------------+
792   | :const:`ALL_COMPLETED`      | The function will return when all      |
793   |                             | futures finish or are cancelled.       |
794   +-----------------------------+----------------------------------------+
795
796   Unlike :func:`~asyncio.wait_for`, ``wait()`` does not cancel the
797   futures when a timeout occurs.
798
799   .. versionchanged:: 3.10
800      Removed the *loop* parameter.
801
802   .. versionchanged:: 3.11
803      Passing coroutine objects to ``wait()`` directly is forbidden.
804
805.. function:: as_completed(aws, *, timeout=None)
806
807   Run :ref:`awaitable objects <asyncio-awaitables>` in the *aws*
808   iterable concurrently. Generators yielding tasks are not accepted
809   as *aws* iterable. Return an iterator of coroutines.
810   Each coroutine returned can be awaited to get the earliest next
811   result from the iterable of the remaining awaitables.
812
813   Raises :exc:`TimeoutError` if the timeout occurs before
814   all Futures are done.
815
816   Example::
817
818       for coro in as_completed(aws):
819           earliest_result = await coro
820           # ...
821
822   .. versionchanged:: 3.10
823      Removed the *loop* parameter.
824
825   .. deprecated:: 3.10
826      Deprecation warning is emitted if not all awaitable objects in the *aws*
827      iterable are Future-like objects and there is no running event loop.
828
829
830Running in Threads
831==================
832
833.. coroutinefunction:: to_thread(func, /, *args, **kwargs)
834
835   Asynchronously run function *func* in a separate thread.
836
837   Any \*args and \*\*kwargs supplied for this function are directly passed
838   to *func*. Also, the current :class:`contextvars.Context` is propagated,
839   allowing context variables from the event loop thread to be accessed in the
840   separate thread.
841
842   Return a coroutine that can be awaited to get the eventual result of *func*.
843
844   This coroutine function is primarily intended to be used for executing
845   IO-bound functions/methods that would otherwise block the event loop if
846   they were run in the main thread. For example::
847
848       def blocking_io():
849           print(f"start blocking_io at {time.strftime('%X')}")
850           # Note that time.sleep() can be replaced with any blocking
851           # IO-bound operation, such as file operations.
852           time.sleep(1)
853           print(f"blocking_io complete at {time.strftime('%X')}")
854
855       async def main():
856           print(f"started main at {time.strftime('%X')}")
857
858           await asyncio.gather(
859               asyncio.to_thread(blocking_io),
860               asyncio.sleep(1))
861
862           print(f"finished main at {time.strftime('%X')}")
863
864
865       asyncio.run(main())
866
867       # Expected output:
868       #
869       # started main at 19:50:53
870       # start blocking_io at 19:50:53
871       # blocking_io complete at 19:50:54
872       # finished main at 19:50:54
873
874   Directly calling ``blocking_io()`` in any coroutine would block the event loop
875   for its duration, resulting in an additional 1 second of run time. Instead,
876   by using ``asyncio.to_thread()``, we can run it in a separate thread without
877   blocking the event loop.
878
879   .. note::
880
881      Due to the :term:`GIL`, ``asyncio.to_thread()`` can typically only be used
882      to make IO-bound functions non-blocking. However, for extension modules
883      that release the GIL or alternative Python implementations that don't
884      have one, ``asyncio.to_thread()`` can also be used for CPU-bound functions.
885
886   .. versionadded:: 3.9
887
888
889Scheduling From Other Threads
890=============================
891
892.. function:: run_coroutine_threadsafe(coro, loop)
893
894   Submit a coroutine to the given event loop.  Thread-safe.
895
896   Return a :class:`concurrent.futures.Future` to wait for the result
897   from another OS thread.
898
899   This function is meant to be called from a different OS thread
900   than the one where the event loop is running.  Example::
901
902     # Create a coroutine
903     coro = asyncio.sleep(1, result=3)
904
905     # Submit the coroutine to a given loop
906     future = asyncio.run_coroutine_threadsafe(coro, loop)
907
908     # Wait for the result with an optional timeout argument
909     assert future.result(timeout) == 3
910
911   If an exception is raised in the coroutine, the returned Future
912   will be notified.  It can also be used to cancel the task in
913   the event loop::
914
915     try:
916         result = future.result(timeout)
917     except TimeoutError:
918         print('The coroutine took too long, cancelling the task...')
919         future.cancel()
920     except Exception as exc:
921         print(f'The coroutine raised an exception: {exc!r}')
922     else:
923         print(f'The coroutine returned: {result!r}')
924
925   See the :ref:`concurrency and multithreading <asyncio-multithreading>`
926   section of the documentation.
927
928   Unlike other asyncio functions this function requires the *loop*
929   argument to be passed explicitly.
930
931   .. versionadded:: 3.5.1
932
933
934Introspection
935=============
936
937
938.. function:: current_task(loop=None)
939
940   Return the currently running :class:`Task` instance, or ``None`` if
941   no task is running.
942
943   If *loop* is ``None`` :func:`get_running_loop` is used to get
944   the current loop.
945
946   .. versionadded:: 3.7
947
948
949.. function:: all_tasks(loop=None)
950
951   Return a set of not yet finished :class:`Task` objects run by
952   the loop.
953
954   If *loop* is ``None``, :func:`get_running_loop` is used for getting
955   current loop.
956
957   .. versionadded:: 3.7
958
959
960.. function:: iscoroutine(obj)
961
962   Return ``True`` if *obj* is a coroutine object.
963
964   .. versionadded:: 3.4
965
966
967Task Object
968===========
969
970.. class:: Task(coro, *, loop=None, name=None, context=None)
971
972   A :class:`Future-like <Future>` object that runs a Python
973   :ref:`coroutine <coroutine>`.  Not thread-safe.
974
975   Tasks are used to run coroutines in event loops.
976   If a coroutine awaits on a Future, the Task suspends
977   the execution of the coroutine and waits for the completion
978   of the Future.  When the Future is *done*, the execution of
979   the wrapped coroutine resumes.
980
981   Event loops use cooperative scheduling: an event loop runs
982   one Task at a time.  While a Task awaits for the completion of a
983   Future, the event loop runs other Tasks, callbacks, or performs
984   IO operations.
985
986   Use the high-level :func:`asyncio.create_task` function to create
987   Tasks, or the low-level :meth:`loop.create_task` or
988   :func:`ensure_future` functions.  Manual instantiation of Tasks
989   is discouraged.
990
991   To cancel a running Task use the :meth:`cancel` method.  Calling it
992   will cause the Task to throw a :exc:`CancelledError` exception into
993   the wrapped coroutine.  If a coroutine is awaiting on a Future
994   object during cancellation, the Future object will be cancelled.
995
996   :meth:`cancelled` can be used to check if the Task was cancelled.
997   The method returns ``True`` if the wrapped coroutine did not
998   suppress the :exc:`CancelledError` exception and was actually
999   cancelled.
1000
1001   :class:`asyncio.Task` inherits from :class:`Future` all of its
1002   APIs except :meth:`Future.set_result` and
1003   :meth:`Future.set_exception`.
1004
1005   An optional keyword-only *context* argument allows specifying a
1006   custom :class:`contextvars.Context` for the *coro* to run in.
1007   If no *context* is provided, the Task copies the current context
1008   and later runs its coroutine in the copied context.
1009
1010   .. versionchanged:: 3.7
1011      Added support for the :mod:`contextvars` module.
1012
1013   .. versionchanged:: 3.8
1014      Added the *name* parameter.
1015
1016   .. deprecated:: 3.10
1017      Deprecation warning is emitted if *loop* is not specified
1018      and there is no running event loop.
1019
1020   .. versionchanged:: 3.11
1021      Added the *context* parameter.
1022
1023   .. method:: done()
1024
1025      Return ``True`` if the Task is *done*.
1026
1027      A Task is *done* when the wrapped coroutine either returned
1028      a value, raised an exception, or the Task was cancelled.
1029
1030   .. method:: result()
1031
1032      Return the result of the Task.
1033
1034      If the Task is *done*, the result of the wrapped coroutine
1035      is returned (or if the coroutine raised an exception, that
1036      exception is re-raised.)
1037
1038      If the Task has been *cancelled*, this method raises
1039      a :exc:`CancelledError` exception.
1040
1041      If the Task's result isn't yet available, this method raises
1042      a :exc:`InvalidStateError` exception.
1043
1044   .. method:: exception()
1045
1046      Return the exception of the Task.
1047
1048      If the wrapped coroutine raised an exception that exception
1049      is returned.  If the wrapped coroutine returned normally
1050      this method returns ``None``.
1051
1052      If the Task has been *cancelled*, this method raises a
1053      :exc:`CancelledError` exception.
1054
1055      If the Task isn't *done* yet, this method raises an
1056      :exc:`InvalidStateError` exception.
1057
1058   .. method:: add_done_callback(callback, *, context=None)
1059
1060      Add a callback to be run when the Task is *done*.
1061
1062      This method should only be used in low-level callback-based code.
1063
1064      See the documentation of :meth:`Future.add_done_callback`
1065      for more details.
1066
1067   .. method:: remove_done_callback(callback)
1068
1069      Remove *callback* from the callbacks list.
1070
1071      This method should only be used in low-level callback-based code.
1072
1073      See the documentation of :meth:`Future.remove_done_callback`
1074      for more details.
1075
1076   .. method:: get_stack(*, limit=None)
1077
1078      Return the list of stack frames for this Task.
1079
1080      If the wrapped coroutine is not done, this returns the stack
1081      where it is suspended.  If the coroutine has completed
1082      successfully or was cancelled, this returns an empty list.
1083      If the coroutine was terminated by an exception, this returns
1084      the list of traceback frames.
1085
1086      The frames are always ordered from oldest to newest.
1087
1088      Only one stack frame is returned for a suspended coroutine.
1089
1090      The optional *limit* argument sets the maximum number of frames
1091      to return; by default all available frames are returned.
1092      The ordering of the returned list differs depending on whether
1093      a stack or a traceback is returned: the newest frames of a
1094      stack are returned, but the oldest frames of a traceback are
1095      returned.  (This matches the behavior of the traceback module.)
1096
1097   .. method:: print_stack(*, limit=None, file=None)
1098
1099      Print the stack or traceback for this Task.
1100
1101      This produces output similar to that of the traceback module
1102      for the frames retrieved by :meth:`get_stack`.
1103
1104      The *limit* argument is passed to :meth:`get_stack` directly.
1105
1106      The *file* argument is an I/O stream to which the output
1107      is written; by default output is written to :data:`sys.stdout`.
1108
1109   .. method:: get_coro()
1110
1111      Return the coroutine object wrapped by the :class:`Task`.
1112
1113      .. versionadded:: 3.8
1114
1115   .. method:: get_name()
1116
1117      Return the name of the Task.
1118
1119      If no name has been explicitly assigned to the Task, the default
1120      asyncio Task implementation generates a default name during
1121      instantiation.
1122
1123      .. versionadded:: 3.8
1124
1125   .. method:: set_name(value)
1126
1127      Set the name of the Task.
1128
1129      The *value* argument can be any object, which is then
1130      converted to a string.
1131
1132      In the default Task implementation, the name will be visible
1133      in the :func:`repr` output of a task object.
1134
1135      .. versionadded:: 3.8
1136
1137   .. method:: cancel(msg=None)
1138
1139      Request the Task to be cancelled.
1140
1141      This arranges for a :exc:`CancelledError` exception to be thrown
1142      into the wrapped coroutine on the next cycle of the event loop.
1143
1144      The coroutine then has a chance to clean up or even deny the
1145      request by suppressing the exception with a :keyword:`try` ...
1146      ... ``except CancelledError`` ... :keyword:`finally` block.
1147      Therefore, unlike :meth:`Future.cancel`, :meth:`Task.cancel` does
1148      not guarantee that the Task will be cancelled, although
1149      suppressing cancellation completely is not common and is actively
1150      discouraged.  Should the coroutine nevertheless decide to suppress
1151      the cancellation, it needs to call :meth:`Task.uncancel` in addition
1152      to catching the exception.
1153
1154      .. versionchanged:: 3.9
1155         Added the *msg* parameter.
1156
1157      .. versionchanged:: 3.11
1158         The ``msg`` parameter is propagated from cancelled task to its awaiter.
1159
1160      .. _asyncio_example_task_cancel:
1161
1162      The following example illustrates how coroutines can intercept
1163      the cancellation request::
1164
1165          async def cancel_me():
1166              print('cancel_me(): before sleep')
1167
1168              try:
1169                  # Wait for 1 hour
1170                  await asyncio.sleep(3600)
1171              except asyncio.CancelledError:
1172                  print('cancel_me(): cancel sleep')
1173                  raise
1174              finally:
1175                  print('cancel_me(): after sleep')
1176
1177          async def main():
1178              # Create a "cancel_me" Task
1179              task = asyncio.create_task(cancel_me())
1180
1181              # Wait for 1 second
1182              await asyncio.sleep(1)
1183
1184              task.cancel()
1185              try:
1186                  await task
1187              except asyncio.CancelledError:
1188                  print("main(): cancel_me is cancelled now")
1189
1190          asyncio.run(main())
1191
1192          # Expected output:
1193          #
1194          #     cancel_me(): before sleep
1195          #     cancel_me(): cancel sleep
1196          #     cancel_me(): after sleep
1197          #     main(): cancel_me is cancelled now
1198
1199   .. method:: cancelled()
1200
1201      Return ``True`` if the Task is *cancelled*.
1202
1203      The Task is *cancelled* when the cancellation was requested with
1204      :meth:`cancel` and the wrapped coroutine propagated the
1205      :exc:`CancelledError` exception thrown into it.
1206
1207   .. method:: uncancel()
1208
1209      Decrement the count of cancellation requests to this Task.
1210
1211      Returns the remaining number of cancellation requests.
1212
1213      Note that once execution of a cancelled task completed, further
1214      calls to :meth:`uncancel` are ineffective.
1215
1216      .. versionadded:: 3.11
1217
1218      This method is used by asyncio's internals and isn't expected to be
1219      used by end-user code.  In particular, if a Task gets successfully
1220      uncancelled, this allows for elements of structured concurrency like
1221      :ref:`taskgroups` and :func:`asyncio.timeout` to continue running,
1222      isolating cancellation to the respective structured block.
1223      For example::
1224
1225        async def make_request_with_timeout():
1226            try:
1227                async with asyncio.timeout(1):
1228                    # Structured block affected by the timeout:
1229                    await make_request()
1230                    await make_another_request()
1231            except TimeoutError:
1232                log("There was a timeout")
1233            # Outer code not affected by the timeout:
1234            await unrelated_code()
1235
1236      While the block with ``make_request()`` and ``make_another_request()``
1237      might get cancelled due to the timeout, ``unrelated_code()`` should
1238      continue running even in case of the timeout.  This is implemented
1239      with :meth:`uncancel`.  :class:`TaskGroup` context managers use
1240      :func:`uncancel` in a similar fashion.
1241
1242      If end-user code is, for some reason, suppresing cancellation by
1243      catching :exc:`CancelledError`, it needs to call this method to remove
1244      the cancellation state.
1245
1246   .. method:: cancelling()
1247
1248      Return the number of pending cancellation requests to this Task, i.e.,
1249      the number of calls to :meth:`cancel` less the number of
1250      :meth:`uncancel` calls.
1251
1252      Note that if this number is greater than zero but the Task is
1253      still executing, :meth:`cancelled` will still return ``False``.
1254      This is because this number can be lowered by calling :meth:`uncancel`,
1255      which can lead to the task not being cancelled after all if the
1256      cancellation requests go down to zero.
1257
1258      This method is used by asyncio's internals and isn't expected to be
1259      used by end-user code.  See :meth:`uncancel` for more details.
1260
1261      .. versionadded:: 3.11
1262