满足条件时终止多处理进程

时间:2019-01-16 20:37:22

标签: python parallel-processing multiprocessing python-multiprocessing

我试图运行的想法是这样的:

RUN 3进行计算的过程 一旦三个过程之一完成任务 立即杀死其他人并继续执行主要任务,我不能让它再运行一秒钟

我尝试过的事情是:  通过multiprocessing.manager放置全局变量,但这仍然可以使进程完成循环。  引发异常

操作系统:Windows PYTHON:2.7

def f(name):
    Doing = True
    try:
        while Doing:
            print 'DOING',name
            somecodethatmarksDoingAsFalse()
    except Exception as error:
        print 'bye'
        print error
        Doing = False
        return True




if __name__ == '__main__':
    p = multiprocessing.Process(target=f, args=('bob',))
    p2 = multiprocessing.Process(target=f, args=('tom',))
    p.start()
    p2.start()

    p.join()
    p2.join()


    raise Exception('I know Python!')
    sys.exit()

当我将操作标记为false时,我希望能够杀死所有进程,引发异常或以任何方式对一个进程进行计算时以任何方式出现

编辑:这不是重复的,因为它仍然完成执行代码,例如,请求模块仍在发送数据

1 个答案:

答案 0 :(得分:1)

如果需要立即终止,则可以使用multiprocessing.Event通知父进程有关已满足的条件,并让其立即终止工作进程。对于这种所需的少量同步,管理器进程将过于繁琐。

import os
from datetime import datetime
from multiprocessing import Process, Event


def worker(range_, target, found_event):
    print('{} | pid: {} started'.format(datetime.now(), os.getpid()))
    for x in range_:
        if x == target:
            print('{} | pid: {} found target'.format(
                datetime.now(), os.getpid())
            )
            found_event.set()


if __name__ == "__main__":

    N_WORKERS = 4

    step = int(200e6)
    ranges = [range(x, x + step) # change `range` to `xrange` for Python 2
              for x in range(0, N_WORKERS * step, step)]
    # range(0, 200000000), ..., range(800000000, 1000000000)]
    target = int(150e6)  # <-- worker finding this value triggers massacre
    found_event = Event()

    pool = [Process(target=worker, args=(range_, target, found_event))
            for range_ in ranges]

    for p in pool:
        p.start()

    found_event.wait()  # <- blocks until condition met
    print('{} | terminating processes'.format(datetime.now()))
    for p in pool:
        p.terminate()
    for p in pool:
        p.join()
    print('{} | all processes joined'.format(datetime.now()))

示例输出:

2019-01-17 01:55:33.781884 | pid: 28376 started
2019-01-17 01:55:33.782333 | pid: 28377 started
2019-01-17 01:55:33.782851 | pid: 28378 started
2019-01-17 01:55:33.783484 | pid: 28379 started
2019-01-17 01:55:54.715425 | pid: 28376 found target
2019-01-17 01:55:54.715613 | terminating processes
2019-01-17 01:55:54.716326 | all processes joined

Process finished with exit code 0