Search code examples
pythonpyzmqkeyboardinterrupt

No keyboard interrupt with PyZMQ 23.3.0.dev with PYthon 3.10 (windows)


It seems that the keyboard interrupt with python 3.10 and pyzmq 23.3.0 does not work anymore (windows).

I remember it working, but now it does not.

I used the example in https://zguide.zeromq.org/docs/chapter2/#Handling-Interrupt-Signals

import signal
import time
import zmq

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5558")

# SIGINT will normally raise a KeyboardInterrupt, just like any other Python call
try:
    socket.recv()
except KeyboardInterrupt:
    print("W: interrupt received, stopping...")
finally:
    # clean up
    socket.close()
    context.term()

to no avail... I read this solution Stop pyzmq receiver by KeyboardInterrupt and indeed, signal.signal(signal.SIGINT, signal.SIG_DFL); works, but it does not execute the print statement, nor does it run the finally. Is this just me?


Solution

  • Actually, the best solution which is recommended in the documentation is to use green.

    import zmq.green as zmq
    

    https://pyzmq.readthedocs.io/en/latest/api/zmq.green.html