@@ -42,11 +42,11 @@ Event Loop
42
42
==========
43
43
44
44
Everything in :mod: `!asyncio ` happens relative to the event loop.
45
- It's the star of the show.
45
+ It's the star of the show, but prefers to work behind the scenes managing
46
+ and coordinating resources.
46
47
It's like an orchestra conductor.
47
- It's behind the scenes managing resources.
48
48
Some power is explicitly granted to it, but a lot of its ability to get things
49
- done comes from the respect and cooperation of its worker bees .
49
+ done comes from the respect and cooperation of its band members .
50
50
51
51
In more technical terms, the event loop contains a collection of jobs to be run.
52
52
Some jobs are added directly by you, and some indirectly by :mod: `!asyncio `.
@@ -60,7 +60,7 @@ This process repeats indefinitely with the event loop cycling endlessly
60
60
onwards.
61
61
If there are no more jobs pending execution, the event loop is smart enough to
62
62
rest and avoid needlessly wasting CPU cycles, and will come back when there's
63
- more work to be done.
63
+ more work to be done - such as when I/O operations complete or timers expire .
64
64
65
65
Effective execution relies on jobs sharing well and cooperating; a greedy job
66
66
could hog control and leave the other jobs to starve, rendering the overall
@@ -171,14 +171,15 @@ Roughly speaking, :ref:`tasks <asyncio-task-obj>` are coroutines (not coroutine
171
171
functions) tied to an event loop.
172
172
A task also maintains a list of callback functions whose importance will become
173
173
clear in a moment when we discuss :keyword: `await `.
174
- The recommended way to create tasks is via :func: `asyncio.create_task `.
175
174
176
175
Creating a task automatically schedules it for execution (by adding a
177
176
callback to run it in the event loop's to-do list, that is, collection of jobs).
177
+ The recommended way to create tasks is via :func: `asyncio.create_task `.
178
178
179
- Since there's only one event loop (in each thread), :mod: `!asyncio ` takes care of
180
- associating the task with the event loop for you. As such, there's no need
181
- to specify the event loop.
179
+ Since there's only one event loop (per thread; in thread-local storage),
180
+ :mod: `!asyncio ` takes care of associating the task with the event loop for
181
+ you.
182
+ As such, there's no need to specify the event loop.
182
183
183
184
::
184
185
@@ -251,6 +252,10 @@ different ways::
251
252
In a crucial way, the behavior of ``await `` depends on the type of object
252
253
being awaited.
253
254
255
+ ^^^^^^^^^^
256
+ await task
257
+ ^^^^^^^^^^
258
+
254
259
Awaiting a task will cede control from the current task or coroutine to
255
260
the event loop.
256
261
In the process of relinquishing control, a few important things happen.
@@ -282,6 +287,10 @@ This is a basic, yet reliable mental model.
282
287
In practice, the control handoffs are slightly more complex, but not by much.
283
288
In part 2, we'll walk through the details that make this possible.
284
289
290
+ ^^^^^^^^^^^^^^^
291
+ await coroutine
292
+ ^^^^^^^^^^^^^^^
293
+
285
294
**Unlike tasks, awaiting a coroutine does not hand control back to the event
286
295
loop! **
287
296
Wrapping a coroutine in a task first, then awaiting that would cede
@@ -348,8 +357,10 @@ The design intentionally trades off some conceptual clarity around usage of
348
357
``await `` for improved performance.
349
358
Each time a task is awaited, control needs to be passed all the way up the
350
359
call stack to the event loop.
351
- That might sound minor, but in a large program with many ``await ``'s and a deep
352
- callstack that overhead can add up to a meaningful performance drag.
360
+ Then, the event loop needs to manage its data structures and work through
361
+ its processing logic to resume the next job.
362
+ That might sound minor, but in a large program with many ``await task ``'s that
363
+ overhead can add up to a meaningful performance drag.
353
364
354
365
------------------------------------------------
355
366
A conceptual overview part 2: the nuts and bolts
@@ -365,7 +376,8 @@ and how to make your own asynchronous operators.
365
376
The inner workings of coroutines
366
377
================================
367
378
368
- :mod: `!asyncio ` leverages four components to pass around control.
379
+ :mod: `!asyncio ` leverages four components of the Python language to pass
380
+ around control.
369
381
370
382
:meth: `coroutine.send(arg) <generator.send> ` is the method used to start or
371
383
resume a coroutine.
0 commit comments