Differences between Futures in Python3 and Promises in ES6
- In both Python and ES6,
await/async
are based on generators. Is it a correct to think Futures are the same as Promises?
Not Future
, but Python's Task
is roughly equivalent to Javascript's Promise
. See more details below.
- I have seen the terms
Task
,Future
andCoroutine
used in theasyncio
documentation. What are the differences between them?
They're quite different concepts. Mainly, Task
consists of Future
and Coroutine
. Let's describe these primitives briefly (I am going to simplify lots of things to describe only main principles):
Future
Future is simply an abstraction of value that may be not computed yet and will be available eventually. It's a simple container that only does one thing - whenever the value is set, fire all registered callbacks.
If you want to obtain that value, you register a callback via add_done_callback()
method.
But unlike in Promise
, the actual computation is done externally - and that external code has to call set_result()
method to resolve the future.
Coroutine
Coroutine is the object very similar to Generator
.
A generator is typically iterated within for
loop. It yields values and, starting from PEP342 acceptance, it receives values.
A coroutine is typically iterated within the event loop in depths of asyncio
library. A coroutine yields Future
instances. When you are iterating over a coroutine and it yields a future, you shall wait until this future is resolved. After that you shall send
the value of future into the coroutine, then you receive another future, and so on.
An await
expression is practically identical to yield from
expression, so by awaiting other coroutine, you stop until that coroutine has all its futures resolved, and get coroutine's return value. The Future
is one-tick iterable and its iterator returns actual Future - that roughly means that await future
equals yield from future
equals yield future
.
Task
Task is Future which has been actually started to compute and is attached to event loop. So it's special kind of Future (class Task
is derived from class Future
), which is associated with some event loop, and it has some coroutine, which serves as Task executor.
Task is usually created by event loop object: you give a coroutine to the loop, it creates Task object and starts to iterate over that coroutine in manner described above. Once the coroutine is finished, Task's Future is resolved by coroutine's return value.
You see, the task is quite similar to JS Promise - it encapsulates background job and its result.
Coroutine Function and Async Function
Coroutine func is a factory of coroutines, like generator function to generators. Notice the difference between Python's coroutine function and Javascript's async function - JS async function, when called, creates a Promise and its internal generator immediately starts being iterated, while Python's coroutine does nothing, until Task is created upon it.
- Should I start writing Python code that always has an event loop running?
If you need any asyncio feature, then you should. As it turns out it's quite hard to mix synchronous and asynchronous code - your whole program had better be asynchronous (but you can launch synchronous code chunks in separate threads via asyncio threadpool API)