-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·225 lines (169 loc) · 6.52 KB
/
main.py
File metadata and controls
executable file
·225 lines (169 loc) · 6.52 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3
import urllib.parse
import sys
import json
import signal
import time
import multiprocessing
import selectors
import socket
import ctypes
import queue
class Data:
def __init__(self, A1: int, A2: int, A3: int):
self.A1_sum = A1
self.A2_max = A2
self.A3_min = A3
def accumulate(self, data: 'Data'):
self.A1_sum += data.A1_sum
self.A2_max = max(self.A2_max, data.A2_max)
self.A3_min = min(self.A3_min, data.A3_min)
class ServerData(Data):
def __init__(self, worker_data: Data):
self.__dict__ = worker_data.__dict__.copy()
self.timestamp = int(time.time())
self.count_type = 10
class Process(multiprocessing.Process):
def __init__(self) -> None:
super().__init__(target=self._run)
self.__is_running = multiprocessing.Value(ctypes.c_bool)
self._timeout = 1
def start(self):
self.__is_running.value = True
super().start()
def stop(self):
self.__is_running.value = False
super().join(self._timeout)
def is_running(self) -> bool:
return self.__is_running.value
def _run(self):
pass
class MessageHandler(Process):
def __init__(self, messages) -> None:
super().__init__()
self.__messages = messages
self.__statistics = multiprocessing.Queue()
self.__flag = multiprocessing.Event()
def _run(self):
print(multiprocessing.current_process().ident, "handler process begin")
data = None
while self.is_running():
try:
# print(multiprocessing.current_process().ident, "handler wait message ...")
try:
message = self.__messages.get(timeout=1)
except queue.Empty:
message = None
pass
# print(multiprocessing.current_process().ident, "handler wait message OK,", message)
if message is not None:
try:
json_data = json.loads(message)
json_data = Data(int(json_data["A1"]), int(json_data["A2"]), int(json_data["A3"]))
if data is None:
data = json_data
# print(multiprocessing.current_process().ident, "new data")
else:
data.accumulate(json_data)
# print(multiprocessing.current_process().ident, "acc data")
except Exception as e:
print(multiprocessing.current_process().ident, e)
# print(multiprocessing.current_process().ident, "handler flag :", self.__flag.is_set())
if self.__flag.is_set():
print(multiprocessing.current_process().ident, "dump data", data)
self.__statistics.put_nowait(data)
data = None
self.__flag.clear()
except Exception as e:
print(multiprocessing.current_process().ident, e)
break
except KeyboardInterrupt:
pass
print(multiprocessing.current_process().ident, "handler process end")
def get_statistics(self):
if not self.is_running():
return None
# print("get statistics ...")
try:
self.__flag.set()
s = self.__statistics.get(self._timeout)
# print("get statistics OK, ", s)
return s
except queue.Empty:
# print("get statistics OK, timeout")
return None
except Exception as e:
print("get statistics error:", e)
class UDPSocketAcceptor(Process):
def __init__(self, messages, url: str) -> None:
super().__init__()
self.__messages = messages
self.__url = urllib.parse.urlparse(url)
assert self.__url.hostname is not None and self.__url.port is not None
def _run(self):
print(multiprocessing.current_process().ident, "server process begin")
selector = selectors.DefaultSelector()
socket_ = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socket_.bind((self.__url.hostname, self.__url.port))
socket_.setblocking(False)
selector.register(socket_, selectors.EVENT_READ)
while self.is_running():
try:
for key, mask in selector.select(timeout=self._timeout):
if key.fileobj == socket_:
self.__messages.put_nowait(socket_.recv(1024))
except KeyboardInterrupt:
pass
selector.unregister(socket_)
socket_.close()
print(multiprocessing.current_process().ident, "server process end")
def stop(signum, frame):
state.is_running = False
print()
def log_statistics(handlers: list[MessageHandler], delay_sec: int, file_path: str):
statistics = None
for handler in handlers:
statistic = handler.get_statistics()
if statistic is None:
continue
if statistics is None:
statistics = statistic
else:
statistics.accumulate(statistic)
if statistics is not None:
statistics.delay_sec = delay_sec
with open(file_path, 'a') as file:
file.write(f"{json.dumps(statistics.__dict__)}\n")
print(file_path, " log ", json.dumps(statistics.__dict__))
if __name__ == "__main__":
if len(sys.argv) < 5:
print(sys.argv[0], "url", "thread_count", "delay_sec", "output_log_path")
exit(1)
messages = multiprocessing.Queue()
statistics = multiprocessing.Queue()
acceptor = UDPSocketAcceptor(messages, sys.argv[1])
handlers = []
delay_sec = int(sys.argv[3])
print("start ...")
for i in range(int(sys.argv[2])):
handler = MessageHandler(messages)
handler.start()
handlers.append(handler)
acceptor.start()
print("start OK")
state = type('', (), {'is_running': True})
signal.signal(signal.SIGINT, stop)
print("press 'ctrl+c' to stop server")
time_step = 0.1
while state.is_running:
for i in range(int(delay_sec / time_step)):
if state.is_running:
time.sleep(time_step)
else:
break
log_statistics(handlers, delay_sec, sys.argv[4])
print("stop ...")
acceptor.stop()
for handler in handlers:
handler.stop()
print("stop OK")