-
Notifications
You must be signed in to change notification settings - Fork 1
/
Handler.py
164 lines (128 loc) · 5.6 KB
/
Handler.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
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 20 17:50:47 2012
@author: rafik
"""
import multiprocessing as mproc
import heapq
import time
class InputHandler(mproc.Process):
"""handels the input plugins, feeds the pipes
will be spawn once
"""
def __init__(self, datapipes, readers):
mproc.Process.__init__(self)
print '%s: init'%self.name
self.datapipes = datapipes
self.readers = readers
def run(self):
print '%s: run @t: %f' % (self.name, time.time())
self.readers[0].setup('bin/testfile.mpg')
#time.sleep(5)
for i in testrange:
print '---------------------------'
print '%s: get dataframe %.0f @t: %f' % (self.name, i, time.time())
data = self.readers[0].getData()
#time.sleep(3)
print '%s: send dataframe %.0f to %.0f @t: %f' % (self.name, i, i%2, time.time())
self.datapipes[i%2].send(data)
#time.sleep(1)
#we're finished, sending signals to workers to close connection
#time.sleep(10)
print '%s: finished, tell workers to shut down @t: %f' % (self.name, time.time())
for pipe in self.datapipes:
pipe.send(-1)
print '%s: stopping @t: %f' % (self.name, time.time())
class WorkerHandler(mproc.Process):
"""handels the processing of the data
can possibly be spawn multiple times (depening on no of cpu cores)
"""
def __init__(self, datapipe, result_queue, workers):
mproc.Process.__init__(self)
print '%s: init'%self.name
self.datapipe = datapipe
self.result_queue = result_queue
self.workers = workers
def setup(self, *args):
print '%s: setup @t: %f' % (self.name, time.time())
for i, worker in enumerate(self.workers):
worker.setup(args[0])
def run(self):
print '%s: run @t: %f' % (self.name, time.time())
i=0
#for i in testrange:
while True:
i=+1
print '%s: waiting for data %.0f @t: %f' % (self.name, i, time.time())
data = self.datapipe.recv() #waits till it gets something
print '%s: got data @t: %f' % (self.name, time.time())
if data == -1: break
data = self.workers[0].procData(data)
print '%s: processed data, put in queue @t: %f' % (self.name, time.time())
#print time.clock()
self.result_queue.put(data)
#time.sleep(5)
self.result_queue.put([-1])
print '%s: stopping @t: %f' % (self.name, time.time())
# def __getstate__(self):
# return 'bla'
#
# def __setstate__(self, str):
# pass
class OutputHandler(mproc.Process):
"""handels the output (display, saving) of the data
will be spawn once
"""
def __init__(self, result_queue, writers):
mproc.Process.__init__(self)
print '%s: init'%self.name
self.result_queue = result_queue
self.writers = writers
#self.writers[0].init()
self.nrunningworkers = 2 #TODO: get this from super
def run(self):
print '%s: run @t: %f' % (self.name, time.time())
self.writers[0].setup()
#datacounter = 0
framecounter = 1
buffer = [] #list used as heap queue (priority queue)
#heapq.heappush(buffer, (10000000, [])) #TODO this is only a workaround to prevent in "while buffer[0][0]..." invalid access (there is always at least one bigger than all others in the pipe)
#for i in testrange:
while True:
#time.sleep(0.2)
print '%s: waiting for data @t: %f' % (self.name, time.time())
data = self.result_queue.get()
#check if quit
#TODO: maybe do this better later, check if all worker not living (using signals) and queue empty
if data[0] == -1:
self.nrunningworkers -= 1
if self.nrunningworkers == 0:
break
continue
#time.sleep(0.5)
print '%s: got dataframe nr %.0f from queue @t: %f' % (self.name, data[0], time.time())
# for writer in self.writers:
# writer.writeData(data)
if data[0] == framecounter:
print '%s: on time, write it @t: %f' % (self.name, time.time())
framecounter += 1
for writer in self.writers:
writer.writeData(data)
else:
print '%s: ahead of time, write it to buffer @t: %f' % (self.name, time.time())
#print 'data1', data
heapq.heappush(buffer, (data[0], data)) #makes already sure the elements in heapq are sorted!!
print ' len buffer:', len(buffer), buffer[0][0], framecounter, data[0]
while (len(buffer) > 0 and (buffer[0][0] == framecounter or data[0] >= framecounter+25)): # as soon as right frame is here OR more than 1 sec (25 frames) behind, continue painting
_, data = heapq.heappop(buffer)
#print 'data2', data
framecounter += 1
for writer in self.writers:
writer.writeData(data)
pass
print '%s: stopping @t: %f' % (self.name, time.time())
# def __getstate__(self):
# return 'bla'
#
# def __setstate__(self, str):
# pass