-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_wstop.py
46 lines (36 loc) · 958 Bytes
/
multi_wstop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#%%
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(0.5)
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)
# %%