Skip to content

Commit ef71d25

Browse files
committed
- Modify language re: event-loop cycling endlessly.
- Discuss why await was designed to not yield for coros.
1 parent 1f8f863 commit ef71d25

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

Doc/howto/a-conceptual-overview-of-asyncio.rst

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ The event loop pops a job from the queue and invokes it (or "gives it control"),
4848
similar to calling a function, and then that job runs.
4949
Once it pauses or completes, it returns control to the event loop.
5050
The event loop will then move on to the next job in its queue and invoke it.
51-
This process repeats indefinitely.
52-
Even if the queue is empty, the event loop continues to cycle (somewhat
53-
aimlessly).
51+
This process repeats indefinitely with the event loop cycling endlessly
52+
onwards.
53+
If the queue is empty, the event loop is smart enough to rest and avoid
54+
needlessly wasting CPU cycles, and will come back when there's more work
55+
to be done.
5456

5557
Effective execution relies on tasks sharing well and cooperating; a greedy job
5658
could hog control and leave the other jobs to starve, rendering the overall
@@ -232,7 +234,7 @@ the original task or coroutine (``plant_a_tree()``) is added back to the event
232234
loops queue to be resumed.
233235

234236
This is a basic, yet reliable mental model.
235-
In practice, it's slightly more complex, but not by much.
237+
In practice, the control handoffs are slightly more complex, but not by much.
236238
In part 2, we'll walk through the details that make this possible.
237239

238240
**Unlike tasks, awaiting a coroutine does not hand control back to the event
@@ -286,6 +288,18 @@ The event loop then works through its queue, calling ``coro_b()`` and then
286288
I am coro_a(). Hi!
287289
I am coro_a(). Hi!
288290
291+
This behavior of ``await coroutine`` can trip a lot of people up!
292+
That example highlights how using only ``await coroutine`` could
293+
unintentionally hog control from other tasks and effectively stall the event
294+
loop.
295+
296+
The design intentionally trades off some conceptual clarity around usage of
297+
``await`` for improved performance.
298+
Each time a task is awaited, control needs to be passed all the way up the
299+
call stack to the event loop.
300+
That might sound minor, but in a large program with a deep callstack that
301+
overhead can add up to a meaningful performance drag.
302+
289303
----------------------------------
290304
How coroutines work under the hood
291305
----------------------------------

0 commit comments

Comments
 (0)