@@ -42,11 +42,11 @@ Event Loop
4242==========
4343
4444Everything 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.
4647It's like an orchestra conductor.
47- It's behind the scenes managing resources.
4848Some 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 .
5050
5151In more technical terms, the event loop contains a collection of jobs to be run.
5252Some 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
6060onwards.
6161If there are no more jobs pending execution, the event loop is smart enough to
6262rest 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 .
6464
6565Effective execution relies on jobs sharing well and cooperating; a greedy job
6666could 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
171171functions) tied to an event loop.
172172A task also maintains a list of callback functions whose importance will become
173173clear in a moment when we discuss :keyword: `await `.
174- The recommended way to create tasks is via :func: `asyncio.create_task `.
175174
176175Creating a task automatically schedules it for execution (by adding a
177176callback 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 `.
178178
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.
182183
183184::
184185
@@ -251,6 +252,10 @@ different ways::
251252In a crucial way, the behavior of ``await `` depends on the type of object
252253being awaited.
253254
255+ ^^^^^^^^^^
256+ await task
257+ ^^^^^^^^^^
258+
254259Awaiting a task will cede control from the current task or coroutine to
255260the event loop.
256261In the process of relinquishing control, a few important things happen.
@@ -282,6 +287,10 @@ This is a basic, yet reliable mental model.
282287In practice, the control handoffs are slightly more complex, but not by much.
283288In part 2, we'll walk through the details that make this possible.
284289
290+ ^^^^^^^^^^^^^^^
291+ await coroutine
292+ ^^^^^^^^^^^^^^^
293+
285294**Unlike tasks, awaiting a coroutine does not hand control back to the event
286295loop! **
287296Wrapping 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
348357``await `` for improved performance.
349358Each time a task is awaited, control needs to be passed all the way up the
350359call 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.
353364
354365------------------------------------------------
355366A conceptual overview part 2: the nuts and bolts
@@ -365,7 +376,8 @@ and how to make your own asynchronous operators.
365376The inner workings of coroutines
366377================================
367378
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.
369381
370382:meth: `coroutine.send(arg) <generator.send> ` is the method used to start or
371383resume a coroutine.
0 commit comments