使用SIGKILL而不是SIGTERM杀死多处理池(我认为)

时间:2017-11-29 12:39:50

标签: python selenium unix multiprocessing

所以,我有这个程序,利用多个selenium浏览器窗口进行多处理。

这是程序的样子:

pool = Pool(5)
results = pool.map_async(worker,range(10))
time.sleep(10)
pool.terminate()

但是,这会等待池中的现有进程完成。我想立即终止所有工人。

1 个答案:

答案 0 :(得分:4)

multiprocessing.PoolPool._pool attr中存储工作进程列表,向他们发送信号很简单:

import multiprocessing
import os
import signal


def kill(pool):
    # stop repopulating new child
    pool._state = multiprocessing.pool.TERMINATE
    pool._worker_handler._state = multiprocessing.pool.TERMINATE
    for p in pool._pool:
        os.kill(p.pid, signal.SIGKILL)
    # .is_alive() will reap dead process
    while any(p.is_alive() for p in pool._pool):
        pass
    pool.terminate()