Skip to content
Closed
Show file tree
Hide file tree
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 Oct 7, 2018
50a901e
Begun work on the case study for the server
cjrh Oct 14, 2018
dfede40
Incorporate review comments from @willingc
cjrh Oct 21, 2018
a11e659
Refine language around threads and processes
cjrh Oct 21, 2018
7e205d2
Incorporate message handling into server code
cjrh Oct 21, 2018
7f2f149
Add message receiving to server code.
cjrh Oct 21, 2018
61402e1
Added skeleton suggestions for the cookbook section
cjrh Oct 21, 2018
550bdbf
Further notes in the cookbook
cjrh Oct 21, 2018
e7bc56d
Further work on describing how async def functions work
cjrh Nov 4, 2018
3d4cdae
Fix review comment from @tirkarthi
cjrh Jun 15, 2019
e0bb48b
Fix typo
cjrh Jun 15, 2019
5e4550a
Clarify the "What is async" section
cjrh Jun 15, 2019
0de2748
Flesh out the sync-versus-async functions section
cjrh Jun 15, 2019
89364f8
Add the blurb entry
cjrh Jun 15, 2019
be474f4
Remove TODOs
cjrh Jun 15, 2019
c403101
Write "Executing Async Functions"
cjrh Jun 15, 2019
69190b8
Fix spurious backtick
cjrh Jun 15, 2019
89f7ca2
Make the case study (server) a little neater.
cjrh Jun 15, 2019
36fc743
Some refactoring and finishing off the server.
cjrh Jun 15, 2019
d55d8fb
Cleaned up the last bit of the chat server code sample.
cjrh Jun 16, 2019
34306f0
Further progress - got a CLI chat client working using prompt-toolkit.
cjrh Jun 16, 2019
0c82755
Include chat client code in the text.
cjrh Jun 16, 2019
a774a98
Fix typo
cjrh Jun 17, 2019
eedbc97
Clarify switching behaviour
cjrh Jun 17, 2019
a8a801d
Add async generators and async context managers discussion.
cjrh Jun 17, 2019
8e6dcfd
Add some comparison with JavaScript async/await and asyncio.create_task
cjrh Jun 17, 2019
0e5ed3f
Fix "no good read" typo
cjrh Jun 17, 2019
4714ed2
Fix "do not required" typo
cjrh Jun 17, 2019
d71da67
Modern -> modern
cjrh Jun 17, 2019
26cc634
Removing the GUI case study section
cjrh Jun 19, 2019
9530021
Remove problematic backticks inside a code-block
cjrh Sep 11, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Further notes in the cookbook
  • Loading branch information
cjrh committed Sep 11, 2019
commit 550bdbf43ae9cc41eecaa631822a2c9ec572caa9
25 changes: 25 additions & 0 deletions Doc/library/asyncio-tutorial/asyncio-cookbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ Using A Queue To Control A Pool of Resources
- show example with a pool of workers
- show example with a connection pool

Best Practices For Timeouts
---------------------------

- start with ``asyncio.wait_for()``
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- start with ``asyncio.wait_for()``
- start with :func:`asyncio.wait_for()`

- also look at ``asyncio.wait()``, and what to do if not all tasks
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- also look at ``asyncio.wait()``, and what to do if not all tasks
- also look at :func:`asyncio.wait()`, and what to do if not all tasks

are finished when the timeout happens. Also look at the different
termination conditions of ``asyncio.wait()``

How To Handle Cancellation
--------------------------

- app shutdown
- how to handle CancelledError and then close sockets
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- how to handle CancelledError and then close sockets
- how to handle :exc:`~asyncio.CancelledError` and then close sockets

- also, when waiting in a loop on ``await queue.get()`` is it better to
handle CancelledError, or use the idiom of putting ``None`` on the
queue? (``None`` would be better because it ensures the contents of the
queue get processed first, but I don't think we can prevent
CancelledError from getting raised so it must be handled anyway. I
can make an example to explain better.)

Keeping Track Of Many Connections
---------------------------------

Expand Down Expand Up @@ -105,6 +125,9 @@ TODO
if __name__ == '__main__':
asyncio.run(main())

- we should also include a brief discussion of "when to use asyncio.gather and
when to use asyncio.wait"

Secure Client-Server Networking
-------------------------------

Expand Down Expand Up @@ -197,6 +220,8 @@ Handling Typical Socket Errors

Might also want to show some examples of ``asyncio.IncompleteReadError``.

Also link/refer to the socket programming HOWTO in the docs.

Graceful Shutdown on Windows
----------------------------

Expand Down
32 changes: 20 additions & 12 deletions Doc/library/asyncio-tutorial/what-asyncio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ finish before the second call (to ``host2``) can begin.
Computers process things sequentially, which is why programming languages
like Python also work sequentially; but what we see here is that our
code is also going to **wait** sequentially. This is, quite literally,
a waste of time. What we would really like to do here is wait for the
a waste of time. What we would really like to do here is wait for
all the replies *concurrently*, i.e., at the same time.

Preemptive Concurrency
Expand All @@ -64,7 +64,8 @@ time on your device. At least, they will appear to be running
concurrently. What is really happening is that the operating system
is sharing little slices of processor (CPU) time among all the
processes. If we started two copies of our ``greet`` program at the
same time, then they *would* run (and therefore wait) concurrently.
same time, they *would* run (and therefore wait) concurrently which is
exactly what we want.

However, there is a price for that: each new process consumes resources
from the operating system. But more than that, there is another tricky
Expand All @@ -79,14 +80,19 @@ task being carried out by a computer system, without requiring
its cooperation, and with the intention of resuming the task
at a later time*.

This means that you will never be sure of when each of your processes
and threads is *actually* executing on a CPU. For processes, this
is quite safe because
Operating Systems do this kind of preemptive switching for both
processes and threads. A simplistic but useful description of the
difference is that one process can have multiple threads, and those
threads share all the memory in their parent process.

Because of this preemptive switching, you will never be sure of
when each of your processes and threads is *actually* executing on
a CPU. For processes, this is quite safe because
their memory spaces are isolated from each other; however,
**threads** are not isolated from each other. In fact, the primary
feature of threads over processes is that multiple threads within a
single process can access the same memory. And this is where all the
problems begin.
**threads** are not isolated from each other (within the same process).
In fact, the primary feature of threads over processes is that
multiple threads within a single process can access the same memory.
And this is where all the problems begin.

Jumping back to our code sample further up: we may also choose to run the
``greet()`` function in multiple threads; and then
Expand All @@ -96,7 +102,7 @@ with no control over
how execution will be transferred between the two threads (unless you
use the synchronization primitives in the ``threading`` module) . This
situation can result in *race conditions* in how objects are modified,
and these bugs can be very difficult to debug.
and these bugs can be very difficult to fix.

Cooperative Concurrency
-----------------------
Expand All @@ -106,11 +112,11 @@ multiple socket connections all in a single thread; and the best part
is that you get to control *when* execution is allowed to switch between
these different contexts.

We will explain more of the details later on in the tutorial,
We will explain more of the details throughout this tutorial,
but briefly, our earlier example becomes something like the following
pseudocode:

.. code-block:: python
.. code-block:: python3

import asyncio

Expand All @@ -123,6 +129,8 @@ pseudocode:
print('Reply:', repr(reply))

async def main():

# Both calls run at the same time
await asyncio.gather(
greet(host1, port1),
greet(host2, port2)
Expand Down