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

假如想象 提交于 2019-12-08 19:45:33

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