-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
bpo-34831: Asyncio tutorial #9748
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
16d3b94
Create basic structure of the asyncio tutorial
cjrh 50a901e
Begun work on the case study for the server
cjrh dfede40
Incorporate review comments from @willingc
cjrh a11e659
Refine language around threads and processes
cjrh 7e205d2
Incorporate message handling into server code
cjrh 7f2f149
Add message receiving to server code.
cjrh 61402e1
Added skeleton suggestions for the cookbook section
cjrh 550bdbf
Further notes in the cookbook
cjrh e7bc56d
Further work on describing how async def functions work
cjrh 3d4cdae
Fix review comment from @tirkarthi
cjrh e0bb48b
Fix typo
cjrh 5e4550a
Clarify the "What is async" section
cjrh 0de2748
Flesh out the sync-versus-async functions section
cjrh 89364f8
Add the blurb entry
cjrh be474f4
Remove TODOs
cjrh c403101
Write "Executing Async Functions"
cjrh 69190b8
Fix spurious backtick
cjrh 89f7ca2
Make the case study (server) a little neater.
cjrh 36fc743
Some refactoring and finishing off the server.
cjrh d55d8fb
Cleaned up the last bit of the chat server code sample.
cjrh 34306f0
Further progress - got a CLI chat client working using prompt-toolkit.
cjrh 0c82755
Include chat client code in the text.
cjrh a774a98
Fix typo
cjrh eedbc97
Clarify switching behaviour
cjrh a8a801d
Add async generators and async context managers discussion.
cjrh 8e6dcfd
Add some comparison with JavaScript async/await and asyncio.create_task
cjrh 0e5ed3f
Fix "no good read" typo
cjrh 4714ed2
Fix "do not required" typo
cjrh d71da67
Modern -> modern
cjrh 26cc634
Removing the GUI case study section
cjrh 9530021
Remove problematic backticks inside a code-block
cjrh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Flesh out the sync-versus-async functions section
- Loading branch information
commit 0de27489edb84bbb768f01689b132783cbe20c13
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -35,7 +35,8 @@ Executing Async Functions | |||||
The ``run`` function is only good for executing an async function | ||||||
from "synchronous" code; and this is usually only used to execute | ||||||
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.
Suggested change
|
||||||
a "main" async function, from which others can be called in a simpler | ||||||
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.
Suggested change
|
||||||
way. | ||||||
way. You will not see ``asyncio.run()`` being called from inside an | ||||||
``async def`` function. | ||||||
|
||||||
That means the following: | ||||||
|
||||||
|
@@ -54,7 +55,7 @@ That means the following: | |||||
|
||||||
The execution of the the async function ``main()`` is performed | ||||||
with ``run()`` 8000 , but once you're inside an ``async def`` function, then | ||||||
all you need to execute another async function is the ``await`` keyword. | ||||||
calling another async function is done with the ``await`` keyword. | ||||||
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.
Suggested change
|
||||||
|
||||||
Ordinary (Sync) Functions Cannot Await Async Functions | ||||||
------------------------------------------------------ | ||||||
|
@@ -104,7 +105,7 @@ is also illegal: | |||||
|
||||||
So ``asyncio.run()`` is really intended only for launching your *first* | ||||||
async function; after that, every other async function should be | ||||||
executed using the ``await`` keyword, and the task-based methods which | ||||||
executed using the ``await`` keyword, and the task-based strategies which | ||||||
we've not yet discussed. | ||||||
|
||||||
Async Functions Can Call Sync Functions | ||||||
|
@@ -123,6 +124,42 @@ from inside ``async def`` functions. Here's an example: | |||||
>>> asyncio.run(f()) | ||||||
Sun Nov 4 15:04:45 2018 | ||||||
|
||||||
One of the benefits of ``asyncio`` is that you can see at a glance | ||||||
which code inside a function is subject to a context switch. In the | ||||||
following code example, we have two kinds of ``sleep()``: a blocking | ||||||
version from the ``time`` module, and an async version from ``asyncio``: | ||||||
|
||||||
.. code-block:: python3 | ||||||
|
||||||
>>> import time, asyncio | ||||||
>>> def func1(): | ||||||
... time.sleep(0) | ||||||
... | ||||||
>>> async def func2(): | ||||||
... await asyncio.sleep(0) | ||||||
... | ||||||
>>> async def main(): | ||||||
... await func2() # (1) | ||||||
... func1() | ||||||
... func1() | ||||||
... func1() | ||||||
... func1() | ||||||
... func1() | ||||||
... func1() | ||||||
... await func2() # (2) | ||||||
... | ||||||
>>> asyncio.run(main()) | ||||||
|
||||||
At (1), the underlying event loop is given the opportunity to switch from | ||||||
``main()`` to any other tasks that are waiting to run, and after line (1) | ||||||
returns, a series of calls to the sync function ``func1()`` occurs before | ||||||
the next allowable context switch on the event loop at (2). While the | ||||||
series of sync calls are running, *no other code* will execute in the | ||||||
current thread, until you get to the next ``await``. This guarantee applies | ||||||
a dramatic simplifying effect on your code, because now you can modify | ||||||
data shared between multiple async tasks without fear of introducing | ||||||
a race condition. | ||||||
|
||||||
Accurate Terminology For Async Functions | ||||||
---------------------------------------- | ||||||
|
||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.