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 progress - got a CLI chat client working using prompt-toolkit.
  • Loading branch information
cjrh committed Sep 11, 2019
commit 34306f0ae8e78f0e0360775b8b00d054962c8303
2 changes: 1 addition & 1 deletion Doc/library/asyncio-tutorial/case-study-chat-server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ membership:

.. literalinclude:: server05.py
:caption: Joining and leaving rooms
:lines: 9-10,18-40
:lines: 9-10,18-39
:language: python3

When we receive a request to join
Expand Down
36 changes: 36 additions & 0 deletions Doc/library/asyncio-tutorial/client05.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import asyncio
from utils import new_messages, send_message

from prompt_toolkit.eventloop.defaults import use_asyncio_event_loop
from prompt_toolkit.patch_stdout import patch_stdout
from prompt_toolkit.shortcuts import PromptSession


async def main():
use_asyncio_event_loop()

reader, writer = await asyncio.open_connection('localhost', '9011')
await send_message(writer, dict(action='connect', username='Eric'))
await send_message(writer, dict(action='joinroom', room='nonsense'))

asyncio.create_task(enter_message(writer))

async for msg in new_messages(reader):
print(f"{msg['from']}: {msg['message']}")


async def enter_message(writer):
session = PromptSession('Send message: ', erase_when_done=True)
while True:
try:
msg = await session.prompt(async_=True)
if not msg:
continue
await send_message(writer, msg)
except (EOFError, asyncio.CancelledError):
return


if __name__ == '__main__':
with patch_stdout():
asyncio.run(main())
32 changes: 32 additions & 0 deletions Doc/library/asyncio-tutorial/pttest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import asyncio

from prompt_toolkit.eventloop.defaults import use_asyncio_event_loop
from prompt_toolkit.patch_stdout import patch_stdout
from prompt_toolkit.shortcuts import PromptSession


async def blah():
while True:
print('.')
await asyncio.sleep(10.0)


async def prompt():
session = PromptSession('Message: ', erase_when_done=True)
while True:
try:
msg = await session.prompt(async_=True)
print(msg)
except (EOFError, asyncio.CancelledError):
return


async def main():
use_asyncio_event_loop()
await asyncio.gather(blah(), prompt())
# await asyncio.gather(blah())


if __name__ == '__main__':
with patch_stdout():
asyncio.run(main())
3 changes: 1 addition & 2 deletions Doc/library/asyncio-tutorial/server05.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ def leaveroom(msg):

def chat(msg):
print(f'chat sent to room {msg.get("room")}: {msg.get("message")}')
payload = json.dumps(msg).encode()
room = ROOMS[msg["room"]]
for friend in room:
asyncio.create_task(send_message(friend, payload))
asyncio.create_task(send_message(friend, msg))

handlers: Dict[str, Callable[[Dict], None]] = dict(
connect=connect,
Expand Down