本文整理汇总了Python中obci.control.common.message.OBCIMessageTool.unpack_msg方法的典型用法代码示例。如果您正苦于以下问题:Python OBCIMessageTool.unpack_msg方法的具体用法?Python OBCIMessageTool.unpack_msg怎么用?Python OBCIMessageTool.unpack_msg使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类obci.control.common.message.OBCIMessageTool
的用法示例。
在下文中一共展示了OBCIMessageTool.unpack_msg方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _gather_other_server_results
# 需要导入模块: from obci.control.common.message import OBCIMessageTool [as 别名]
# 或者: from obci.control.common.message.OBCIMessageTool import unpack_msg [as 别名]
def _gather_other_server_results(ctx, this_addr, ip_list):
exps = []
if not ip_list:
return exps
mtool = OBCIMessageTool(message_templates)
other_exps_pull = ctx.socket(zmq.PULL)
port = other_exps_pull.bind_to_random_port('tcp://*',
min_port=PORT_RANGE[0],
max_port=PORT_RANGE[1], max_tries=500)
my_push_addr = this_addr + ':' + str(port)
LOGGER.info("my exp_data pull: " + my_push_addr)
harvester = zmq.Poller()
harvester.register(other_exps_pull)
reqs = {}
for srv_ip in ip_list:
addr = 'tcp://' + srv_ip + ':' + net.server_rep_port()
req = ctx.socket(zmq.REQ)
req.connect(addr)
send_msg(req, mtool.fill_msg('find_eeg_experiments',
client_push_address='tcp://' + my_push_addr,
checked_srvs=[this_addr]))
harvester.register(req, zmq.POLLIN)
reqs[req] = addr
srv_responses = 0
for i in range(len(reqs) * 50):
socks = dict(harvester.poll(timeout=300))
for req in socks:
msg = recv_msg(req)
msg = mtool.unpack_msg(msg)
if msg.type == 'rq_ok':
LOGGER.info("waiting for experiments from server: " + str(reqs[req]))
harvester.unregister(req)
req.close()
elif msg.type == 'eeg_experiments':
LOGGER.info("GOT EXPERIMENTS from: " + msg.sender_ip)
exps += msg.experiment_list
srv_responses += 1
else:
LOGGER.warning('strange msg: ' + str(msg))
if srv_responses == len(ip_list):
print("GOT ALL eeg_experiment RESPONSES :-) [addr list: ", ip_list, "]")
break
other_exps_pull.close()
return exps
示例2: update_nearby_servers
# 需要导入模块: from obci.control.common.message import OBCIMessageTool [as 别名]
# 或者: from obci.control.common.message.OBCIMessageTool import unpack_msg [as 别名]
def update_nearby_servers(srv_data, bcast_port, ctx=None, update_push_addr=None):
mtool = OBCIMessageTool(message_templates)
loops_to_update = _LOOPS
s = socket(AF_INET, SOCK_DGRAM)
s.bind(('', bcast_port))
notify_sock = None
if update_push_addr is not None:
ctx = ctx if ctx else zmq.Context()
notify_sock = ctx.socket(zmq.PUSH)
notify_sock.connect(update_push_addr)
while 1:
changed = False
try:
inp, out, exc = select.select([s], [], [], UPDATE_INTERVAL / _LOOPS)
except Exception, e:
srv_data.logger.critical("nearby_servers_update - exception: %s", str(e))
srv_data.logger.critical("nearby_servers - aborting")
return
if s in inp:
data, wherefrom = s.recvfrom(1500, 0)
# sys.stderr.write(repr(wherefrom) + '\n')
# sys.stdout.write(data)
msg = unicode(data, encoding='utf-8')
msg = msg[:-1]
message = mtool.unpack_msg(msg)
changed = srv_data.update(ip=wherefrom[0], hostname=message.sender_ip,
uuid=message.sender, rep_port=message.rep_port,
pub_port=message.pub_port)
else:
# print "no data"
pass
loops_to_update -= 1
if loops_to_update == 0:
loops_to_update = _LOOPS
changed = srv_data.clean_silent()
if changed:
send_msg(notify_sock, mtool.fill_msg('nearby_machines',
nearby_machines=srv_data.dict_snapshot()))
示例3: kill
# 需要导入模块: from obci.control.common.message import OBCIMessageTool [as 别名]
# 或者: from obci.control.common.message.OBCIMessageTool import unpack_msg [as 别名]
def kill(self):
# send "kill" to the process or kill request to its supervisor?
self.stop_monitoring()
if not self._ctx:
self._ctx = zmq.Context()
rq_sock = self._ctx.socket(zmq.REQ)
try:
rq_sock.connect(self.rq_address)
mtool = OBCIMessageTool(message_templates)
poller = PollingObject()
send_msg(rq_sock, mtool.fill_msg("kill_process", pid=self.pid, machine=self.machine_ip))
res, _ = poller.poll_recv(rq_sock, timeout=5000)
finally:
rq_sock.close()
if res:
res = mtool.unpack_msg(res)
print "Response to kill request: ", res
with self._status_lock:
self._status = TERMINATED
示例4: EEGExperimentFinder
# 需要导入模块: from obci.control.common.message import OBCIMessageTool [as 别名]
# 或者: from obci.control.common.message.OBCIMessageTool import unpack_msg [as 别名]
#.........这里部分代码省略.........
req_sock = self.ctx.socket(zmq.REQ)
try:
req_sock.connect(rep_addr)
send_msg(req_sock, self.mtool.fill_msg('get_experiment_info'))
res, details = self.poll_recv(req_sock, 4000)
finally:
req_sock.close()
if not res:
LOGGER.error("Connection failed (experiment " + exp_desc['name'] +
"), get_experiment_info")
return None
exp_info = res.dict() # json.loads(res)
for field in ["peers_status", "details"]:
del exp_info["experiment_status"][field]
peer_list = exp_info["peers"]
if not self._has_mx(peer_list):
LOGGER.info("Experiment " + exp_desc['name'] +
" does not have a multiplexer.")
return None
maybe_amps = self._amp_like_peers(peer_list)
if not maybe_amps:
LOGGER.info("Experiment " + exp_desc['name'] +
" -- no amplifier.")
return None
req_sock = self.ctx.socket(zmq.REQ)
try:
req_sock.connect(rep_addr)
for peer in maybe_amps:
info, params = self._get_amp_info(req_sock, peer)
if not self._is_amplifier(info, params):
LOGGER.info("Experiment " + exp_desc['name'] +
" -- peer " + str(peer) + "is not an amplifier.")
continue
else:
exp_data = self._create_exp_data(exp_info, info, params['param_values'],
rep_addr, pub_addr, tcp_addr)
amp_options.append(exp_data)
finally:
req_sock.close()
return amp_options
def _get_amp_info(self, exp_sock, peer_id):
send_msg(exp_sock, self.mtool.fill_msg('get_peer_info', peer_id=peer_id))
info, details = self.poll_recv(exp_sock, 4000)
send_msg(exp_sock, self.mtool.fill_msg('get_peer_param_values', peer_id=peer_id))
params, details = self.poll_recv(exp_sock, 4000)
if not info or not params:
LOGGER.error("get_peer_info failed " + str(peer_id) + " " + str(details))
return None, None
info = info.dict()
params = params.dict()
for field in ["sender", "sender_ip", "receiver", "type", "local_params", "external_params",
"config_sources", "launch_dependencies"]:
del info[field]
return info, params
def _is_amplifier(self, peer_info, peer_params):
info = peer_info
peer_id = info['peer_id']
if not info['peer_type'] == 'obci_peer':
LOGGER.info("Peer " + str(peer_id) + " not obci_peer")
return False
params = peer_params['param_values']
if 'channels_info' not in params or\
'active_channels' not in params:
LOGGER.info('Peer ' + str(peer_id) + " no channels_info param.")
return False
return True
def _create_exp_data(self, exp_info, peer_info, params, rep_addr, pub_addr, tcp_addr):
data = {}
data['amplifier_params'] = params
data['amplifier_peer_info'] = peer_info
data['experiment_info'] = exp_info
data['rep_addrs'] = [rep_addr]
data['pub_addrs'] = [pub_addr]
data['tcp_addrs'] = [tcp_addr]
return data
def _has_mx(self, peer_list):
return [peer for peer in peer_list if peer.startswith('mx')] != []
def _amp_like_peers(self, peer_list):
return [peer for peer in peer_list if peer.startswith('amplifier')]
def poll_recv(self, socket, timeout):
result, details = self.poller.poll_recv(socket, timeout)
if result:
result = self.mtool.unpack_msg(result)
return result, details
示例5: SubprocessMonitor
# 需要导入模块: from obci.control.common.message import OBCIMessageTool [as 别名]
# 或者: from obci.control.common.message.OBCIMessageTool import unpack_msg [as 别名]
#.........这里部分代码省略.........
path=path,
args=args,
machine_ip=machine,
pid=popen_obj.pid)
# io_handler will be None if no stdio is captured
io_handler = start_stdio_handler(popen_obj, std_actions,
':'.join([machine, path, name]),
stdout_log, stderr_log)
new_proc = LocalProcess(process_desc, popen_obj, io_handler=io_handler,
reg_timeout_desc=timeout_desc,
monitoring_optflags=monitoring_optflags,
logger=self.logger)
if monitoring_optflags & PING:
new_proc._ctx = self._ctx
with self._proc_lock:
self._processes[(machine, popen_obj.pid)] = new_proc
new_proc.start_monitoring()
return new_proc, None
def new_remote_process(self, path, args, proc_type, name,
machine_ip, conn_addr,
capture_io= STDOUT | STDIN,
stdout_log=None,
stderr_log=None,
register_timeout_desc=None,
monitoring_optflags=PING):
"""Send a request to conn_addr for a process launch. By default
the process will be monitored with ping requests and locally by the
remote peer."""
timeout_desc = register_timeout_desc
rq_message = self._mtool.fill_msg('launch_process',
path=path,
args=args, proc_type=proc_type,
name=name,
machine_ip=machine_ip,
capture_io=capture_io,
stdout_log=stdout_log,
stderr_log=stderr_log)
rq_sock = self._ctx.socket(zmq.REQ)
try:
rq_sock.connect(conn_addr)
except zmq.ZMQError as e:
det = "Could not connect to {0}, err: {1}, {2}".format(
conn_addr, e, e.args)
self.logger.error(det)
return None, det
self.logger.info("SENDING LAUNCH REQUEST {0} {1} {2} {3}".format(
machine_ip, _DEFAULT_TIMEOUT_MS, 'ms', conn_addr))
send_msg(rq_sock, rq_message)
result, details = self.poller.poll_recv(rq_sock, _DEFAULT_TIMEOUT_MS)
rq_sock.close()
if not result:
details = details + " [address was: {0}]".format(conn_addr)
self.logger.error(details)
return None, details
else:
result = self._mtool.unpack_msg(result)
if result.type == 'rq_error':
det = "REQUEST FAILED" + str(result.err_code) + ':' + str(result.details)
self.logger.error(det)
return None, det
elif result.type == 'launched_process_info':
self.logger.info("REQUEST SUCCESS %s", result.dict())
process_desc = ProcessDescription(proc_type=result.proc_type,
name=result.name,
path=result.path,
args=args,
machine_ip=result.machine,
pid=result.pid)
new_proc = RemoteProcess(process_desc, conn_addr,
reg_timeout_desc=timeout_desc,
monitoring_optflags=monitoring_optflags,
logger=self.logger)
if monitoring_optflags & PING:
new_proc._ctx = self._ctx
with self._proc_lock:
self._processes[(result.machine, result.pid)] = new_proc
new_proc.start_monitoring()
return new_proc, None
示例6: str
# 需要导入模块: from obci.control.common.message import OBCIMessageTool [as 别名]
# 或者: from obci.control.common.message.OBCIMessageTool import unpack_msg [as 别名]
port = exp_info_pull.bind_to_random_port('tcp://*',
min_port=PORT_RANGE[0],
max_port=PORT_RANGE[1], max_tries=500)
client_push_addr = my_addr + ':' + str(port)
print(client_push_addr)
send_msg(server_req, mtool.fill_msg('find_eeg_experiments',
client_push_address=client_push_addr))
msg, details = pl.poll_recv(server_req, 5000)
if not msg:
print("srv request timeout!")
server_req.close()
sys.exit(1)
response = mtool.unpack_msg(msg)
if not response.type == 'rq_ok':
print("whaaa?")
sys.exit(1)
msg, details = pl.poll_recv(exp_info_pull, 20000)
if not msg:
print("TIMEOUT")
else:
exp_info = mtool.unpack_msg(msg)
sss = json.dumps(exp_info.experiment_list, indent=4)
# print sss
print(len(sss))
print([(exp['rep_addr'], exp['experiment_info']['name']) for exp in exp_info.experiment_list])
示例7: OBCIClient
# 需要导入模块: from obci.control.common.message import OBCIMessageTool [as 别名]
# 或者: from obci.control.common.message.OBCIMessageTool import unpack_msg [as 别名]
#.........这里部分代码省略.........
custom_config_path=None, config_sources=None, launch_dependencies=None,
apply_globals=True):
response = self.get_experiment_contact(strname)
if response.type == "rq_error" or response.type == "no_data":
return response
sock = self.ctx.socket(zmq.REQ)
try:
self._connect(sock, response.rep_addrs)
send_msg(sock, self.mtool.fill_msg("add_peer",
peer_id=peer_id, peer_path=path, peer_type='obci_peer',
machine=machine, param_overwrites=param_overwrites,
custom_config_path=custom_config_path, config_sources=config_sources,
launch_dependencies=launch_dependencies, apply_globals=apply_globals))
response, details = self.poll_recv(sock, 5000)
return response
finally:
sock.close()
def kill_peer(self, exp_strname, peer_id, remove_config=False):
response = self.get_experiment_contact(exp_strname)
if response.type == "rq_error" or response.type == "no_data":
return response
try:
sock = self.ctx.socket(zmq.REQ)
self._connect(sock, response.rep_addrs)
send_msg(sock, self.mtool.fill_msg("kill_peer",
peer_id=peer_id, remove_config=remove_config))
response, details = self.poll_recv(sock, 5000)
return response
finally:
sock.close()
def _connect(self, sock, addr_list):
print("**** ", addr_list)
this = self._is_this_machine(addr_list)
connected = False
for addr in addr_list:
if not this and ('localhost' in addr or '127.0.0.1' in addr):
continue
try:
sock.connect(addr)
connected = True
except zmq.ZMQError as e:
print(addr, " ::: ", str(e))
if not connected:
raise Exception("Could not connect to any of the addresses: " + str(addr_list))
def _is_this_machine(self, addr_list):
for addr in addr_list:
if self.dns.is_this_machine(addr):
return True
return False
def leave_experiment(self, strname, peer_id):
response = self.get_experiment_contact(strname)
if response.type == "rq_error" or response.type == "no_data":
return response
try:
sock = self.ctx.socket(zmq.REQ)
self._connect(sock, response.rep_addrs)
send_msg(sock, self.mtool.fill_msg("leave_experiment",
peer_id=peer_id))
response, details = self.poll_recv(sock, 5000)
return response
finally:
sock.close()
def get_tail(self, strname, peer_id, len_):
response = self.get_experiment_contact(strname)
if response.type == "rq_error" or response.type == "no_data":
return response
sock = self.ctx.socket(zmq.REQ)
try:
for addr in response.rep_addrs:
sock.connect(addr)
send_msg(sock, self.mtool.fill_msg("get_tail", peer_id=peer_id, len=len_))
response, details = self.poll_recv(sock, 4000)
return response
finally:
sock.close()
def poll_recv(self, socket, timeout):
result, details = self.poller.poll_recv(socket, timeout)
if result:
result = self.mtool.unpack_msg(result)
else:
result = EmptyResponse(details)
return result, details
示例8: ExperimentEngineInfo
# 需要导入模块: from obci.control.common.message import OBCIMessageTool [as 别名]
# 或者: from obci.control.common.message.OBCIMessageTool import unpack_msg [as 别名]
#.........这里部分代码省略.........
for peer, status in exp_msg.experiment_status['peers_status'].items():
self.status.peer_status(peer).set_status(
status['status_name'],
details=status['details'])
def parameters(self, peer_id, mode):
params = {}
peer = self.exp_config.peers[peer_id]
if mode == MODE_BASIC:
for par in peer.public_params:
params[par] = (self.exp_config.param_value(peer_id, par), None)
else:
params = peer.config.local_params
for param in peer.config.local_params:
params[param] = (self.exp_config.param_value(peer_id, param), None)
for param, defi in peer.config.ext_param_defs.items():
source_symbol = defi[0]
source = peer.config.config_sources[source_symbol]
params[param] = (self.exp_config.param_value(peer_id, param), source + '.' + defi[1])
return params
def comm_exp(self, msg):
send_msg(self.exp_req, msg)
response, _ = self.poller.poll_recv(self.exp_req, timeout=3000)
if not response:
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!1 no response to ", msg)
self.exp_req.close()
self.exp_req = self.ctx.socket(zmq.REQ)
for addr in self.launcher_data['rep_addrs']:
if self._addr_connectable(addr, self.launcher_data['origin_machine']):
self.exp_req.connect(addr)
return None
return self.mtool.unpack_msg(response)
def updatable(self, peer_id, config_part, **kwargs):
return False
def update_peer_param(self, peer_id, param, value, runtime=False):
changes = self.overwrites if not runtime else self.runtime_changes
ovr = changes.get(peer_id, None)
ovr = ovr if ovr is not None else {}
if param not in ovr:
old = self.exp_config.param_value(peer_id, param)
if old != value:
ovr[param] = old
changes[peer_id] = ovr
self.exp_config.update_local_param(peer_id, param, value)
def get_launch_args(self):
d = dict(launch_file=self.launch_file, name=self.name)
args = ['--ovr']
if self.overwrites:
for peer_id in self.overwrites:
args.append('--peer')
args.append(peer_id)
for arg in self.overwrites[peer_id]:
args += ['-p', arg, self.exp_config.param_value(peer_id, arg)]
pack = peer_cmd.peer_overwrites_pack(args)
d['overwrites'] = pack
print("overwrites pack!!!!!!!!!!!!!!!!!!!!! ", pack)
return d