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)

或类似的.

相关文章