添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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:
            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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.