-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
56 lines (52 loc) · 1.87 KB
/
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
import ast
import multiprocessing as mp
import select
import socket
import sys
import threading
import time
import distrib
if __name__ == "__main__":
try:
server = distrib.DistributedServer()
server.start()
while True:
raw = input()
cmd = raw.split()
if len(cmd) == 0:
continue
if cmd[0] == 'exit':
break
elif cmd[0] == 'info':
out = "\n"
out += "WORKER POOL INFO\n"
out += "Worker count : {}\n".format(server.n_workers)
out += "Worker threads : {}\n".format(server.n_total_threads)
out += "Total memory : {:01.1f}GB\n".format(server.memory)
out += "Queued tasks : {}\n".format(server.n_queued_tasks)
print(out)
elif cmd[0] == 'send':
msg = raw.split(maxsplit=1)[1]
print("Sending message: {}".format(msg))
server.broadcast(msg)
elif cmd[0] == 'task' and len(cmd) >= 2:
num = 1
data = None
num = int(cmd[2])
if len(cmd) >= 4:
data = ast.literal_eval(raw.split(maxsplit=3)[3])
print("Requesting {} task(s): {}".format(num, cmd[1]))
res = []
for _ in range(num):
res.append(server.execute(cmd[1], data))
for r in res:
r.result()
print("Done!")
elif cmd[0] == 'share':
data = ast.literal_eval(raw.split(maxsplit=1)[1])
print("Sharing data: {}".format(data))
server.share(data)
else:
print("Unknown command: {}".format(raw))
finally:
server.stop()