Killing a multiprocessing process when condition is met

别说谁变了你拦得住时间么 提交于 2021-01-28 00:59:26

问题


The idea im trying to run is this:

RUN 3 Processes doing calculation ONCE one of the 3 processes finishes the task KILL others imediatly and continue with the main task, i can't let it run any second longer

The things i've tried was: Putting the global variable through multiprocessing.manager, but that still lets processes finish their loops. Raising an exception

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

I expect to be able to kill all processes when i mark doing as false, raise an exception or any way when calculation is done on ONE of the processes

EDIT: It's not a duplicate, because it still finishes executing code, for example Requests module still sends data


回答1:


If you need instant kills, you can use a multiprocessing.Event to inform the parent-process about the condition met and let it kill the worker processes immediately. A manager-process would be too heavyweight for this little synchronization needed.

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

Example Output:

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


来源:https://stackoverflow.com/questions/54224989/killing-a-multiprocessing-process-when-condition-is-met

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!