-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
socketserver context manager #70592
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
Comments
As Martin commented on my patch at bpo-26392 the socketserver.server_close is like the file close. That made me think that we should add a context manager to the socketserver. |
I would support this change, as long as we aren’t supporting using server_close() to stop the server loop at the same time. It might be worth updating other example code that uses server_close(), in particular in the xmlrpc.server documentation. |
Only closing the server :).
|
Thanks, the patch looks pretty good to me. I left some comments about minor things. Also, someone will need to write a What’s New entry. |
Updated the patch:
|
updated the patch according to the CR comments. |
This seems like an appropriate enhancement. I notice that the serve_forever examples did not previously have server_close(). Was this an oversight or will the server_close in __exit__ just be a no-op? |
The examples did not close the server but I am not sure if that was a mistake. The server is not being closed only on examples where it is expected to run until there is a keyboard interrupt. However you can see that when you send keyboard interrupt to a server you start using python -m http.server you will do close the server. |
Server_close() was only documentated last year; see bpo-23254. For the examples that run until you interrupt them, the servers currently emit a resource warning (in addition to the KeyboardInterrupt traceback and the Python 2 bytes warnings): $ python -bWall TCPServer.py
127.0.0.1 wrote:
TCPServer.py:16: BytesWarning: str() on a bytes instance
print(self.data)
b'hello world with TCP'
127.0.0.1 wrote:
TCPServer.py:16: BytesWarning: str() on a bytes instance
print(self.data)
b'python is nice'
^CTraceback (most recent call last):
File "TCPServer.py", line 28, in <module>
server.serve_forever()
File "/usr/lib/python3.5/socketserver.py", line 237, in serve_forever
ready = selector.select(poll_interval)
File "/usr/lib/python3.5/selectors.py", line 367, in select
fd_event_list = self._poll.poll(timeout)
KeyboardInterrupt
sys:1: ResourceWarning: unclosed <socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 9999)>
[Exit 1] If you ignore the warning, there isn’t much effective difference, because the socket gets closed when the process exits, or if you are lucky, when Python garbage collects the global “server” object. But IMO it is bad practice not to clean up resources properly, especially in an API example. |
I agree and consider this is an argument for adding the context manager methods and using them with 'with' in the examples. |
New changeset 5c4303c46a18 by Martin Panter in branch 'default': |
Thanks for the patch Aviv. I made a few minor English grammar etc tweaks in the version I committed, as pointed out in the review comments. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: