Skip to content

gh-112020: socketserver: Add an example for keeping the connection open #112054

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
wants to merge 8 commits into from

Conversation

talcs
Copy link
Contributor

@talcs talcs commented Nov 14, 2023

documentation - an example where TCP handler can handle many requests over the same connection, which is an undocumented important use case,


📚 Documentation preview 📚: https://cpython-previews--112054.org.readthedocs.build/

documentation - an example where TCP handler can handle many requests over the same connection,
@ghost
Copy link

ghost commented Nov 14, 2023

All commit authors signed the Contributor License Agreement.
CLA signed

@AA-Turner AA-Turner changed the title gh-112020: Update socketserver.rst with a new code example gh-112020: socketserver: Add an example for keeping the connection open Jan 12, 2024
Copy link
Member

@AA-Turner AA-Turner left a comment

Choose a reason for hiding this comment

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

This seems reasonable but socketserver isn't my area of expertise.

A

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

I am not sure that a new example is needed. And it may be not enough, taking into account that the client code only calls a single sendall(), and that there are no examples for repeated readline() or for UDP sockets.

We can simply add that recv() and readline() can be called repeatedly, until tehy return an empty bytes object.

@@ -528,6 +528,24 @@ The difference is that the ``readline()`` call in the second handler will call
single ``recv()`` call in the first handler will just return what has been sent
Copy link
Member

Choose a reason for hiding this comment

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

In the line above add "or ``recv()`` returns an empty bytes object" after "until it encounters a newline character"

@talcs
Copy link
Contributor Author

talcs commented Feb 20, 2024

Hi,

Please check my new commit. It uses while loops within the existing handler examples, i.e., allows handling multiple requests under the same connection.

Handling a single request is a special case of handling multiple requests, i.e., while supporting multiple requests, you also support a single request. This also means that the existing user examples are still compatible with the server examples.

Copy link
Contributor Author

@talcs talcs left a comment

Choose a reason for hiding this comment

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

Guys, please check this change. Here the existing handle methods were extended to support one or more requests under the same connection. I think it looks elegant and clear now.

@hugovk hugovk removed the needs backport to 3.11 only security fixes label Apr 11, 2024
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.13 bugs and security fixes label May 9, 2024
@gpshead
Copy link
Member

gpshead commented Feb 6, 2025

closing in favor of #129741 which modernizes the examples.

we don't need to expand these further, showing handling multiple "requests" per handle call is not needed as that isn't the point of these examples which are merely about demonstrating handle being invoked per connection and how to interact with the socket from there.

@gpshead gpshead closed this Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir needs backport to 3.12 only security fixes needs backport to 3.13 bugs and security fixes skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

5 participants