用SIGKILL而不是SIGTERM杀死一个多处理池(我认为) [英] Kill a multiprocessing pool with SIGKILL instead of SIGTERM (I think)

查看:201
本文介绍了用SIGKILL而不是SIGTERM杀死一个多处理池(我认为)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个程序,它利用多个硒浏览器窗口进行多处理.

So, I have this program that utilizes multiprocessing with multiple selenium browser windows.

程序如下所示:

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

但是,这将等待pool中的现有过程完成.我要立即解雇所有工人.

However, this waits for the existing process in pool to complete. I want instant termination of all the workers.

推荐答案

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

multiprocessing.Pool store worker processes list in Pool._pool attr, send a signal to them is straightforward then:

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()

这篇关于用SIGKILL而不是SIGTERM杀死一个多处理池(我认为)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆