0

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?

will.mendil
  • 752
  • 2
  • 6
  • 21

2 Answers2

2

on possible answer apparently is to fit the recv with the allow_interrupt method. no very elegant but if this is the only solution

import signal
import time
import zmq
import zmq.utils.win32

def stop_my_application():
    print("W: interrupt received, stopping...")
    socket.close()
    context.term()

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

# SIGINT will normally raise a KeyboardInterrupt, just like any other Python call
with zmq.utils.win32.allow_interrupt(stop_my_application):
    socket.recv()
will.mendil
  • 752
  • 2
  • 6
  • 21
0

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

will.mendil
  • 752
  • 2
  • 6
  • 21