|
| 1 | +++++++++++++++++++ |
| 2 | +Why using asyncio? |
| 3 | +++++++++++++++++++ |
| 4 | + |
| 5 | +Why asynchronous programming? |
| 6 | +============================= |
| 7 | + |
| 8 | +asyncio is a library to write asynchronous applications. It is the most |
| 9 | +efficient way to implement a network server having to handle many concurrent |
| 10 | +users. |
| 11 | + |
| 12 | + |
| 13 | +But gevent and eventlet just work! |
| 14 | +================================== |
| 15 | + |
| 16 | +Or *Why should I bother with all these extra annoying async and await |
| 17 | +keywords?*. |
| 18 | + |
| 19 | +In short, asyncio adopted a radically different solution for race conditions. |
| 20 | + |
| 21 | +Parallel computing using threads is hard because of race conditions. Gevent and |
| 22 | +eventlet have a similar issue using "green" (lightweight) threads. |
| 23 | + |
| 24 | +Code written with asyncio is less error-prone: by just looking at the code, it |
| 25 | +is possible to identify which parts of the code are under our controls and |
| 26 | +where the event loop takes over the control flow and is able to run other tasks |
| 27 | +when our task is waiting for something. |
| 28 | + |
| 29 | +gevent and eventlet are designed to hide the asynchronous programming. For |
| 30 | +non-expert, and sometimes even for experts, it is really hard to guess where |
| 31 | +the event loop is allowed to suspend the task and run other tasks in |
| 32 | +background. It is even worse. A modification in a third party library can |
| 33 | +change the behaviour of our code, introduce a new point where the task is |
| 34 | +suspended. |
| 35 | + |
| 36 | +For an example, see the "Ca(sh|che Coherent) Money" section of the `Unyielding |
| 37 | +<https://glyph.twistedmatrix.com/2014/02/unyielding.html>` article (by Glyph, |
| 38 | +February, 2014). |
0 commit comments