我生成了几个multiprocessing.Process
es来将工作负载分配到不同的进程上。现在,我希望能够通过按Ctrl来关闭整个程序,但是信号只发送给childs,而不是发送到父进程。
我的程序是这样的:
import sys
import multiprocessing
import signal
# ignoring handler, signal.SIG_IGN is not working on windows
def sighandler(signal, frame):
pass
# worker thread definiton
def worker():
signal.signal(signal.SIGINT, sighandler)
signal.signal(signal.SIGTERM, sighandler)
# do stuff here...
def main():
num_procs = 4
procs = []
print("Starting up %d processes:" %num_procs)
for i in range(num_procs):
print("\tstarting worker %d..." %(i+1))
p = multiprocessing.Process(target=worker)
procs.append(p)
p.start()
print("All processes started...")
try:
for p in procs:
p.join()
except KeyboardInterrupt as e:
print "STOPPING"
for p in procs:
p.terminate()
sys.exit(0)
但是如果我按ctrl键,只会叫4次。当我在父进程中注册信号时,处理程序既不会被调用,KeyboardInterrupt异常也不会工作。我能从里面阻止孩子的进程吗?处理程序中的sys.exit(0)
不适合我。
发布于 2014-01-14 05:53:30
你试过使用os._exit(n)
吗?
从医生那里:
退出状态为n的进程,而不调用清理处理程序、刷新stdio缓冲区等。 可用性: Unix,Windows。 注意,退出的标准方式是sys.exit(n)。通常,_exit()只应在子进程中使用,而不应在叉()之后使用。
https://stackoverflow.com/questions/21115028
复制相似问题