ZMQ 经销商没有收到带有 asyncio 的消息
ZMQ dealer does not receive message with asyncio
我正在使用 pyzmq 经销商套接字和提供的异步功能为 Hyperledger Sawtooth 事件设置侦听器。当前返回 futures 但有时只完成,即使消息发送到套接字。
奇怪的是,这适用于连接消息(仅当如下所示在它之前休眠时)但不适用于事件消息。我已经使用 JavaScript 实现了它并且它可以正常工作。看来问题不在于 Sawtooth,而在于 pyzmq 的 asyncio 功能实现或我的代码。
class EventListener:
def __init__(self):
...
ctx = Context.instance()
self._socket = ctx.socket(zmq.DEALER)
self._socket.connect("tcp://127.0.0.1:4004")
async def subscribe(self):
...
await self._socket.send_multipart([connection_msg])
async def receive(self):
# Necessary wait otherwise the future is never finished
await asyncio.sleep(0.1)
resp = await self._socket.recv_multipart()
handle_response(resp)
async def listen(self):
while True:
# here sleep is not helping
# await asyncio.sleep(0.1)
# follwing await is never finished
resp = await self._socket.recv_multipart()
handle_response(resp)
...
listener = listener.EventListener()
await asyncio.gather(
listener.receive(), listener.subscribe())
await asyncio.create_task(listener.listen())
...
调试显示返回的 Future 对象从未从 pending 状态更改为 finished 状态。那么,我的代码是否不正确,我是否需要以不同的方式等待消息,或者 pyzmq 的 asyncio 功能是否有问题?另外,为什么我需要睡在 receive()
,这不是我们有 asyncio 的原因吗?
查询太多,此答案可能无法解决所有问题。希望至少这会帮助其他人寻找设置事件侦听器的方法。
Hyperledger Sawtooth python SDK 为客户端提供订阅事件的选项。可以在 https://github.com/hyperledger/sawtooth-sdk-python/blob/master/sawtooth_sdk/messaging/stream.py
找到执行您要执行的操作的代码的 SDK 部分
可以在此处找到使用 Hyperledger Sawtooth python SDK 进行事件订阅的示例代码 https://github.com/danintel/sawtooth-cookiejar/blob/master/events/events_client.py
我正在使用 pyzmq 经销商套接字和提供的异步功能为 Hyperledger Sawtooth 事件设置侦听器。当前返回 futures 但有时只完成,即使消息发送到套接字。
奇怪的是,这适用于连接消息(仅当如下所示在它之前休眠时)但不适用于事件消息。我已经使用 JavaScript 实现了它并且它可以正常工作。看来问题不在于 Sawtooth,而在于 pyzmq 的 asyncio 功能实现或我的代码。
class EventListener:
def __init__(self):
...
ctx = Context.instance()
self._socket = ctx.socket(zmq.DEALER)
self._socket.connect("tcp://127.0.0.1:4004")
async def subscribe(self):
...
await self._socket.send_multipart([connection_msg])
async def receive(self):
# Necessary wait otherwise the future is never finished
await asyncio.sleep(0.1)
resp = await self._socket.recv_multipart()
handle_response(resp)
async def listen(self):
while True:
# here sleep is not helping
# await asyncio.sleep(0.1)
# follwing await is never finished
resp = await self._socket.recv_multipart()
handle_response(resp)
...
listener = listener.EventListener()
await asyncio.gather(
listener.receive(), listener.subscribe())
await asyncio.create_task(listener.listen())
...
调试显示返回的 Future 对象从未从 pending 状态更改为 finished 状态。那么,我的代码是否不正确,我是否需要以不同的方式等待消息,或者 pyzmq 的 asyncio 功能是否有问题?另外,为什么我需要睡在 receive()
,这不是我们有 asyncio 的原因吗?
查询太多,此答案可能无法解决所有问题。希望至少这会帮助其他人寻找设置事件侦听器的方法。
Hyperledger Sawtooth python SDK 为客户端提供订阅事件的选项。可以在 https://github.com/hyperledger/sawtooth-sdk-python/blob/master/sawtooth_sdk/messaging/stream.py
找到执行您要执行的操作的代码的 SDK 部分可以在此处找到使用 Hyperledger Sawtooth python SDK 进行事件订阅的示例代码 https://github.com/danintel/sawtooth-cookiejar/blob/master/events/events_client.py