python - multiprocessing.Event.wait hangs when interrupted by a signal -
i use following code handle sigint event. code sets multiprocessing.event "wakeup" main thread waiting.
import multiprocessing import signal class class1(object): _stop_event = multiprocessing.event() @staticmethod def interrupt(): class1._stop_event.set() def wait(self): print("waiting _stop_event") if class1._stop_event.wait(5): print("_stop_event set.") else: print("timed out.") def stop(signum, frame): print("received sig") class1.interrupt() signal.signal(signal.sigint, stop) c = class1() c.wait()
without signal, wait method times out after 10 seconds , process exits following output expected:
waiting _stop_event timed out.
when sending sigint signals, signal gets handled event.wait method not return, neither nor after timeout. process never exits. output is:
waiting _stop_event ^creceived sig
i can continue send sigints. process won't exit , output is:
waiting _stop_event ^creceived sig ^creceived sig ^creceived sig ^creceived sig ....
everything works expected if replace class1.wait method check event.is_set:
def wait(self): print("waiting _stop_event") while true: if class1._stop_event.is_set(): print("_stop_event set.") break
the process exits , output is:
waiting _stop_event ^creceived sig _stop_event set.
how make event.wait return once event set? reason wait method doesn't timeout anymore?
you guys going one. use threading.event
, not multiprocessing.event
. when press ^c
signal handler called should!
source
import threading import signal class class1(object): _stop_event = threading.event() @staticmethod def interrupt(): class1._stop_event.set() def wait(self): print("waiting _stop_event") if class1._stop_event.wait(5): print("_stop_event set.") else: print("timed out.") def stop(signum, frame): print("received sig") class1.interrupt() signal.signal(signal.sigint, stop) c = class1() c.wait()
output
waiting _stop_event ^creceived sig _stop_event set.
Comments
Post a Comment