-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommunication.py
39 lines (35 loc) · 908 Bytes
/
communication.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
import json
import socket
import threading
import logging
import time
# debugging
from pprint import pformat
debug = False
# connection sockets for clients
clients = []
# TODO remove this, used for debugging
from pprint import pformat
dont_use_network = False
_lock = threading.Lock()
def send_command(name, data={}):
"""
Send a command: name is the target function's name, data is the target
function's kwargs.
"""
global clients
with _lock:
data['__cmd__'] = name
if debug:
print 'Sending:', pformat(data)
jdata = json.dumps(data) + '\n'
for c in clients:
try:
c.send(jdata)
except socket.timeout as e:
logging.exception(e)
continue
except IOError as e:
logging.exception(e)
clients.remove(c)
time.sleep(0.02)