如何在 ZeroMQ python 中正确声明套接字类型?

凯文普瑞甘2

我有一个奇怪的问题。我为我的 discord bot 开发了一个 web 界面,并使用 ZeroMQ 在 bot 进程和 fastAPI 进程之间进行通信。我的程序是结构化的,因此 fastAPI 进程会发送一个 ZeroMQ REQ我的 discord bot 充当“服务器”,因为它提供信息,而 fastAPI 进程充当客户端。这个特定的方法试图让我的不和谐机器人获取在线成员,然后将它们发送回请求者。这是我的代码:

服务器端:( 为了简洁,不相关部分)

import asyncio

import zmq
import zmq.asyncio


async def openWebServerInterface():
    zmqctx = zmq.asyncio.Context()
    s = zmqctx.socket(zmq.REP)
    s.connect('tcp://127.0.0.1:1234')
    print('waiting on request...')
    request = await s.recv_string()
    if request == "gimmethedata":
        print("sending online people")
        s.send_string(await returnOnline())
    print("data request sent!")
    s.close()

async def returnOnline():
    message = "this will return my online members"
    return message

asyncio.run(openWebServerInterface())

客户端 :

async def listenForResponse():
    await openBotConnection()
    print("logo interface opened!")
    r = zmqctx.socket(zmq.REP)
    print("socket connected, waiting for response")
    reply = await r.recv_string()
    print(reply)
    return reply

async def openBotConnection():
    s = zmqctx.socket(zmq.REQ)
    s.connect('tcp://127.0.0.1:1234')
    await s.send_string("gimmethedata")
    print("data request sent!")
    s.close()

@app.get("/")
async def root():
    print("listing for logo's response!")
    return await listenForResponse()

我收到的错误消息位于服务器程序上(运行“客户端”程序没有问题)。错误如下:

Traceback (most recent call last):
File "E:/DiscordBotContainer/ServerSideTestFile", line 23, in <module> asyncio.run(openWebServerInterface()) File "*\AppData\Local\Programs\Python\Python38\lib\asyncio\runners.py", line 43, in run return loop.run_until_complete(main) File "*\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete 返回 future.result() File "E:/DiscordBotContainer/fuck it", line 9, in openWebServerInterface s = zmqctx.socket(zmq.REP) File "*\AppData\Local\Programs\Python\Python38\lib\site-packages\zmq\sugar\context.py", line 204, in socket s = self._socket_class(self, socket_type, **kwargs) File "*\AppData\Local\Programs\Python\Python38\lib\site-packages\zmq\_future.py", line 144, in __init__ self._init_io_state() File "*\AppData\Local\Programs\Python\Python38\lib\site-packages\zmq\asyncio\__init__.py", line 53, in _init_io_state self.io_loop.add_reader(self._fd, lambda : self._handle_events(0, 0)) File "*\AppData\Local\Programs\Python\Python38\lib\asyncio\events.py", line 501, in add_reader raise NotImplementedError

我的直觉让我认为这是服务器端套接字声明的问题,但我在客户端使用完全相同的语法声明套接字没有问题。

用户3666197

“如何在 ZeroMQ python 中正确声明套接字类型?”

声明不是您的问题,REQ/REPZeroMQ 正式行为原型的任一侧都已正确实例化,使用.socket( { zmq.REP | zmq.REQ } )具有各自原型类型的方法。

您的问题在于管理所选tcp://传输类的 AccessPoint ISO-OSI-L3 定义

在这里,任何已知 ZeroMQ 原型类型的一侧必须.bind(),而另一侧必须.connect()没有例外,没有借口。

因此,让您的REQ/REPArchetype 中的任何一个做一个.bind(),让另一个让.connect()您的设置工作。这里没有其他条件。


如果您从未使用过 ZeroMQ,
可以在这里先看看不到五秒的ZeroMQ原则”,
然后再深入了解更多细节



本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 cpp 类中声明 zeromq 套接字

如何使用ZeroMQ实现多个套接字?

如何在Python中正确调用套接字模块

ZeroMQ:如何将Poller中使用的pollitem_t项目转换回ZeroMQ套接字?

如何知道ZeroMQ套接字是否已准备好?

PyZMQ(ZeroMQ)-如何从SUB套接字获取订阅密钥?

如何通过ZeroMQ套接字发送OpenCV视频素材?

ZeroMQ用于环形拓扑-如何配置套接字?

如何使用ZeroMQ通过TCP套接字发送PNG图像?

如何在Erlang或Elixir中使用ZeroMQ实现无阻塞套接字接收?

如何在ZeroMQ套接字中检索和存储随机UUID?

了解高级ZeroMQ套接字类型

如何使用cppzmq从ROUTER套接字向特定的DEALER套接字发送ZeroMQ消息?

如何在TypeScript中正确要求并声明节点库类型?

ZeroMQ在context.close()中被阻止。如何在C ++中安全关闭套接字和上下文?

ZeroMQ在套接字上将ZMQError抛出绑定到ipc://协议地址(python)

如何在要应用于熊猫数据框的python函数中正确声明'NaT'?

如何在 Python 中正确编码?

如何在Python中正确写入FIFO?

如何在 Python 中正确执行 eval?

如何在python中正确使用FlatBuffers?

如何在python中正确使用json

如何在python中正确使用断言?

如何在python中正确使用队列?

如何在python中正确使用'or'和'and'

如何在python中正确复制列表

如何在 Python 中正确编写 .onclick()?

如何在 Python 中正确使用 import

进程间通信使用哪种ZeroMQ套接字类型?