-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel_readproc.py
193 lines (169 loc) · 6.57 KB
/
parallel_readproc.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import multiprocessing as mp
import numpy as n
import time
def scheme1():
""" Uses two threads optionally called in while-1 loop.
Works!
"""
def gendata(d, l):
""" Data generating stage of parallel data function.
data0 is where data is held to be processed.
if reading gets ahead, data1 is used for next chunk.
"""
name = mp.current_process().name
n.random.seed()
ts = n.random.random()
print 'gendata %s needs %.1f seconds' % (name, ts)
time.sleep(ts)
with l:
kk = d.keys()
if 'data0' not in kk: # if data0 empty, fill it in one of two ways
if 'data1' not in kk:
d['data0'] = n.random.random(10)
print 'filling new data0'
else:
d['data0'] = d.pop('data1')
print 'moving to data0'
elif 'data1' not in kk: # once data0 is ok, then consider filling data1
d['data1'] = n.random.random(10)
print 'filling new data1'
def processdata(d, l):
""" Processing stage of parallel data function.
Only processes from data0. Uses a "first in, first out" model, where data0 is next in line.
"""
name = mp.current_process().name
n.random.seed()
ts = n.random.random()
print 'processdata %s needs %.1f seconds' % (name, ts)
time.sleep(ts)
kk = d.keys()
with l:
if 'data0' in kk:
d['mean'] = d.pop('data0').mean()
def start():
""" Threading for parallel data reading and processing.
Either side can be faster than the other, since data are held for processing in shared buffer.
"""
mgr = mp.Manager()
d = mgr.dict()
l = mp.Lock()
pgenlist = []; pproclist = []
try:
while 1:
kk = d.keys()
# print kk, len(pgenlist), len(pproclist)
if ((('data0' not in kk) or ('data1' not in kk)) and (len(pgenlist) <= 1)):
pgen = mp.Process(target=gendata, args=(d,l))
pgen.start()
pgenlist.append(pgen)
if (('data0' in kk) and (len(pproclist) == 0)):
pproc = mp.Process(target=processdata, args=(d,l))
pproc.start()
pproclist.append(pproc)
# process cleanup
for proc in pgenlist:
# print 'checking proc', proc, proc.is_alive()
if not proc.is_alive():
# print 'joining proc', proc
proc.join()
pgenlist.pop(pgenlist.index(proc))
for proc in pproclist:
# print 'checking proc', proc, proc.is_alive()
if not proc.is_alive():
# print 'joining proc', proc
proc.join()
pproclist.pop(pproclist.index(proc))
print 'mean = ', d['mean']
except KeyboardInterrupt:
print 'Ctrl-C received. Shutting down threads...'
for pgen in pgenlist:
pgen.terminate()
pgen.join()
for pproc in pproclist:
pproc.terminate()
pproc.join()
def scheme2():
""" Tries to mimic reading data with an iterator triggered from outside, a la msiter.
"""
def gendata(d, l, e):
""" Data generating stage of parallel data function.
data0 is where data is held to be processed.
if reading gets ahead, data1 is used for next chunk.
reading triggered by event
"""
data = iter(range(10))
while 1:
e.wait()
try:
name = mp.current_process().name
n.random.seed()
ts = n.random.random()
ts = 0.2
print 'gendata %s needs %.1f seconds' % (name, ts)
time.sleep(ts)
with l:
kk = d.keys()
if 'data0' not in kk: # if data1 empty, fill it
if 'data1' not in kk:
d['data0'] = data.next()
print 'filling new data0'
else:
d['data0'] = d.pop('data1')
print 'moving to data0'
elif ('data1' not in kk): # if data0 is ok, fill from data1, if filled
d['data1'] = data.next()
print 'filling new data1'
except StopIteration:
print 'End of iteration'
break
e.clear()
def processdata(d, l, e):
""" Processing stage of parallel data function.
Only processes from data0. Uses a "first in, first out" model, where data0 is next in line.
Needs event signal to process.
"""
while 1:
e.wait()
name = mp.current_process().name
n.random.seed()
ts = n.random.random()
ts = 0.1
print 'processdata %s needs %.1f seconds' % (name, ts)
time.sleep(ts)
kk = d.keys()
with l:
if 'data0' in kk:
d['mean'] = float(d.pop('data0'))
e.clear()
print 'mean = ', d['mean']
def start():
""" Threading for parallel data reading and processing.
Either side can be faster than the other, since data are held for processing in shared buffer.
"""
mgr = mp.Manager()
d = mgr.dict()
l = mp.Lock()
egen = mp.Event()
eproc = mp.Event()
try:
# start processes
pgen = mp.Process(target=gendata, args=(d,l,egen))
pgen.start()
pproc = mp.Process(target=processdata, args=(d,l,eproc))
pproc.start()
# loop monitors data areas to determine whether to trigger more reading or processing
while 1:
kk = d.keys()
if ('data0' not in kk) or ('data1' not in kk):
egen.set()
if 'data0' in kk:
eproc.set()
except KeyboardInterrupt:
print 'Ctrl-C received.'
finally:
print 'Shutting down threads...'
pgen.terminate()
pgen.join()
pproc.terminate()
pproc.join()
start()