- In Python and ES6,
await/async generator-based. Is it right to consider futures the same as Promises?
Not Future , but Python Task roughly equivalent to Javascript Promise . See below for more details.
- I saw the terms
Task , Future and Coroutine used in the asyncio documentation. What are the differences between the two?
These are completely different concepts. Basically Task consists of Future and Coroutine . Let's briefly describe these primitives (I'm going to simplify a lot of things to describe only the basic principles):
Future
The future is simply an abstraction of value that may not yet be calculated and will be available in the long run. This is a simple container that does only one thing - every time a value is set, start all registered callbacks.
If you want to get this value, you register a callback through the add_done_callback() method.
But unlike Promise , the actual calculation is done externally - and this external code must call the set_result() method to solve the future.
coroutine
Coroutine is an object very similar to Generator .
The generator is usually repeated in a for loop. It gives values ββand, starting with PEP342 , it receives values.
A route is usually repeated inside an event loop in the depths of the asyncio library. Corutin gives instances of Future . When you iterate through a coroutine, and it gives the future, you must wait until that future is resolved. After that you will send value of the future in the coroutine, then you will get a different future, etc.
An await expression is almost identical to the yield from expression, therefore, waiting for another coroutine, you stop until this coroutine completes all its futures and receives the return value of coroutine. Future has an iteration of the same name, and its iterator returns the actual future - this roughly means that await future is equal to yield from future is equal to yield future .
Task
The "task of the future", which actually began to be calculated and tied to a cycle of events. Thus, a special kind of Future (the Task class is derived from the Future class), which is associated with some event loop, and it has some coroutine that serves as a task executor.
A task is usually created by an event loop object: you provide a coroutine for the loop, it creates a Task object and iterates over this coroutine in the manner described above. Once coroutine is complete, Task Future will be restored using the coroutine return value.
You see, the task is very similar to JS Promise - it encapsulates the background task and its result.
Coroutine Function and Async Function
The coroutine func function is a factory coroutine such as a generator function for generators. Note the difference between the Python coprocessor function and the async Javascript function. The async JS function creates Promise when called, and its internal generator starts iteration immediately, while the Pouton coroutine does nothing until Task is created on it.
- Should I start writing Python code that always has an event loop?
If you need any asynchronous function then you need. As it turned out, it's quite difficult to mix synchronous and asynchronous code - your whole program is better asynchronous (but you can run synchronous pieces of code in separate threads through the asyncio threadpool API)