-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
gh-79012: Add asyncio chat server HOWTO #144604
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?
Conversation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Doc/howto/asyncio-chat-server.rst
Outdated
| Common pitfalls | ||
| =============== |
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.
This section isn't really about the chat server. These are just common asyncio traps that would fit better in the tutorial or the reference docs.
- Explain concepts (start_server, StreamReader/StreamWriter) before code - Use asyncio.TaskGroup for concurrent broadcasting - Use contextlib.suppress instead of bare except/pass - Remove Python test client, keep only nc/telnet - Properly explain asyncio.timeout before showing usage - Move implementation notes to code comments - Remove Exercises and Common pitfalls sections - Reorder seealso links in asyncio.rst Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
I have made the requested changes; please review again |
|
Thanks for making the requested changes! : please review the changes made to this pull request. |
Doc/howto/asyncio-chat-server.rst
Outdated
| The :meth:`~asyncio.StreamWriter.write` method buffers data without sending | ||
| it immediately. Awaiting :meth:`~asyncio.StreamWriter.drain` flushes the | ||
| buffer and applies back-pressure if the client is slow to read. Similarly, | ||
| :meth:`~asyncio.StreamWriter.close` initiates shutdown, and awaiting | ||
| :meth:`~asyncio.StreamWriter.wait_closed` waits until the connection is | ||
| fully closed. |
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.
This should go above the example.
Doc/howto/asyncio-chat-server.rst
Outdated
| :: | ||
|
|
||
| import asyncio | ||
| import contextlib | ||
|
|
||
| connected_clients: dict[str, asyncio.StreamWriter] = {} | ||
|
|
||
| async def broadcast(message, *, sender=None): | ||
| """Send a message to all connected clients except the sender.""" | ||
| async def send(writer): | ||
| with contextlib.suppress(ConnectionError): | ||
| writer.write(message.encode()) | ||
| await writer.drain() | ||
|
|
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.
This code is really large. We should introduce small concepts and explain things, and then finally show the complete example at the end.
| async with server: | ||
| await server.serve_forever() |
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.
This isn't explained.
| writer.close() | ||
| await writer.wait_closed() |
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.
This needs to be explained too.
- Move write/drain and close/wait_closed explanations above the echo server example - Explain async with server, serve_forever, and asyncio.run - Break chat server into subsections: client tracking, broadcasting, then the complete example - Show broadcast function separately before the full listing Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
I have made the requested changes; please review again |
|
Thanks for making the requested changes! : please review the changes made to this pull request. |
Summary
Adds a focused HOWTO for building a TCP chat server with asyncio streams, as suggested by @ZeroIntensity in the review of #144594:
The guide progressively builds from an echo server to a full chat server:
start_server,StreamReader/StreamWriter, write/drain, close/wait_closed)asyncio.timeout, plus exercisesNo overlap with the existing Conceptual Overview — this is purely a practical, hands-on HOWTO.
Test plan
make -C Doc checkpassesmake -C Doc htmlpasses (no warnings)🤖 Generated with Claude Code
📚 Documentation preview 📚: https://cpython-previews--144604.org.readthedocs.build/