Tasks and coroutines
Coroutines
A coroutine is a generator that follows certain conventions. For
documentation purposes, all coroutines should be decorated with
@asyncio.coroutine
, but this cannot be strictly enforced.
Coroutines use the yield from
syntax introduced in PEP 380,
instead of the original yield
syntax.
The word "coroutine", like the word "generator", is used for two different (though related) concepts:
- The function that defines a coroutine (a function definition
decorated with
@asyncio.coroutine
). If disambiguation is needed we will call this a coroutine function (:func:`iscoroutinefunction` returnsTrue
). - The object obtained by calling a coroutine function. This object
represents a computation or an I/O operation (usually a combination)
that will complete eventually. If disambiguation is needed we will
call it a coroutine object (:func:`iscoroutine` returns
True
).
Things a coroutine can do:
-
result = yield from future
-- suspends the coroutine until the future is done, then returns the future's result, or raises an exception, which will be propagated. (If the future is cancelled, it will raise aCancelledError
exception.) Note that tasks are futures, and everything said about futures also applies to tasks. -
result = yield from coroutine
-- wait for another coroutine to produce a result (or raise an exception, which will be propagated). Thecoroutine
expression must be a call to another coroutine. -
return expression
-- produce a result to the coroutine that is waiting for this one usingyield from
. -
raise exception
-- raise an exception in the coroutine that is waiting for this one usingyield from
.
Calling a coroutine does not start its code running -- it is just a
generator, and the coroutine object returned by the call is really a
generator object, which doesn't do anything until you iterate over it.
In the case of a coroutine object, there are two basic ways to start
it running: call yield from coroutine
from another coroutine
(assuming the other coroutine is already running!), or schedule its execution
using the :func:`async` function or the :meth:`BaseEventLoop.create_task`
method.
Coroutines (and tasks) can only run when the event loop is running.
Note
In this documentation, some methods are documented as coroutines, even if they are plain Python functions returning a :class:`Future`. This is intentional to have a freedom of tweaking the implementation of these functions in the future. If such a function is needed to be used in a callback-style code, wrap its result with :func:`async`.
Example: Hello World coroutine
Example of coroutine displaying "Hello World"
:
import asyncio
@asyncio.coroutine
def hello_world():
print("Hello World!")
loop = asyncio.get_event_loop()
# Blocking call which returns when the hello_world() coroutine is done
loop.run_until_complete(hello_world())
loop.close()
Example: Coroutine displaying the current date
Example of coroutine displaying the current date every second during 5 seconds using the :meth:`sleep` function:
import asyncio
import datetime
@asyncio.coroutine
def display_date(loop):
end_time = loop.time() + 5.0
while True:
print(datetime.datetime.now())
if (loop.time() + 1.0) >= end_time:
break
yield from asyncio.sleep(1)
loop = asyncio.get_event_loop()
# Blocking call which returns when the display_date() coroutine is done
loop.run_until_complete(display_date(loop))
loop.close()
Example: Chain coroutines
Example chaining coroutines:
import asyncio
@asyncio.coroutine
def compute(x, y):
print("Compute %s + %s ..." % (x, y))
yield from asyncio.sleep(1.0)
return x + y
@asyncio.coroutine
def print_sum(x, y):
result = yield from compute(x, y)
print("%s + %s = %s" % (x, y, result))
loop = asyncio.get_event_loop()
loop.run_until_complete(print_sum(1, 2))
loop.close()
compute()
is chained to print_sum()
: print_sum()
coroutine waits
until compute()
is completed before returning its result.
Sequence diagram of the example:

The "Task" is created by the :meth:`BaseEventLoop.run_until_complete` method when it gets a coroutine object instead of a task.
The diagram shows the control flow, it does not describe exactly how things work internally. For example, the sleep coroutine creates an internal future which uses :meth:`BaseEventLoop.call_later` to wake up the task in 1 second.
InvalidStateError
TimeoutError
Note
This exception is different from the builtin :exc:`TimeoutError` exception!
Future
This class is almost compatible with :class:`concurrent.futures.Future`.
Differences:
- :meth:`result` and :meth:`exception` do not take a timeout argument and raise an exception when the future isn't done yet.
- Callbacks registered with :meth:`add_done_callback` are always called via the event loop's :meth:`~BaseEventLoop.call_soon_threadsafe`.
- This class is not compatible with the :func:`~concurrent.futures.wait` and :func:`~concurrent.futures.as_completed` functions in the :mod:`concurrent.futures` package.
This class is :ref:`not thread safe <asyncio-multithreading>`.
Example: Future with run_until_complete()
Example combining a :class:`Future` and a :ref:`coroutine function <coroutine>`:
import asyncio
@asyncio.coroutine
def slow_operation(future):
yield from asyncio.sleep(1)
future.set_result('Future is done!')
loop = asyncio.get_event_loop()
future = asyncio.Future()
asyncio.async(slow_operation(future))
loop.run_until_complete(future)
print(future.result())
loop.close()
The coroutine function is responsible for the computation (which takes 1 second) and it stores the result into the future. The :meth:`~BaseEventLoop.run_until_complete` method waits for the completion of the future.
Note
The :meth:`~BaseEventLoop.run_until_complete` method uses internally the :meth:`~Future.add_done_callback` method to be notified when the future is done.
Example: Future with run_forever()
The previous example can be written differently using the :meth:`Future.add_done_callback` method to describe explicitly the control flow:
import asyncio
@asyncio.coroutine
def slow_operation(future):
yield from asyncio.sleep(1)
future.set_result('Future is done!')
def got_result(future):
print(future.result())
loop.stop()
loop = asyncio.get_event_loop()
future = asyncio.Future()
asyncio.async(slow_operation(future))
future.add_done_callback(got_result)
try:
loop.run_forever()
finally:
loop.close()
In this example, the future is used to link slow_operation()
to
got_result()
: when slow_operation()
is done, got_result()
is called
with the result.
Task
Example: Parallel execution of tasks
Example executing 3 tasks (A, B, C) in parallel:
import asyncio
@asyncio.coroutine
def factorial(name, number):
f = 1
for i in range(2, number+1):
print("Task %s: Compute factorial(%s)..." % (name, i))
yield from asyncio.sleep(1)
f *= i
print("Task %s: factorial(%s) = %s" % (name, number, f))
loop = asyncio.get_event_loop()
tasks = [
asyncio.async(factorial("A", 2)),
asyncio.async(factorial("B", 3)),
asyncio.async(factorial("C", 4))]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
Output:
Task A: Compute factorial(2)...
Task B: Compute factorial(2)...
Task C: Compute factorial(2)...
Task A: factorial(2) = 2
Task B: Compute factorial(3)...
Task C: Compute factorial(3)...
Task B: factorial(3) = 6
Task C: Compute factorial(4)...
Task C: factorial(4) = 24
A task is automatically scheduled for execution when it is created. The event loop stops when all tasks are done.
Task functions
Note
In the functions below, the optional loop argument allows to explicitly set the event loop object used by the underlying task or coroutine. If it's not provided, the default event loop is used.