-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathheart_rate_server.py
77 lines (58 loc) · 2.03 KB
/
heart_rate_server.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
import sys
import zmq
import threading
opencv_path = sys.argv[1]
print "Importing HeartRateGenerator and HeartRateImposer"
from data_analysis.heart_rate_generator import HeartRateGenerator
from server.heart_rate_imposer import HeartRateImposer
print "Imported HeartRateGenerator and HeartRateImposer"
class ServerTask(threading.Thread):
"""ServerTask"""
def __init__(self):
threading.Thread.__init__ (self)
def run(self):
context = zmq.Context()
frontend = context.socket(zmq.ROUTER)
backend = context.socket(zmq.DEALER)
frontend.bind('tcp://*:5555')
backend.bind('inproc://backend')
workers = []
for i in range(10):
worker = ServerWorker(context)
worker.start()
workers.append(worker)
zmq.proxy(frontend, backend)
frontend.close()
backend.close()
context.term()
class ServerWorker(threading.Thread):
"""ServerWorker"""
def __init__(self, context):
threading.Thread.__init__ (self)
self.context = context
def run(self):
worker = self.context.socket(zmq.DEALER)
worker.connect('inproc://backend')
print 'Worker started'
while True:
ident, msg = worker.recv_multipart()
print 'Worker received ' + msg + ' from ' + ident
[video_file, age, gender] = msg.split()
if age == 'None':
age = None
if gender == 'None':
gender = None
hri = HeartRateImposer(video_file, opencv_path)
# Send data generated by heart rate imposer
for data in hri.gen_heartrate_frames(age, gender):
worker.send_multipart([ident, data])
# Tell the client we are done sending the files
worker.send_multipart([ident, 'KILL'])
worker.close()
def main():
"""main function"""
server = ServerTask()
server.start()
if __name__ == "__main__":
print "Starting heart rate deamon server"
main()