How to terminate a multiprocess in python when a given condition is met? [duplicate]

两盒软妹~` 提交于 2020-01-03 02:24:08

问题


Let's say I have the function:

def f():  
    while True:          
        x = generate_something()  
        if x == condition:  
            return x

if __name__ == '__main__':
    p=Pool(4)

I want to run this function in a multiprocess and when one of the processes meets my function's condition, I want all other processes to stop.


回答1:


You can use event and terminate in multiprocessing since you want to stop all processes once condition is met in one of the child process.

Check the below working example in which I am creating two processes which will check the value of variable x is 5 or not.

Once, one of the process sets the value of x to 5, event is set.

Event is set or unset is continuously checked in main code.

Code:

import random
import multiprocessing
import sys
import time

def generate_something():
    return random.choice(range(10))

def f(event):
    while True:
        x = generate_something()
        print "Value is ",x
        if x == 5:
            print "Got what I am searching for."
            event.set()
        time.sleep(2)

if __name__ == '__main__':

    jobs = []
    #Create Event
    event = multiprocessing.Event()

    #Create two processes
    for i in range(2):
        p = multiprocessing.Process(target=f,args=(event,))
        p.start()
        jobs.append(p)

    #Check whether event is set or not
    #When set close all child processes
    while True:
        if event.is_set():
            print "Exiting all child processess.."
            for i in jobs:
                #Terminate each process
                i.terminate()
            #Terminating main process
            sys.exit(1)
        time.sleep(2)

Output:

"C:\Program Files (x86)\Python27\python.exe" C:/Users/punddin/PycharmProjects/demo/a.py
Value is  4
Value is  2
Value is  7
Value is  6
Value is  3
Value is  4
Value is  8
Value is  3
Value is  8
Value is  1
Value is  9
Value is  9
Value is  1
Value is  6
Value is  5
Got what I am searching for.
Value is  6
Exiting all processess..


来源:https://stackoverflow.com/questions/47903791/how-to-terminate-a-multiprocess-in-python-when-a-given-condition-is-met

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