-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonLauncher.py
More file actions
54 lines (49 loc) · 1.5 KB
/
pythonLauncher.py
File metadata and controls
54 lines (49 loc) · 1.5 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
import sys
import psutil
import zmq
import sys
try:
import cbor2 as cbor
except ModuleNotFoundError:
import cbor
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind(sys.argv[1])
socket.setsockopt(zmq.RCVTIMEO, 1000)
PID = int(sys.argv[2])
module = {}
def isCoppeliaSimAlive():
try:
proc = psutil.Process(PID)
if proc.is_running() and proc.status() != psutil.STATUS_ZOMBIE and proc.name() == 'coppeliaSim':
return True
except (psutil.NoSuchProcess, psutil.AccessDenied):
pass
return False
while True:
try:
# in: cmd + (code or func+args), out: ret or err
raw = socket.recv()
req = cbor.loads(raw)
rep = {}
if req['cmd'] == 'loadCode':
try:
code = compile(req['code'], req['info'], "exec") #__EXCEPTION__
exec(code,module) #__EXCEPTION__
rep = {'ret': None}
except Exception as e:
import traceback
rep = {'err': traceback.format_exc()}
elif req['cmd'] == 'callFunc':
try:
func = module[req['func']]
rep = {'ret': func(*req['args'])} #__EXCEPTION__
except Exception as e:
import traceback
rep = {'err': traceback.format_exc()}
else:
rep = {'err': f'unknown command: "{req["cmd"]}"'}
socket.send(cbor.dumps(rep))
except zmq.Again:
if isCoppeliaSimAlive():
continue