[Python] ZeroMQ 'Restart' if no reply was recieved

Discussão em 'Python' iniciado por Stack, Fevereiro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I have a python program which sends height data from my client to my server program. My server program will not be always running so if I don't recieve a response I would like it to try again.

    So far what I have is that if a response (from the server) is not given in 20 Seconds it causes an exception and recalls my input. It works fine until I try it a second time.

    Here is my code:

    import zmq
    from time import sleep

    global radius
    context = zmq.Context()
    print("Remote Deployment Application")
    print("Lightweight ZMQ Communication")
    print("Connecting to Desk Ctrl Service")
    socket = context.socket(zmq.REQ)
    socket.connect("tcp://192.168.1.9:5555")
    socket.setsockopt(zmq.RCVTIMEO, 30000)
    socket.setsockopt(zmq.LINGER, 0)


    # Do 10 requests,waiting each time for a response
    def __init__(self, height):
    self.height = height

    def start():
    global height
    height = input("Enter in request: ")
    SendHeightVal()


    def SendHeightVal():
    global userinput
    global height
    print("Sent Request. Awaiting Reply.")
    so_bytes = height.encode()
    socket.send(so_bytes)
    so_bytes = 0
    try:
    message = socket.recv()
    except:
    print("Something went wrong. No response from server!")
    message = None
    start()
    print(message)
    start()


    Here is the error:

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "client.py", line 44, in <module>
    start()
    File "client.py", line 22, in start
    SendHeightVal()
    File "client.py", line 37, in SendHeightVal
    start()
    File "client.py", line 22, in start
    SendHeightVal()
    File "client.py", line 30, in SendHeightVal
    socket.send(so_bytes)
    File "/usr/local/lib/python3.6/site-packages/zmq/sugar/socket.py", line 391, in send
    return super(Socket, self).send(data, flags=flags, copy=copy, track=track)
    File "zmq/backend/cython/socket.pyx", line 727, in zmq.backend.cython.socket.Socket.send
    File "zmq/backend/cython/socket.pyx", line 774, in zmq.backend.cython.socket.Socket.send
    File "zmq/backend/cython/socket.pyx", line 249, in zmq.backend.cython.socket._send_copy
    File "zmq/backend/cython/socket.pyx", line 244, in zmq.backend.cython.socket._send_copy
    File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc
    zmq.error.ZMQError: Operation cannot be accomplished in current state

    Continue reading...

Compartilhe esta Página