-
Notifications
You must be signed in to change notification settings - Fork 26
add example explaining concurrent programming with coroutines #15
Comments
Re-implementing asyncio from scratch requires:
All those concepts are more or less interdependent. I don't see how to explain asyncio coroutines without tasks + futures + event loop, and single-threaded concurrency without a selector. It's an interesting exercise though, I wonder how long a basic asyncio implementation could be. |
This is more about concurrent programming model with coroutines than asyncio itself. As you can see, you can run |
Coroutines alone are not enough to provide concurrency. Rather, they can be used along with a cooperative scheduler (i.e an event loop) to implement cooperative multitasking. That also means you need a protocol for the coroutines and the scheduler to communicate. In asyncio, this is done by yielding from a future, which means "please wake me up when this operation is finished". In your example, you can easily replace the coroutines with regular classes, and it would work the same:
|
Probably we are splitting hairs over definitions here a bit. You can replace "concurrency" with "multitasking" (maybe wording of https://en.wikipedia.org/wiki/Coroutine and http://www.dabeaz.com/coroutines/ will help to explain my position). You can use classes of course, but then you have to maintain state of your processing on object level - global from class method perspective. Every example provided in presentations at http://www.dabeaz.com/coroutines/ can be reimplemented with classes. Async I/O can be reimplemented with callbacks. But I believe we all agree that coroutines provide better programming model, so we use them and IMHO there is no point to discuss this. :) It seems we disagree on the need of event loop. Point of my example is that there is no event loop. You can analyze the code of the example and see when and what happens - there is no magic of event loop. It is illustration of multitasking (or concurrency or cooperative execution) with coroutines. Nothing else, nothing more. The programming model is provided by Python language coroutines. Asyncio uses the model to enable asynchronous I/O programming. IMHO, if we first explain the former, the latter will be easier to understand. |
Yes I used them as synonyms, sorry if I didn't make that clear.
Indeed!
What bothers me in your example is this part:
Things here are executed in a very strict order, so it looks much more like serialized calls than concurrent tasks. I wouldn't call that concurrency, but as you said, it might be a matter of definition. Anyway, this is how I would illustrate concurrency using coroutines instead. |
Indeed, the order is quite strict and non-coroutine part of my example is quite long. Would be nice to have shorter example to analyze. BTW. The following article explains coroutines in Python. If asyncio docs do not have its own description of coroutines, then maybe it would be worth to link to it? |
That part of the article is quite interesting:
Now keep in mind that this article is from 2013, and it was only the beginning of asyncio. Here's what PEP 3156 says about coroutines:
Then, in 2015, PEP 492 introduced
and call everything else using In my opinion, this article by Brett Cannon would be a better fit for the documentation: |
I shut down the project: #33 |
I wonder if the following example should be included into the docs to explain concurrent programming with coroutines.
The code uses generator coroutine syntax and is easy to follow
After describing the example, it could be folded into async/await version running on asyncio event loop. Benefits of asyncio event loop can be shown, i.e.
Would that make sense?
The text was updated successfully, but these errors were encountered: