-
Notifications
You must be signed in to change notification settings - Fork 1
/
echo_executor.py
76 lines (63 loc) · 2.58 KB
/
echo_executor.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
from pycolonies import Crypto
from pycolonies import colonies_client
import signal
import os
import uuid
class PythonExecutor:
def __init__(self):
colonies, colonyname, colony_prvkey, _, _ = colonies_client()
self.colonyname = colonyname
self.colonies = colonies
self.colony_prvkey = colony_prvkey
crypto = Crypto()
self.executor_prvkey = crypto.prvkey()
self.executorid = crypto.id(self.executor_prvkey)
self.register()
def register(self):
executor = {
"executorname": "echo-executor",
"executorid": self.executorid,
"colonyname": self.colonyname,
"executortype": "echo-executor"
}
self.executorname = executor["executorname"]
try:
self.colonies.add_executor(executor, self.colony_prvkey)
self.colonies.approve_executor(self.colonyname, executor["executorname"], self.colony_prvkey)
except Exception as err:
print(err)
print("Executor", self.executorname, "registered")
try:
self.colonies.add_function(self.colonyname,
self.executorname,
"echo",
self.executor_prvkey)
except Exception as err:
print(err)
def start(self):
while (True):
try:
process = self.colonies.assign(self.colonyname, 10, self.executor_prvkey)
print("Process", process.processid, "is assigned to executor")
if process.spec.funcname == "echo":
# if "in" is defined, it is the output of the parent process,
# use the output from parent process instead of args
if len(process.input)>0:
args = process.input
else:
args = process.spec.args
# just set output to input value
self.colonies.close(process.processid, [args[0]], self.executor_prvkey)
except Exception as err:
print(err)
pass
def unregister(self):
self.colonies.remove_executor(self.colonyname, self.executorname, self.colony_prvkey)
print("Executor", self.executorname, "unregistered")
os._exit(0)
def sigint_handler(signum, frame):
executor.unregister()
if __name__ == '__main__':
signal.signal(signal.SIGINT, sigint_handler)
executor = PythonExecutor()
executor.start()