zero-mq: socket.recv() call is blocking

后端 未结 1 657
执念已碎
执念已碎 2021-01-05 22:12

I am trying to use zero-mq.My requirement is very simple.I want to be able to communicate between two peers in a network.I came across this program in the examples in the bo

1条回答
  •  爱一瞬间的悲伤
    2021-01-05 22:37

    zmq.Socket.recv will not block if you pass the zmq.NOBLOCK flag parameter.

    The docs say:

    If NOBLOCK is set, this method will raise a ZMQError with EAGAIN if a message is not ready.
    

    zmq will queue messages that it receives and one message will be returned for each recv() call until this queue is exhausted after which ZMQError is raised.

    zmq.Again used in the exmaples below is a wrapper for zmq.EAGAIN.

    For example:

    while True:
        try:
            #check for a message, this will not block
            message = socket.recv(flags=zmq.NOBLOCK)
    
            #a message has been received
            print "Message received:", message
    
        except zmq.Again as e:
            print "No message received yet"
    
        # perform other important stuff
        time.sleep(10)
    

    The sub_client.py example could perhaps be written to use non-blocking behaviour like this:

    import sys, time
    import zmq
    
    port = "5556"
    if len(sys.argv) > 1:
        port =  sys.argv[1]
        int(port)
    
    # Socket to talk to server
    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    
    print "Collecting updates from weather server..."
    socket.connect ("tcp://localhost:%s" % port)
    
    # Subscribe to zipcode, default is NYC, 10001
    topicfilter = "10001"
    socket.setsockopt(zmq.SUBSCRIBE, topicfilter)
    
    # Process 5 updates
    total_value = 0
    received_value_count = 0
    do_receive_loop = True
    while do_receive_loop:
        try:
            #process all messages waiting on subscribe socket
            while True:
                #check for a message, this will not block
                string = socket.recv(flags=zmq.NOBLOCK)
    
                #message received, process it
                topic, messagedata = string.split()
                total_value += int(messagedata)
                print ('{} {}'.format(topic, messagedata))
    
                #check if we have all the messages we want
                received_value_count += 1
                if received_value_count > 4:
                    do_receive_loop = False
                    break
    
        except zmq.Again as e:
            #No messages waiting to be processed
            pass
    
        #Here we can do other stuff while waiting for messages
        #contemplate answer to 'The Last Question'
        time.sleep(15)
        print "INSUFFICIENT DATA FOR MEANINGFUL ANSWER"
    
    print('Avg data value for topic {} was {}'.format(topicfilter, (total_value/5)))
    

    0 讨论(0)
提交回复
热议问题