c = threading.Condition()
try:
c.acquire()
c.wait() # You won't be able to interrupt this
except KeyboardInterrupt:
print("Caught it")
c = threading.Condition()
try:
c.acquire()
c.wait(timeout=100) # You CAN interrupt this
except KeyboardInterrupt:
print("Caught it")
因此,要使您的map调用可中断,请执行以下操作:
if __name__ == "__main__":
p = multiprocessing.Pool()
try:
p.map_async(func, iterable).get(timeout=10000000)
except KeyboardInterrupt:
print("Caught it")
# Optionally try to gracefully shut down the worker processes here.
p.close()
# DON'T join the pool. You'll end up hanging.
不幸的是,在Python2.x中并没有一个很好的解决方案。我知道最好的解决方法是使用
pool.map_async(...).get(timeout=<large number>)
,而不是pool.map
。问题是pool.map
调用threading.Condition.wait()
,这由于某种原因不能被Python 2.x中的Ctrl+C中断(它在Python 3中工作)。当您使用map_async()
时,它调用threading.Condition.wait(timeout=<large number>)
,最后会执行一个繁忙的等待循环,这个循环可以被Ctrl+C中断你自己试试吧:
因此,要使您的
map
调用可中断,请执行以下操作:另外请注意,正如phihag所指出的,这个问题在Python 3.4中得到了修复(可能在3.x中更早的版本)。
有几种方法。第一种方法是使用
在穿线方面
在多处理中
所有标记为守护进程的线程都将与主线程一起终止。这不是正确的方法,因为它不允许线程清理
下一种方法是使用try catch监听
KeyboardInterrupt
,然后.join()这样的线程。如果线程处于循环中,则可以使用布尔值等条件,将其设置为false,当条件为false时,它将执行清理。
SIGQUIT(Ctrl+\)将杀死所有进程,即使在Python 2.x下也是如此
您还可以更新到Python3.x,在这里,这种行为(只有子级获得信号)似乎已经被修复。
相关问题 更多 >
编程相关推荐