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
andterminate
inmultiprocessing
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
to5
, event is set.Event
isset
orunset
is continuously checked inmain
code.Code:
Output: