You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a basic client-server example where the server code is shown below. The code works fine on macOS and Linux but on Windows the KeyboardInterrupt is not captured when I press Control+C on the keyboard.
importzmqdefmain():
"""Run server."""context=zmq.Context()
socket=context.socket(zmq.REP)
socket.bind("tcp://*:5555")
print("Server is running...")
try:
whileTrue:
# Wait for message from clientmessage=socket.recv_string()
print(f"\nReceived message: {message}")
# Send reply back to clientsocket.send_string("there")
print("Sent message: there")
exceptKeyboardInterrupt:
socket.close()
context.term()
print("\nControl+C pressed. Socket closed. Context terminated.")
if__name__=="__main__":
main()
To capture Control+C on Windows, I used the allow_interrupt context manager as shown below. This allows me to use Control+C on Windows to quit the server but the stop_server function gets called twice: once by the allow_interrupt and again by the KeyboardInterrupt exception. On macOS and Linux, the stop_server function is called once.
importzmqfromzmq.utils.win32importallow_interruptdefmain():
"""Run server."""context=zmq.Context()
socket=context.socket(zmq.REP)
socket.bind("tcp://*:5555")
print("Server is running...")
defstop_server():
socket.close()
context.term()
print("\nControl+C pressed. Socket closed. Context terminated.")
try:
whileTrue:
withallow_interrupt(stop_server):
# Wait for message from clientmessage=socket.recv_string()
print(f"\nReceived message: {message}")
# Send reply back to clientsocket.send_string("there")
print("Sent message: there")
exceptKeyboardInterrupt:
stop_server()
if__name__=="__main__":
main()
Is there a way to close the server with Control+C without calling the stop_server function twice on Windows?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I have a basic client-server example where the server code is shown below. The code works fine on macOS and Linux but on Windows the
KeyboardInterruptis not captured when I press Control+C on the keyboard.To capture Control+C on Windows, I used the
allow_interruptcontext manager as shown below. This allows me to use Control+C on Windows to quit the server but thestop_serverfunction gets called twice: once by theallow_interruptand again by theKeyboardInterruptexception. On macOS and Linux, thestop_serverfunction is called once.Is there a way to close the server with Control+C without calling the
stop_serverfunction twice on Windows?Beta Was this translation helpful? Give feedback.
All reactions