def _prepare_subprocess_info(self): self._subprocess_pull = self.ctx.socket(zmq.PULL) self._subprocess_pull.bind(self._subpr_push_addr) self.subprocess_thr = threading.Thread(target=self._subprocess_info, args=[self._subpr_push_addr]) self.subprocess_thr.daemon = True self._stop_monitoring = False self.subprocess_thr.start() recv_msg(self._subprocess_pull) self._all_sockets.append(self._subprocess_pull)
def _prepare_publisher(self): tmp_pull = self.ctx.socket(zmq.PULL) tmp_pull.bind(self._pull_addr) self.pub_thr = threading.Thread(target=self._publisher_thread, args=[self.pub_addresses, self._push_addr, self._pull_addr]) self.pub_thr.daemon = True self._stop_publishing = False self.pub_thr.start() recv_msg(tmp_pull) self._publish_socket = self._push_sock(self.ctx, self._push_addr) self._all_sockets.append(self._publish_socket) tmp_pull.close()
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
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
def run(self): self.pre_run() poller = zmq.Poller() poll_sockets = list(self._poll_sockets) for sock in poll_sockets: poller.register(sock, zmq.POLLIN) try: while True: socks = [] try: socks = dict(poller.poll()) except zmq.ZMQError as e: self.logger.warning(": zmq.poll(): " + str(e.strerror)) for sock in socks: if socks[sock] == zmq.POLLIN: more = True while more: try: msg = recv_msg(sock, flags=zmq.NOBLOCK) except zmq.ZMQError as e: if e.errno == zmq.EAGAIN or sock.getsockopt(zmq.TYPE) == zmq.REP: more = False else: self.logger.error("handling socket read error: %s %d %s", e, e.errno, sock) poller.unregister(sock) if sock in poll_sockets: poll_sockets.remove(sock) self.handle_socket_read_error(sock, e) break else: self.handle_message(msg, sock) else: self.logger.warning("sock not zmq.POLLIN! Ignore !") if self.interrupted: break self._update_poller(poller, poll_sockets) except Exception as e: # from urllib2 import HTTPError # try: # self.logger.critical("UNHANDLED EXCEPTION IN %s!!! ABORTING! Exception data: %s, e.args: %s, %s", # self.name, e, e.args, vars(e), exc_info=True, # extra={'stack': True}) # except HTTPError, e: # self.logger.info('sentry sending failed....') self._clean_up() raise(e) self._clean_up()
def _register(self, rep_addrs, pub_addrs, params): message = self.mtool.fill_msg("register_peer", peer_type=self.type, uuid=self.uuid, rep_addrs=rep_addrs, pub_addrs=pub_addrs, name=self.name, other_params=params) self.logger.debug("_register() " + str(message)) send_msg(self.source_req_socket, message) response_str = recv_msg(self.source_req_socket) response = self.mtool.unpack_msg(response_str) if response.type == "rq_error": self.logger.critical("Registration failed: {0}".format(response_str)) sys.exit(2) return response
def obci_monitor(self, ctx, pull_addr, server_ip=None): pull = ctx.socket(zmq.PULL) pull.connect(pull_addr) subscriber = ctx.socket(zmq.SUB) subscriber.setsockopt(zmq.SUBSCRIBE, "") poller = zmq.Poller() poller.register(pull, zmq.POLLIN) poller.register(subscriber, zmq.POLLIN) _all = [pull, subscriber] if server_ip: addr = server_ip else: addr = net.server_address(sock_type='pub') subscriber.connect(addr) def handle_msg(msg): if msg.type == '_launcher_engine_msg': if msg.task == 'connect': print "msg.pub.addr ", msg.pub_addr, msg subscriber.connect(msg.pub_addr) else: self.obci_state_change.emit(msg) while not self._stop_monitoring: try: socks = dict(poller.poll(timeout=200)) except: break for socket in socks: if socks[socket] == zmq.POLLIN: msg = self.mtool.unpack_msg(recv_msg(socket)) if not self._stop_monitoring: handle_msg(msg) for sock in _all: sock.close() # linger=0)
def test(self): # for i in range(SEND): # send_msg(self.push, str(i)) self.pull = self.ctx.socket(zmq.SUB) self.pull.bind("tcp://*:16789") received = 0 prev = -1 for i in range(SEND): msg = recv_msg(self.pull) if int(msg): # prev = int(msg) received += 1 if received % 10000 == 0: print "zmq: received ", received, "messages, last: ", msg if received == SEND: print "zmq: OK" else: print "WUT?", received # self.push.close() self.pull.close()
def run(self): self.pre_run() poller = zmq.Poller() poll_sockets = list(self._poll_sockets) for sock in poll_sockets: poller.register(sock, zmq.POLLIN) try: while True: socks = [] try: socks = dict(poller.poll()) except zmq.ZMQError, e: self.logger.error(": zmq.poll(): " +str( e.strerror)) for sock in socks: if socks[sock] == zmq.POLLIN: more = True while more: try: msg = recv_msg(sock, flags=zmq.NOBLOCK) except zmq.ZMQError, e: if e.errno == zmq.EAGAIN or sock.getsockopt(zmq.TYPE) == zmq.REP: more = False else: self.logger.error("handling socket read error: %s %d %s", e, e.errno, sock) poller.unregister(sock) if sock in poll_sockets: poll_sockets.remove(sock) self.handle_socket_read_error(sock, e) break else: self.handle_message(msg, sock) else: self.logger.warning("sock not zmq.POLLIN! Ignore !") if self.interrupted: break self._update_poller(poller, poll_sockets)
def test(self): # for i in range(SEND): # send_msg(self.push, str(i)) print("zmq client --- start receiving") received = 0 prev = -1 for i in range(SEND): msg = recv_msg(self.pull) if int(msg): # prev = int(msg) received += 1 if received % 10000 == 0: print("zmq: received ", received, "messages, last: ", msg) if received == SEND: print("zmq: OK") else: print("OHHHH NOOOOOOOOO :( :( :( :( :(", received) # self.push.close() self.pull.close() print("zmq: finished.")
def run(self): self.pre_run() poller = zmq.Poller() poll_sockets = list(self._poll_sockets) for sock in poll_sockets: poller.register(sock, zmq.POLLIN) try: while True: socks = [] try: socks = dict(poller.poll()) except zmq.ZMQError, e: self.logger.warning(": zmq.poll(): " +str( e.strerror)) for sock in socks: if socks[sock] == zmq.POLLIN: more = True while more: try: msg = recv_msg(sock, flags=zmq.NOBLOCK) except zmq.ZMQError, e: if e.errno == zmq.EAGAIN or sock.getsockopt(zmq.TYPE) == zmq.REP: more = False else: self.logger.error("handling socket read error: %s %d %s", e, e.errno, sock) poller.unregister(sock) if sock in poll_sockets: poll_sockets.remove(sock) self.handle_socket_read_error(sock, e) break else: self.handle_message(msg, sock) else: self.logger.warning("sock not zmq.POLLIN! Ignore !") if self.interrupted: break self._update_poller(poller, poll_sockets)
def test(self): # for i in range(SEND): # send_msg(self.push, str(i)) self.pull = self.ctx.socket(zmq.SUB) self.pull.bind('tcp://*:16789') received = 0 prev = -1 for i in range(SEND): msg = recv_msg(self.pull) if int(msg): # prev = int(msg) received += 1 if received % 10000 == 0: print "zmq: received ", received, "messages, last: ", msg if received == SEND: print "zmq: OK" else: print "WUT?", received # self.push.close() self.pull.close()
def test(self): # for i in range(SEND): # send_msg(self.push, str(i)) print "zmq client --- start receiving" received = 0 prev = -1 for i in range(SEND): msg = recv_msg(self.pull) if int(msg): # prev = int(msg) received += 1 if received % 10000 == 0: print "zmq: received ", received, "messages, last: ", msg if received == SEND: print "zmq: OK" else: print "OHHHH NOOOOOOOOO :( :( :( :( :(", received # self.push.close() self.pull.close() print "zmq: finished."