Search code examples
zeromqpyzmq

Set timeout ZMQ - with a condition


I am working on a client-server app - with several clients. A process creates jobs, and initiate them. The server waits on a ZMQ socket to these jobs answers.

The problem:

Currently I am working with 6 jobs, and I want to receive an answer from at least 4. After 4 answers were received - I want to wait 2 more seconds and if didn't get any more results - process the results I got, and then return to listen on the socket.

My thoughts:

I have seen several ways (Poll, ZMQ_CONNECT_TIMEOUT etc.), but I couldn't figure a way to use it in my case.

I thought of initiating another process in the server once 4 jobs were done, which goes to sleep for 2 seconds and then sends a SIGSTP, but I'm afraid it will affect the server's return to listen on the socket.

Any suggestions?


Solution

  • Well, no one answered, but I found a solution that works for me.

    Posting cause it might help others.

    What I did is as simple as it gets:

     while active_clients > 0:
    
        # check if we already have MIN_REQ sites
        if requirement:
            try:
                time.sleep(10)   # give another chance to the last client
                message = socket.recv_pyobj(flags=zmq.NOBLOCK)  # peek to see if received a msg
                results.append(message)
                break
    
            except zmq.Again as e:
                break
    
        else:
            #  Wait for next response from client
            message = socket.recv_pyobj()
            results.append(message)
            
            active_clients -= 1
        # Send reply back to client
        socket.send_pyobj(b"ok")
    

    Notice the if else pattern

    If requirement (which can be any requirement you'd like) is fulfilled - just open a Non-Blocking socket.

    You could also remove the break statement, and enter another time to loop.