def __init__(self, address): self.pull = self.poller = None self.monitor_poller = self.monitor_socket = None self.address = address self.context = Context.instance() self.open() self.first_missing = None
def __init__(self): super().__init__() self.ctx = Context.instance() #cmd socket is a synchronous socket, we don't want the asyncio context. self.cmd_socket = zmq.Context().socket(zmq.REQ) self.cmd_socket.connect("tcp://127.0.0.1:{}".format( os.environ.get('MODEL_PORT', 12312))) init_vals = self.get_cavity_ACTs_from_model() cav_pvs = { device_name: CavityPV(device_name, self.on_cavity_change, initial_values=init_vals[device_name], prefix=device_name) for device_name in init_vals.keys() } #setting up convenient linac section PVs for changing all of the L1B/L2B/L3B cavities simultaneously. linac_init_vals = _make_linac_table(init_vals) linac_pvs = { device_name: CavityPV(device_name, self.on_cavity_change, initial_values=linac_init_vals[device_name], prefix=device_name) for device_name in linac_init_vals.keys() } self.add_pvs(cav_pvs) self.add_pvs(linac_pvs) L.info("Initialization complete.")
async def events_publish(cn, payload): ''' publish a lot of events @cn number of events for publishing @paload even size in bytes ''' ctx = Context.instance() pub = ctx.socket(zmq.PUB) pub.connect(pub_url) print(f'publisher url {pub_url}, counter {cn}') #mandatory delay time.sleep(1) print('start perfomance measureing...') evnt = [topic, b'perf start'] await pub.send_multipart(evnt) while cn: cn = cn - 1 evnt = [topic, payload] await pub.send_multipart(evnt) evnt = [topic, b'perf stop'] await pub.send_multipart(evnt) print('stop perfomance measureing...')
async def __zmqSimpleClient(self, zmqConf): self.assertTrue(ZMQInterface.CONF_IP_ADDRESS in zmqConf) self.assertTrue(ZMQInterface.CONF_PORT in zmqConf) await asyncio.sleep(1) try: zmqCtx = Context.instance() address = 'tcp://%s:%s' % (str( zmqConf[ZMQInterface.CONF_IP_ADDRESS]), str(zmqConf[ZMQInterface.CONF_PORT])) socket = zmqCtx.socket(zmq.REQ) socket.connect(address) await socket.send( str.encode(json.dumps({"request": "resourcesInfo"}))) logging.info("request sent - waiting for response") resp = await socket.recv() logging.info("received response: %s" % bytes.decode(resp)) except: logging.exception("Client failed") socket.close() logging.info("client finishing")
async def zmq_iota_recv(): ctx = Context.instance() print("Connecting to ZMQ...") s = ctx.socket(zmq.SUB) s.connect('tcp://%s:%s' % (IOTA_HOST, IOTA_ZMQ_PORT)) print("Subscribing to tx_trytes...") s.subscribe(b"tx_trytes") while True: msg = await s.recv() #print('received', msg) topic, data, hash_ = msg.split(b' ') str_data = data.decode('ascii') str_hash = hash_.decode('ascii') tx = Transaction.from_tryte_string(data, hash_) print(str(tx.address), connection_addresses.get(str(tx.address), []), repr(connection_addresses)) tasks = [ send_json( subscription.connection, { 'id': subscription.id, 'type': 'transaction', 'data': str_data, 'hash': str_hash }) for subscription in connection_addresses.get(str(tx.address), []) ] if tasks: await asyncio.wait(tasks) s.close()
def __init__(self): super().__init__() self.ctx = Context.instance() #cmd socket is a synchronous socket, we don't want the asyncio context. self.cmd_socket = zmq.Context().socket(zmq.REQ) self.cmd_socket.connect("tcp://127.0.0.1:{}".format( os.environ.get('MODEL_PORT', 12312))) init_vals = self.get_initial_values() mag_pvs = { device_name: MagnetPV(device_name, simulacrum.util.convert_device_to_element(device_name), self.on_magnet_change, length=init_vals[device_name]['length'], initial_value=init_vals[device_name], prefix=device_name) for device_name in simulacrum.util.device_names if device_name.startswith("XCOR") or device_name.startswith("YCOR") or device_name.startswith("QUAD") or device_name.startswith("BEND") } self.add_pvs(mag_pvs) # Now that we've set up all the magnets, we need to send the model a # command to use non-normalized magnetic field units. self.cmd_socket.send_pyobj({ "cmd": "tao", "val": "set ele Kicker::*,Quadrupole::* field_master = T" }) self.cmd_socket.recv_pyobj() L.info("Initialization complete.")
def __init__(self): super().__init__() self.ctx = Context.instance() #cmd socket is a synchronous socket, we don't want the asyncio context. self.cmd_socket = zmq.Context().socket(zmq.REQ) self.cmd_socket.connect("tcp://127.0.0.1:{}".format( os.environ.get('MODEL_PORT', 12312))) init_vals, init_cud_vals = self.get_klystron_ACTs_from_model() init_sbst_vals = self.get_sbst_ACTs_from_model() klys_pvs = { device_name: KlystronPV(device_name, convert_device_to_element(device_name), self.on_klystron_change, initial_values=init_vals[device_name], prefix=device_name) for device_name in init_vals.keys() } cud_pvs = { device_name: CudKlys(device_name, convert_device_to_element(device_name), initial_value=init_cud_vals[device_name], prefix=device_name) for device_name in init_cud_vals.keys() } sbst_pvs = { device_name: SubboosterPV(device_name, convert_sbst_to_element(device_name), prefix=device_name) for device_name in init_sbst_vals.keys() } L.info(init_vals) self.add_pvs(klys_pvs) self.add_pvs(cud_pvs) self.add_pvs(sbst_pvs) L.info("Initialization complete.")
async def recv_batch_worker(buffer, exe, event, lock, threshold_size): """ coroutine to receive batch from actors """ loop = asyncio.get_event_loop() ctx = Context.instance() socket = ctx.socket(zmq.DEALER) socket.connect("ipc:///tmp/5101.ipc") start = False cnt = 0 ts = time.time() while True: identity, data = await socket.recv_multipart(copy=False) async with lock: await loop.run_in_executor(exe, push_batch, buffer, data) await socket.send_multipart((identity, b'')) # TODO: 1. Only one worker should print log to console. # 2. Hard-coded part in (50 * cnt * 4) should be fixed. data = None cnt += 1 if cnt % 100 == 0: print("Buffer Size: {} / FPS: {:.2f}".format( len(buffer), (50 * cnt * 4) / (time.time() - ts) )) ts = time.time() if not start and len(buffer) >= threshold_size: start = True event.set() return True
def __init__(self): self.stop = False self.conx = Context.instance() self.socket = self.conx.socket(zmq.ROUTER) self.socket.bind('tcp://*:5555') self.periodic = PeriodicCallback(self.timer, 4000) self.periodic.start()
async def send_batch_worker(buffer, exe, event, lock, batch_size, beta, actor_num, actor_ips): """ coroutine to send training batches to learner """ seed = int(str(time.time())[-4:]) utils.set_global_seeds(seed, use_torch=False) loop = asyncio.get_event_loop() ctx = Context.instance() socket = ctx.socket(zmq.DEALER) socket.connect("ipc:///tmp/5103.ipc") actors_sockets = [] for i in range(actor_num): ctx = zmq.Context() socket = ctx.socket(zmq.DEALER) socket.connect('tcp://{}:51004'.format(actor_ips[i])) actors_sockets.append(socket) await event.wait() while True: identity, _ = await socket.recv_multipart(copy=False) # TODO: Is there any other greay way to support lock but make sampling faster? async with lock: batch = await loop.run_in_executor(exe, sample_batch, buffer, batch_size, beta, actors_sockets) await socket.send_multipart([identity, batch], copy=False) batch = None return True
def __init__(self, sub_topic_list: list, pub_topic_list: list): self.pub_topic_list = pub_topic_list + [b'disconnect'] self.messages_to_send = asyncio.Queue() self.messages_received = asyncio.Queue() self.client_log = log_sys.get_logger('client') self.client_log.set_level(INFO) self.pub_url = 'tcp://127.0.0.1:' self.broker_listen_url = 'tcp://127.0.0.1:9500' self.broker_pub_url = 'tcp://127.0.0.1:9501' self.ctx = Context.instance() self.pub_sock = self.ctx.socket(zmq.PUB) self.sub_sock = self.ctx.socket(zmq.SUB) self.sub_sock.connect(self.broker_pub_url) sub_topic_list += [b'disconnect'] for st in sub_topic_list: self.sub_sock.setsockopt(zmq.SUBSCRIBE, st) self.loop = None self.tasks_h = None self.is_connected = False self.client_tasks = [self._subscribe_task(), self._publish_task()] self.all_tasks = []
def setup(self, conf): """Open ZMQ interface. If port number is not specified in QCG-PilotJob configuration, it is chosen randomly from configured range. """ # zmq.asyncio.install() self.zmq_ctx = Context.instance() self.address = Config.ZMQ_IFACE_ADDRESS.get(conf) self.socket = self.zmq_ctx.socket(zmq.REP) #pylint: disable=maybe-no-member if Config.ZMQ_PORT.get(conf): self.socket.bind(self.address) else: self.local_port = self.socket.bind_to_random_port( self.address, min_port=int(Config.ZMQ_PORT_MIN_RANGE.get(conf)), max_port=int(Config.ZMQ_PORT_MAX_RANGE.get(conf))) self.real_address = str( bytes.decode(self.socket.getsockopt(zmq.LAST_ENDPOINT))) #pylint: disable=maybe-no-member # the real address might contain the 0.0.0.0 IP address which means that it listens on all # interfaces, sadly this address is not valid for external services to communicate, so we # need to replace 0.0.0.0 with the real address IP self.external_address = self.real_address if '//0.0.0.0:' in self.real_address: self.external_address = self.real_address.replace( '//0.0.0.0:', '//{}:'.format(socket.gethostbyname(socket.gethostname()))) _logger.info( 'ZMQ interface configured (address %s) @ %s, external address @ %s', self.address, self.real_address, self.external_address)
async def eddn(bot): context = Context.instance() subscriber = context.socket(zmq.SUB) subscriber.subscribe(b"") subscriber.set(zmq.RCVTIMEO, __timeoutEDDN) allowed_events = ['Location', 'FSDJump'] bgs = bot.get_cog('BGS') if bgs is None: bot.load_extension('cogs.bgs') bgs = bot.get_cog('BGS') while bot.bgs_run: try: subscriber.connect(__relayEDDN) while bot.bgs_run: __message = await subscriber.recv() if not __message: subscriber.disconnect(__relayEDDN) break __message = zlib.decompress(__message) if ("prismatic imperium" in str(__message).lower() or "adamantine union" in str(__message).lower() or "colonists of aurora" in str(__message).lower()): __json = json.loads(__message, object_hook=lambda d: SimpleNamespace(**d)) message = __json.message if message.event in allowed_events: await bgs.submit(message) except zmq.ZMQError as e: print('ZMQSocketException: ' + str(e)) sys.stdout.flush() subscriber.disconnect(__relayEDDN) time.sleep(5) except Exception as error: embed = discord.Embed(title='Command Exception', color=discord.Color.red()) embed.set_footer(text='Occured on') embed.timestamp = datetime.datetime.utcnow() exc = ''.join(traceback.format_exception(type(error), error, error.__traceback__, chain=False)) exc = exc.replace('`', '\u200b`') embed.description = '```py\n{}\n```'.format(exc) embed.add_field(name='EDDN error', value="EDDN encountered an error") try: for channel in config.ERROR_CHANNELS: await bot.get_channel(channel).send(type(error), embed=embed) except Exception as error: print(error) subscriber.disconnect(__relayEDDN) time.sleep(5)
def __init__(self): super().__init__() self.ctx = Context.instance() #cmd socket is a synchronous socket, we don't want the asyncio context. self.cmd_socket = zmq.Context().socket(zmq.REQ) self.cmd_socket.connect("tcp://127.0.0.1:{}".format(os.environ.get('MODEL_PORT', 12312))) init_vals = self.get_heaters_from_file() heater_pvs = {device_name: HeaterPV(self.on_heater_change, initial_values=init_vals[device_name], prefix=device_name) for device_name in init_vals.keys()}
async def connect_zmq(): context = Context.instance() # Socket to talk to server app.logger.debug("Connecting to FlowMachine server…") socket = context.socket(zmq.REQ) socket.connect(f"tcp://{os.getenv('SERVER')}:5555") request.socket = socket app.logger.debug("Connected.")
def step1(loop, context): """Step 1""" context = context or Context.instance() # Signal downstream to step 2 sender = context.socket(zmq.PAIR) sender.connect("inproc://step2") msg = b'message from step1' yield from sender.send(msg) print('step1 -- sent msg: {}'.format(msg))
def __init__(self, listener: RabbitMqListener, ipc_address, tracer): RabbitMqListener.__init__(self, listener.config) context = Context.instance() self.socket = context.socket(PUSH) self.socket.bind("ipc://" + ipc_address) self.tracer = tracer
def __init__(self): tao_lib = os.environ.get('TAO_LIB', '') self.tao = pytao.Tao(so_lib=tao_lib) path_to_lattice = os.path.join(os.path.dirname(os.path.realpath(__file__)), "lcls.lat") path_to_init = os.path.join(os.path.dirname(os.path.realpath(__file__)), "tao.init") self.tao.init("-noplot -lat {lat_path} -init {init_path}".format(lat_path=path_to_lattice, init_path=path_to_init)) self.ctx = Context.instance() self.model_broadcast_socket = zmq.Context().socket(zmq.PUB) self.model_broadcast_socket.bind("tcp://*:{}".format(os.environ.get('MODEL_BROADCAST_PORT', 66666)))
def __init__(self, name, socket_type, address, bind_mode): self._name = name if name else Utils.uuid1()[:8] self._context = Context.instance() self._socket = self._context.socket(socket_type) self._address = address self._bind_mode = bind_mode
def step1(loop, context): """Step 1""" context = context or Context.instance() # Signal downstream to step 2 sender = context.socket(zmq.PAIR) sender.connect("inproc://step2") msg = b'message from step1' yield from sender.send(msg) print('step1 -- sent msg: {}'.format(msg))
class TickerParser(object): zmqContext = Context.instance() @staticmethod async def execute_parser_request(endpoint, parameters, timeout=5): socket = TickerParser.zmqContext.socket(REQ) socket.connect("tcp://parser:6900") socket.setsockopt(LINGER, 0) poller = Poller() poller.register(socket, POLLIN) await socket.send_multipart([endpoint] + parameters) responses = await poller.poll(timeout * 1000) if len(responses) != 0: response = await socket.recv_multipart() socket.close() return response else: socket.close() raise Exception("time out") return None @staticmethod async def find_exchange(raw, platform, bias): [success, exchange] = await TickerParser.execute_parser_request(b"find_exchange", [raw.encode(), platform.encode(), bias.encode()]) exchange = None if exchange == b"" else loads(exchange) return bool(int(success)), exchange @staticmethod async def match_ticker(tickerId, exchange, platform, bias): exchangeId = exchange.get("id").lower() if bool(exchange) else "" [ticker, error] = await TickerParser.execute_parser_request(b"match_ticker", [tickerId.encode(), exchangeId.encode(), platform.encode(), bias.encode()]) ticker = None if ticker == b"" else loads(ticker) error = None if error == b"" else error.decode() return ticker, error @staticmethod async def check_if_fiat(tickerId): [success, fiat] = await TickerParser.execute_parser_request(b"check_if_fiat", [tickerId.encode()]) fiat = None if fiat == b"" else fiat.decode() return bool(int(success)), fiat @staticmethod async def get_listings(tickerBase, tickerQuote): [listings, total] = await TickerParser.execute_parser_request(b"get_listings", [tickerBase.encode(), tickerQuote.encode()]) return loads(listings), int(total) @staticmethod async def get_formatted_price_ccxt(exchangeId, symbol, price): [response] = await TickerParser.execute_parser_request(b"get_formatted_price_ccxt", [exchangeId.encode(), symbol.encode(), str(price).encode()]) return response.decode() @staticmethod async def get_formatted_amount_ccxt(exchangeId, symbol, amount): [response] = await TickerParser.execute_parser_request(b"get_formatted_amount_ccxt", [exchangeId.encode(), symbol.encode(), str(amount).encode()]) return response.decode()
def __init__(self, address): super(Server, self).__init__() self.address = address self.queue = asyncio.Queue() self.context = Context.instance() self.socket = self.context.socket(zmq.PULL) self.poller = Poller() self._listen_future = None
def __init__(self): super().__init__() self.ctx = Context.instance() #cmd socket is a synchronous socket, we don't want the asyncio context. self.cmd_socket = zmq.Context().socket(zmq.REQ) self.cmd_socket.connect("tcp://127.0.0.1:{}".format( os.environ.get('MODEL_PORT', 12312))) init_vals = self.get_initial_values() undulator_element_list = self.get_undulator_list_from_model() undulator_device_list = [ simulacrum.util.convert_element_to_device(element) for element in undulator_element_list ] for device_name in undulator_device_list: if device_name in init_vals: initial_value = init_vals[device_name] print( f'{device_name} {simulacrum.util.convert_device_to_element(device_name)} {initial_value}' ) und_pvs = { device_name: UndulatorPV(device_name, simulacrum.util.convert_device_to_element(device_name), self.on_undulator_change, initial_values=init_vals[device_name], prefix=device_name) for device_name in undulator_device_list if device_name in init_vals and device_name.startswith('USEG') } phas_pvs = { device_name: phaseShifterPV( device_name, simulacrum.util.convert_device_to_element(device_name), self.on_undulator_change, initial_values=init_vals[device_name], prefix=device_name) for device_name in undulator_device_list if device_name in init_vals and device_name.startswith('PHAS') } dev_name = 'USEG:IN20:466' init_valsH = {'kactH': 1.3852} laser_heater_pvs = { dev_name: LaserHeaterUndulatorPV('USEG:IN20:466', 'LH_UND', self.on_heater_und_change, initial_values=init_valsH, prefix=dev_name) } #laser_heater_pvs = {dev_name: LaserHeaterUndulatorPV('USEG:IN20:466', 'LH_UND', self.on_undulator_change, initial_values=init_valsH , prefix=dev_name)} print(laser_heater_pvs.keys()) print(phas_pvs.keys()) self.add_pvs(laser_heater_pvs) self.add_pvs(phas_pvs) self.add_pvs(und_pvs) L.info("Initialization complete.")
async def _init_input_iface(self, local_port=None, proto='tcp', ip_addr='0.0.0.0', port_min=MIN_PORT_RANGE, port_max=MAX_PORT_RANGE): """Initialize input interface. The ZMQ socket is created and binded for incoming notifications (from node agents). Args: local_port - optional local port to bind to proto - optional protocol for ZMQ socket ip_addr - optional local IP address port_min - optional minimum range port number if local_port has not been defined port_max - optional maximum range port number if local port has not been defined """ self.zmq_ctx = Context.instance() self.in_socket = self.zmq_ctx.socket(zmq.REP) #pylint: disable=maybe-no-member if local_port: address = '{}://{}:{}'.format(proto, ip_addr, local_port) port = self.in_socket.bind(address) else: addr = '{}://{}'.format(proto, ip_addr) port = self.in_socket.bind_to_random_port(addr, min_port=port_min, max_port=port_max) address = '{}:{}'.format(addr, port) self.local_address = address local_hostname = socket.gethostname() if os.getenv('SLURM_CLUSTER_NAME', 'unknown') == 'supermucng': logging.info(f'modifing launcher export address for supermucng') local_opa_hostname = '.'.join( elem + '-opa' if it == 0 else elem for it, elem in enumerate(local_hostname.split('.'))) _logger.info(f'local hostname: {local_hostname}') _logger.info(f'local modified hostname: {local_opa_hostname}') _logger.info( f'local hostname ip address: {socket.gethostbyname(local_hostname)}' ) _logger.info( f'local modified hostname ip address: {socket.gethostbyname(local_opa_hostname)}' ) local_hostname = local_opa_hostname self.local_export_address = '{}://{}:{}'.format( proto, socket.gethostbyname(local_hostname), port) _logger.debug('local zmq address accessible by other machines: %s', self.local_export_address) self.iface_task = asyncio.ensure_future(self._input_iface())
async def connect_zmq(): context = Context.instance() # Socket to talk to server current_app.flowapi_logger.debug("Connecting to FlowMachine server…") socket = context.socket(zmq.REQ) socket.connect( f"tcp://{current_app.config['FLOWMACHINE_SERVER']}:{current_app.config['FLOWMACHINE_PORT']}" ) request.socket = socket current_app.flowapi_logger.debug("Connected.")
def __init__(self): super().__init__() bpm_pvs = {device_name: BPMPV(prefix=device_name) for device_name in simulacrum.util.device_names if device_name.startswith("BPM")} self.add_pvs(bpm_pvs) self.ctx = Context.instance() #cmd socket is a synchronous socket, we don't want the asyncio context. self.cmd_socket = zmq.Context().socket(zmq.REQ) self.cmd_socket.connect("tcp://127.0.0.1:{}".format(os.environ.get('MODEL_PORT', 12312))) self.orbit = self.initialize_orbit() print("Initialization complete.")
def __init__(self): super().__init__() self.ctx = Context.instance() #cmd socket is a synchronous socket, we don't want the asyncio context. self.cmd_socket = zmq.Context().socket(zmq.REQ) self.cmd_socket.connect("tcp://127.0.0.1:{}".format(os.environ.get('MODEL_PORT', 12312))) init_vals = self.get_cavity_ACTs_from_model() cav_pvs = {device_name: CavityPV(self.on_cavity_change, initial_values=init_vals[device_name], prefix=device_name) for device_name in init_vals.keys()} self.add_pvs(cav_pvs) print("Initialization complete.")
async def main(): options.parse_command_line() if (options.debug): async_debug(asyncio.get_event_loop()) context = Context.instance() socket = context.socket(zmq.SUB) socket.connect("tcp://localhost:%s" % options.zport) socket.subscribe(b'') app.listen(options.port) await recv(socket)
def run(loop): context = Context.instance() client = context.socket(zmq.ROUTER) client.bind("tcp://*:5671") tasks = [ asyncio.ensure_future(worker_task(idx)) for idx in range(NBR_WORKERS) ] tasks.append(asyncio.ensure_future(requestor(client))) loop.run_until_complete(asyncio.wait(tasks)) for task in tasks: print('result: {}'.format(task.result()))
def __init__(self): self.ctx = Context.instance() target_set = {'build_queue'} self.socket = self.ctx.socket(zmq.SUB) self.socket.connect(OUT) for target in target_set: self.socket.setsockopt(zmq.SUBSCRIBE, target.encode('utf-8')) self.queue = asyncio.Queue() self.dont_build = set()
def __init__(self): self.ctx = Context.instance() target_set = {'build_queue'} self.socket = self.ctx.socket(zmq.SUB) self.socket.connect(OUT) for target in target_set: self.socket.setsockopt(zmq.SUBSCRIBE, target.encode('utf-8')) self.queue = asyncio.Queue() self.dont_build = set()
def run(loop): context = Context.instance() client = context.socket(zmq.ROUTER) client.bind("tcp://*:5671") tasks = [ asyncio.ensure_future(worker_task(idx)) for idx in range(NBR_WORKERS) ] tasks.append(asyncio.ensure_future(requestor(client))) loop.run_until_complete(asyncio.wait(tasks)) for task in tasks: print('result: {}'.format(task.result()))
def __init__(self): self.class_path = os.getenv('AGENT_CLS_PATH') if not self.class_path: raise Exception( "Yoy must specify environment variable AGENT_CLS_PATH") self.url = os.getenv('SERVER_URL', '127.0.0.1') self.port = os.getenv('SERVER_PORT', '4181') self.url = "tcp://{}:{}".format(self.url, self.port) self.my_name = os.getenv('AGENT_NAME', '<no name>') self.ctx = Context.instance() self.connection_uid = str(uuid.uuid4())
def main(): loop = asyncio.get_event_loop() if (options.debug): async_debug(loop) context = Context.instance() socket = context.socket(zmq.SUB) socket.connect("tcp://localhost:%s" % options.port) socket.subscribe(b'') loop.run_until_complete(recv(socket)) loop.close()
def run(loop): context = Context.instance() client = context.socket(zmq.ROUTER) client.bind(CONNECTION_ADDRESS) tasks = [ asyncio.ensure_future(worker_a(context)), asyncio.ensure_future(worker_b(context)), asyncio.ensure_future(dealer(client)), ] loop.run_until_complete(asyncio.wait(tasks)) for task in tasks: print("result: {}".format(task.result()))
def step2(loop, context): """Step 2""" context = context or Context.instance() # Bind to inproc: endpoint, then start upstream thread receiver = context.socket(zmq.PAIR) receiver.bind("inproc://step2") # Wait for signal printdbg('(step2) waiting for signal') msg = yield from receiver.recv() print('step2 -- received msg: {}'.format(msg)) # Signal downstream to step 3 sender = context.socket(zmq.PAIR) sender.connect("inproc://step3") msg = b'message from step2' yield from sender.send(msg) print('step2 -- sent msg: {}'.format(msg))
def __init__(self, url): self._url = url self._ctx = Context.instance() self._socket = self._ctx.socket(zmq.DEALER) self._socket.identity = uuid.uuid4().hex.encode()[0:16] self._msg_router = _MessageRouter() self._receiver = _Receiver(self._socket, self._msg_router) self._sender = _Sender(self._socket, self._msg_router) self._recv_task = None # Monitoring properties self._monitor_sock = None self._monitor_fd = None self._monitor_task = None
def __init__(self, url='127.0.0.1', port='5555'): # get ZeroMQ version print("Current libzmq version is %s" % zmq.zmq_version()) print("Current pyzmq version is %s" % zmq.__version__) self.url = "tcp://{}:{}".format(url, port) # pub/sub and dealer/router self.ctx = Context.instance() # init hello world publisher obj self.hello_world = HelloWorld() # activate publishers / subscribers asyncio.get_event_loop().run_until_complete(asyncio.wait([ self.hello_world_pub(), self.hello_world_sub(), self.lang_changer_router(), # less restrictions than REP self.lang_changer_dealer(), # less restrictions than REQ ]))
def worker_task(id, context=None): context = context or Context.instance() worker = context.socket(zmq.REQ) # We use a string identity for ease here zhelpers.set_id(worker) worker.connect("tcp://localhost:5671") total = 0 while True: # Tell the router we're ready for work yield from worker.send(b"ready") # Get workload from router, until finished workload = yield from worker.recv() #print('worker {} received: {}'.format(id, workload)) finished = workload == b"END" if finished: print("worker %d processed: %d tasks" % (id, total)) break total += 1 # Do some random work yield from asyncio.sleep(0.1 * random.random()) return ('worker {}'.format(id), total)
def run(): ''' Run Ironhouse example ''' # These directories are generated by the generate_certificates script base_dir = os.path.dirname(__file__) keys_dir = os.path.join(base_dir, 'certificates') public_keys_dir = os.path.join(base_dir, 'public_keys') secret_keys_dir = os.path.join(base_dir, 'private_keys') if not (os.path.exists(keys_dir) and os.path.exists(public_keys_dir) and os.path.exists(secret_keys_dir)): logging.critical("Certificates are missing - run generate_certificates.py script first") sys.exit(1) ctx = Context.instance() # Start an authenticator for this context. auth = AsyncioAuthenticator(ctx) auth.start() auth.allow('127.0.0.1') # Tell authenticator to use the certificate in a directory auth.configure_curve(domain='*', location=public_keys_dir) server = ctx.socket(zmq.PUSH) server_secret_file = os.path.join(secret_keys_dir, "server.key_secret") server_public, server_secret = zmq.auth.load_certificate(server_secret_file) server.curve_secretkey = server_secret server.curve_publickey = server_public server.curve_server = True # must come before bind server.bind('tcp://*:9000') client = ctx.socket(zmq.PULL) # We need two certificates, one for the client and one for # the server. The client must know the server's public key # to make a CURVE connection. client_secret_file = os.path.join(secret_keys_dir, "client.key_secret") client_public, client_secret = zmq.auth.load_certificate(client_secret_file) client.curve_secretkey = client_secret client.curve_publickey = client_public server_public_file = os.path.join(public_keys_dir, "server.key") server_public, _ = zmq.auth.load_certificate(server_public_file) # The client must know the server's public key to make a CURVE connection. client.curve_serverkey = server_public client.connect('tcp://127.0.0.1:9000') yield from server.send(b"Hello") if (yield from client.poll(1000)): msg = yield from client.recv() if msg == b"Hello": logging.info("Ironhouse test OK") else: logging.error("Ironhouse test FAIL") # close sockets server.close() client.close() # stop auth task auth.stop()
"""Example using zmq with asyncio coroutines""" # Copyright (c) PyZMQ Developers. # This example is in the public domain (CC-0) import time import zmq from zmq.asyncio import Context, Poller import asyncio url = 'tcp://127.0.0.1:5555' ctx = Context.instance() async def ping(): """print dots to indicate idleness""" while True: await asyncio.sleep(0.5) print('.') async def receiver(): """receive messages with polling""" pull = ctx.socket(zmq.PULL) pull.connect(url) poller = Poller() poller.register(pull, zmq.POLLIN) while True: events = await poller.poll() if pull in dict(events):