我有一个奇怪的问题。我为我的 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
我的直觉让我认为这是服务器端套接字声明的问题,但我在客户端使用完全相同的语法声明套接字没有问题。
问:“如何在 ZeroMQ python 中正确声明套接字类型?”
声明不是您的问题,REQ/REP
ZeroMQ 正式行为原型的任一侧都已正确实例化,使用.socket( { zmq.REP | zmq.REQ } )
具有各自原型类型的方法。
您的问题在于管理所选tcp://
传输类的 AccessPoint ISO-OSI-L3 定义。
在这里,任何已知 ZeroMQ 原型类型的一侧必须.bind()
,而另一侧必须.connect()
。没有例外,没有借口。
因此,让您的REQ/REP
Archetype 中的任何一个做一个.bind()
,让另一个让.connect()
您的设置工作。这里没有其他条件。
如果您从未使用过 ZeroMQ,
可以在这里先看看“不到五秒的ZeroMQ原则”,
然后再深入了解更多细节
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句