async def pusher(address): ctx = zmq.Context() sock = ctx.socket(zmq.PUSH) sock.bind(address) for n in range(100): await sock.send(b'Message %d' % n) await sock.send(b'exit')
async def puller(address): ctx = zmq.Context() sock = ctx.socket(zmq.PULL) sock.connect(address) while True: msg = await sock.recv() if msg == b'exit': break print('Got:', msg)
def __init__(self, zync_url, udp_ctrl_url, fs): context = zmq.Context() self.zclient = ZClientCurioBase(zync_url, zmq=zmq, context=context) self.udp_client = UClientCurio(udp_ctrl_url, zmq=zmq, context=context) super().__init__(fs=fs) self.acquire_channel = ChannelGeRMAcquireUDP( value=0, zclient=self.zclient, uclient=self.udp_client, parent=self)
async def rpc_server(address): ctx = zmq.Context() sock = ctx.socket(zmq.REP) sock.bind(address) while True: func, args, kwargs = await sock.recv_pyobj() try: result = func(*args, **kwargs) await sock.send_pyobj(result) except Exception as e: await sock.send_pyobj(e)
async def client(address): # Run a background task to make sure the message passing operations don't block await spawn(ticker, daemon=True) # Compute the first 40 fibonacci numbers ctx = zmq.Context() sock = ctx.socket(zmq.REQ) sock.connect(address) for n in range(1, 40): await sock.send_pyobj((fib, (n, ), {})) result = await sock.recv_pyobj() print(n, result)
async def loop_zmq(msg_queue): ctx = zmq.Context() sock = ctx.socket(zmq.SUB) sub_url = get_connect_url("sub") sock.connect(sub_url) sock.subscribe("obs-log") while True: print("waiting") _ = await sock.recv() topic, messagedata = _.split(b"\x00") if messagedata == b'exit': break r = json.loads(json.loads(messagedata)["msg"]) await msg_queue.put(r)
# A Curio implementation of the zmq tests posted at: # https://github.com/achimnol/asyncio-zmq-benchmark import time import os from curio import * import curio.zmq as zmq ctx = zmq.Context() async def pushing(): server = ctx.socket(zmq.PUSH) server.bind('tcp://*:9000') for i in range(5000): await server.send(b'Hello %d' % i) await server.send(b'exit') async def pulling(): client = ctx.socket(zmq.PULL) client.connect('tcp://127.0.0.1:9000') with open(os.devnull, 'w') as null: while True: greeting = await client.recv_multipart() if greeting[0] == b'exit': break print(greeting[0], file=null) async def main():