-
-
Notifications
You must be signed in to change notification settings - Fork 33k
gh-138072: Small clarifications and phrasing improvements to asyncio HOWTO #138073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 11 commits
07d5a0a
34f496f
b189b94
f97bfb6
a389c09
2831208
274c727
12d6e35
176814e
e475aef
1391594
0692644
13843e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,7 @@ | ||||||
.. _a-conceptual-overview-of-asyncio: | ||||||
|
||||||
**************************************** | ||||||
A Conceptual Overview of :mod:`!asyncio` | ||||||
A conceptual overview of :mod:`!asyncio` | ||||||
**************************************** | ||||||
|
||||||
This :ref:`HOWTO <how-tos>` article seeks to help you build a sturdy mental | ||||||
|
@@ -38,15 +38,15 @@ In part 1, we'll cover the main, high-level building blocks of :mod:`!asyncio`: | |||||
the event loop, coroutine functions, coroutine objects, tasks and ``await``. | ||||||
|
||||||
========== | ||||||
Event Loop | ||||||
Event loop | ||||||
========== | ||||||
|
||||||
Everything in :mod:`!asyncio` happens relative to the event loop. | ||||||
It's the star of the show. | ||||||
It's the star of the show, but prefers to work behind the scenes, managing | ||||||
and coordinating resources. | ||||||
It's like an orchestra conductor. | ||||||
It's behind the scenes managing resources. | ||||||
Some power is explicitly granted to it, but a lot of its ability to get things | ||||||
done comes from the respect and cooperation of its worker bees. | ||||||
done comes from the respect and cooperation of its band members. | ||||||
|
||||||
In more technical terms, the event loop contains a collection of jobs to be run. | ||||||
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 | |||||
onwards. | ||||||
If there are no more jobs pending execution, the event loop is smart enough to | ||||||
rest and avoid needlessly wasting CPU cycles, and will come back when there's | ||||||
more work to be done. | ||||||
more work to be done, such as when I/O operations complete or timers expire. | ||||||
|
||||||
Effective execution relies on jobs sharing well and cooperating; a greedy job | ||||||
could hog control and leave the other jobs to starve, rendering the overall | ||||||
|
@@ -171,14 +171,14 @@ Roughly speaking, :ref:`tasks <asyncio-task-obj>` are coroutines (not coroutine | |||||
functions) tied to an event loop. | ||||||
A task also maintains a list of callback functions whose importance will become | ||||||
clear in a moment when we discuss :keyword:`await`. | ||||||
The recommended way to create tasks is via :func:`asyncio.create_task`. | ||||||
|
||||||
Creating a task automatically schedules it for execution (by adding a | ||||||
callback to run it in the event loop's to-do list, that is, collection of jobs). | ||||||
The recommended way to create tasks is via :func:`asyncio.create_task`. | ||||||
|
||||||
Since there's only one event loop (in each thread), :mod:`!asyncio` takes care of | ||||||
associating the task with the event loop for you. As such, there's no need | ||||||
to specify the event loop. | ||||||
Since there's only one event loop (in each thread), :mod:`!asyncio` takes | ||||||
care of associating the task with the event loop for you. | ||||||
As such, there's no need to specify the event loop. | ||||||
|
||||||
:: | ||||||
|
||||||
|
@@ -251,6 +251,10 @@ different ways:: | |||||
In a crucial way, the behavior of ``await`` depends on the type of object | ||||||
being awaited. | ||||||
|
||||||
^^^^^^^^^^ | ||||||
await task | ||||||
anordin95 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
^^^^^^^^^^ | ||||||
|
||||||
Awaiting a task will cede control from the current task or coroutine to | ||||||
the event loop. | ||||||
In the process of relinquishing control, a few important things happen. | ||||||
|
@@ -282,6 +286,10 @@ This is a basic, yet reliable mental model. | |||||
In practice, the control handoffs are slightly more complex, but not by much. | ||||||
In part 2, we'll walk through the details that make this possible. | ||||||
|
||||||
^^^^^^^^^^^^^^^ | ||||||
await coroutine | ||||||
anordin95 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
^^^^^^^^^^^^^^^ | ||||||
|
||||||
**Unlike tasks, awaiting a coroutine does not hand control back to the event | ||||||
loop!** | ||||||
Wrapping a coroutine in a task first, then awaiting that would cede | ||||||
|
@@ -348,8 +356,10 @@ The design intentionally trades off some conceptual clarity around usage of | |||||
``await`` for improved performance. | ||||||
Each time a task is awaited, control needs to be passed all the way up the | ||||||
call stack to the event loop. | ||||||
That might sound minor, but in a large program with many ``await``'s and a deep | ||||||
callstack that overhead can add up to a meaningful performance drag. | ||||||
Then, the event loop needs to manage its internal state and work through | ||||||
its processing logic to resume the next job. | ||||||
That might sound minor, but in a large program with many ``await``\ s, that | ||||||
overhead can add up to a meaningful performance drag. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think "meaningful" has a positive connotation here, which we don't want. How about this?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to make sure I understand, the concern is that "meaningful" has a positive connotation, which may confuse a reader into thinking we're saying a "performance drag" is a good thing, yeah? I think "major" would be overstating the effect. The goal was to describe something that's non-trivial or non-negligible. For what it's worth, I think "meaningful" can be used either way e.g. "a meaningful decline in sales", "a meaningful improvement in test scores", etc. Regardless, would switching to "non-trivial" be amenable? |
||||||
|
||||||
------------------------------------------------ | ||||||
A conceptual overview part 2: the nuts and bolts | ||||||
|
@@ -365,7 +375,8 @@ and how to make your own asynchronous operators. | |||||
The inner workings of coroutines | ||||||
================================ | ||||||
|
||||||
:mod:`!asyncio` leverages four components to pass around control. | ||||||
:mod:`!asyncio` leverages four components of Python to pass | ||||||
around control. | ||||||
|
||||||
:meth:`coroutine.send(arg) <generator.send>` is the method used to start or | ||||||
resume a coroutine. | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a bit redundant to mention
asyncio
again given the prior sentence also does.