Skip to content

Commit 07d5a0a

Browse files
committed
- Small clarifications and phrasing improvements
1 parent f278afc commit 07d5a0a

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ Event Loop
4242
==========
4343

4444
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.
4647
It's like an orchestra conductor.
47-
It's behind the scenes managing resources.
4848
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.
5050

5151
In more technical terms, the event loop contains a collection of jobs to be run.
5252
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
6060
onwards.
6161
If there are no more jobs pending execution, the event loop is smart enough to
6262
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.
6464

6565
Effective execution relies on jobs sharing well and cooperating; a greedy job
6666
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
171171
functions) tied to an event loop.
172172
A task also maintains a list of callback functions whose importance will become
173173
clear in a moment when we discuss :keyword:`await`.
174-
The recommended way to create tasks is via :func:`asyncio.create_task`.
175174

176175
Creating a task automatically schedules it for execution (by adding a
177176
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`.
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::
251252
In a crucial way, the behavior of ``await`` depends on the type of object
252253
being awaited.
253254

255+
^^^^^^^^^^
256+
await task
257+
^^^^^^^^^^
258+
254259
Awaiting a task will cede control from the current task or coroutine to
255260
the event loop.
256261
In the process of relinquishing control, a few important things happen.
@@ -282,6 +287,10 @@ This is a basic, yet reliable mental model.
282287
In practice, the control handoffs are slightly more complex, but not by much.
283288
In 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
286295
loop!**
287296
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
348357
``await`` for improved performance.
349358
Each time a task is awaited, control needs to be passed all the way up the
350359
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.
353364

354365
------------------------------------------------
355366
A conceptual overview part 2: the nuts and bolts
@@ -365,7 +376,8 @@ and how to make your own asynchronous operators.
365376
The 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
371383
resume a coroutine.

0 commit comments

Comments
 (0)