1

I'm having an odd issue. I develop a web-interface for my discord bot, and am using ZeroMQ for communication between the bot's process and the fastAPI process. My program is structured so the fastAPI process sends a ZeroMQ REQ. My discord bot is acting as the "server" since it is providing an information and fastAPI process is acting as the client. This specific method is attempting to have my discord bot fetch online members and then send them back to the requester. Here is my code:

Server side : ( irrelevant parts committed for concision )

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())

Client Side :

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()

The error message I receive is located on the server program (no issues running the "client" program). The error is as follows :

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 return 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

My intuition makes me think this is an issue with the socket declaration on the server side, but I have no issues declaring the socket using the exact same syntax on the client side.

user3666197
  • 1
  • 6
  • 50
  • 92

1 Answers1

1

Q : "How do I properly declare socket type in ZeroMQ python?"

Declaration is not your problem, the either side of the REQ/REP ZeroMQ Formal Behaviour Archetype was correctly instantiated, using the .socket( { zmq.REP | zmq.REQ } ) method with either respective Archetype-type.

Your problem is with managing the AccessPoint ISO-OSI-L3 definition, for the selected tcp:// Transport Class.

Here, one side of any of the known ZeroMQ Archetype kinds, has to .bind(), whereas the other has to .connect(). No exception, no excuse.

So make either one of your REQ/REP Archetype do a .bind() and let the other .connect() and your setup will work. No other conditions here.

halfer
  • 19,824
  • 17
  • 99
  • 186
user3666197
  • 1
  • 6
  • 50
  • 92