python multiprocess.Pool无法处理KeyboardInterrupt吗?

时间:2016-01-28 04:52:29

标签: python parallel-processing pool keyboardinterrupt

我想在按Ctrl-C时终止我的程序,代码如下:

#!/usr/bin/env python
# encoding: utf-8

import multiprocessing
import time
import signal
import sys

def init_worker():
    signal.signal(signal.SIGINT, signal.SIG_IGN)

def worker():
    while(True):
        time.sleep(1.1234)
        print "Working..."

if __name__ == "__main__":
    pool = multiprocessing.Pool(50, init_worker)
    try:
        for i in range(50):
            pool.apply_async(worker)

        # time.sleep(10)
        pool.close()
        pool.join()

    except KeyboardInterrupt:
        print "Caught KeyboardInterrupt, terminating workers"
        pool.terminate()
        pool.join()

但这不能正常工作

1 个答案:

答案 0 :(得分:0)

您可以尝试这种方式:

done