Keyboard Interrupts with python#39;s multiprocessing Pool(python的多处理池的键盘中断)
问题描述
如何使用 python 的多处理池处理 KeyboardInterrupt 事件?这是一个简单的例子:
How can I handle KeyboardInterrupt events with python's multiprocessing Pools? Here is a simple example:
from multiprocessing import Pool
from time import sleep
from sys import exit
def slowly_square(i):
sleep(1)
return i*i
def go():
pool = Pool(8)
try:
results = pool.map(slowly_square, range(40))
except KeyboardInterrupt:
# **** THIS PART NEVER EXECUTES. ****
pool.terminate()
print "You cancelled the program!"
sys.exit(1)
print "
Finally, here are the results: ", results
if __name__ == "__main__":
go()
在运行上面的代码时,当我按下 ^C
时,KeyboardInterrupt
会被引发,但该进程会在此时挂起,我必须在外部将其杀死.
When running the code above, the KeyboardInterrupt
gets raised when I press ^C
, but the process simply hangs at that point and I have to kill it externally.
我希望能够随时按 ^C
并让所有进程正常退出.
I want to be able to press ^C
at any time and cause all of the processes to exit gracefully.
推荐答案
这是一个 Python 错误.在 threading.Condition.wait() 中等待条件时,永远不会发送 KeyboardInterrupt.再现:
This is a Python bug. When waiting for a condition in threading.Condition.wait(), KeyboardInterrupt is never sent. Repro:
import threading
cond = threading.Condition(threading.Lock())
cond.acquire()
cond.wait(None)
print "done"
KeyboardInterrupt 异常在 wait() 返回之前不会被传递,并且它永远不会返回,因此中断永远不会发生.KeyboardInterrupt 几乎肯定会中断条件等待.
The KeyboardInterrupt exception won't be delivered until wait() returns, and it never returns, so the interrupt never happens. KeyboardInterrupt should almost certainly interrupt a condition wait.
请注意,如果指定了超时,则不会发生这种情况;cond.wait(1) 将立即收到中断.因此,一种解决方法是指定超时.为此,请替换
Note that this doesn't happen if a timeout is specified; cond.wait(1) will receive the interrupt immediately. So, a workaround is to specify a timeout. To do that, replace
results = pool.map(slowly_square, range(40))
与
results = pool.map_async(slowly_square, range(40)).get(9999999)
或类似的.
这篇关于python的多处理池的键盘中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:python的多处理池的键盘中断
基础教程推荐
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01