所以,我有这个程序,利用多个selenium浏览器窗口进行多处理。
这是程序的样子:
pool = Pool(5)
results = pool.map_async(worker,range(10))
time.sleep(10)
pool.terminate()
但是,这会等待池中的现有进程完成。我想立即终止所有工人。
答案 0 :(得分:4)
multiprocessing.Pool
在Pool._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()