-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultiprocessing-events.py
96 lines (71 loc) · 2.12 KB
/
multiprocessing-events.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import multiprocessing
import time
def wait_for_event(e):
"""Wait for the event to be set before doing anything"""
print('wait_for_event: starting')
e.wait()
print('wait_for_event: e.is_set()->', e.is_set())
def wait_for_event_timeout(e, t):
"""Wait t seconds and then timeout"""
print('wait_for_event_timeout: starting')
e.wait(t)
print('wait_for_event_timeout: e.is_set()->', e.is_set())
if __name__ == '__main__':
e = multiprocessing.Event()
w1 = multiprocessing.Process(name='block',
target=wait_for_event,
args=(e,))
w1.start()
w2 = multiprocessing.Process(name='non-block',
target=wait_for_event_timeout,
args=(e, 2))
w2.start()
print('main: waiting before calling Event.set()')
time.sleep(3)
e.set()
print('main: event is set')
flag1 = multiprocessing.Value('b',True)
import multiprocessing
import time
mng = multiprocessing.Manager()
#global run_closed_loop
global mv
mv = mng.Value('b',True)
global dict1
dict1 = mng.dict()
dict1["right"]=True, # True = correct
dict1["left"]=False # False = wrong
def get_random_side(stim_dict):
random_side = bool(random.getrandbits(1))
stim_dict["right"]=random_side
stim_dict["left"]= not(random_side)
def tester():
time.sleep(5)
global dict1
print()
mp = multiprocessing.Process(target=tester)
mp.start()
for i in range(6):
print(mv.value)
time.sleep(1)
import random
#random.seed(random)
global correct_stim_side
correct_stim_side = {
"right" : True, # True = correct
"left" : False # False = wrong
}
def get_random_side():
global correct_stim_side
random_side = bool(random.getrandbits(1))
correct_stim_side["right"]=random_side
correct_stim_side["left"]= not(random_side)
#stimulus
sides_li = []
for _ in range(10):
print()
random_side = bool(random.getrandbits(1))
correct_stim_side["right"]=random_side
correct_stim_side["left"]= not(random_side)
print(random_side)
sides_li.append(correct_stim_side.copy())