-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmqGeneratorFactory.py
190 lines (145 loc) · 4.63 KB
/
zmqGeneratorFactory.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
from nexus2event import *
from neventarray import *
import sys, os, getopt
import errno
import numpy
import time
import threading
import psutil
import zmq
import twisted
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
def usage() :
print ""
print "Usage:"
print "\tpython",sys.argv[0],"[-h -m -r] <nexus file> <port> (<multiplier>)"
print ""
print "-h: this help"
print "-m: use fake data"
print "-r: don't wait for external signal to start send data"
print ""
class generatorSource(LineReceiver):
def __init__(self):
self.status = False
def connectionMade(self):
self.sendLine("Type run to start counter")
self.sendLine("Type pause to stop counter")
def connectionLost(self, reason):
print "Disconnetting controller"
def lineReceived(self, line):
print line
if line == "run" :
self.status = True
self.factory.run()
if line == "pause" :
self.status = False
self.factory.pause()
self.sendLine(str(self.factory.count))
class generatorSourceFactory(Factory):
protocol = generatorSource
def __init__ (self,source,port,multiplier,mock=False,status=False):
self.source = source
self.port = port
self.multiplier = multiplier
self.status = status
self.data = self.load(mock)
self.context = zmq.Context()
# mem = psutil.virtual_memory().available
#
# print self.data.size*event_t.itemsize/(1024.*1024.),"MB/message"
# print mem/(1024.*1024.),"MB"
# prin
self.socket = self.connect()
self.count = 0
if (self.status == True):
self.status = False
self.run()
def load(self,mock):
if not mock:
data = loadNeXus2event(self.source)
else:
data = self.dummy()
if self.multiplier > 1:
data = multiplyNEventArray(data,int(self.multiplier))
print "ready to run"
return data
def dummy(self):
data = np.empty(shape=1,dtype=event_t)
data["ts"]=1234
data["x"] = 23
data["y"] = 4
return data
def connect(self):
zmq_socket = self.context.socket(zmq.PUSH)
zmq_socket.bind("tcp://127.0.0.1:"+self.port)
return zmq_socket
def run(self) :
if (self.status == False) :
print "started counting"
self.status = True
thread = threading.Thread(target=self.start)
thread.daemon = True
thread.start()
else :
print "Nothing to do, I'm already counting!"
def pause(self) :
if (self.status == True) :
print "paused counting"
self.status = False
else :
print "Nothing to do, I'm already in pause!"
def start(self):
data = self.data
ctime=time.time()
pulseID=0
while True:
itime = time.time()
dataHeader=header(pulseID,itime)
def send_data(socket,head):
socket.send_json(head,zmq.SNDMORE)
socket.send(self.data)
self.count += 1
if (self.status == True):
send_data(self.socket,dataHeader)
elapsed = time.time() - itime
remaining = 1./14-elapsed
if remaining > 0:
time.sleep (remaining)
pulseID += 1
if time.time()-ctime > 10 :
size = (data.size*event_t.itemsize+
sys.getsizeof(dataHeader))
print "Sent ",self.count," events @ ",size*self.count/(10.*1e6)," MB/s"
self.count = 0
ctime = time.time()
def main(argv,mock=False,status=False):
source = argv[0]
port = argv[1]
multiplier = 1
if len(argv) > 2:
multiplier = argv[2]
reactor.listenTCP(8123, generatorSourceFactory(source,port,multiplier,mock,status))
reactor.run()
if __name__ == "__main__":
try:
opts,args = getopt.getopt(sys.argv[1:], "hmr",["help","mock","run"])
except getopt.GetoptError as err:
print str(err)
usage()
sys.exit(2)
if len(args) < 2:
usage()
exit(2)
mock = False
run = False
for o,a in opts:
if o in ("-h","--help"):
usage()
sys.exit()
if o in ("-m","--mock"):
mock = True
if o in ("-r","--run"):
run = True
main(args,mock,run)