满足条件时终止多处理进程 [英] Killing a multiprocessing process when condition is met

查看:64
本文介绍了满足条件时终止多处理进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The idea im trying to run is this:

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

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

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

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

操作系统:Windows蟒蛇:2.7

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

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

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:它不是重复的,因为它仍然执行完代码,例如Requests模块仍然发送数据

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

推荐答案

如果你需要即时杀戮,你可以使用 multiprocessing.Event 通知父进程满足条件并让它立即杀死工作进程.管理器进程对于这种需要的少量同步来说太重了.

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

示例输出:

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

这篇关于满足条件时终止多处理进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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