This question already has answers here:
Closed 2 years ago.
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.
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..