Python 非忙着等待ZeroMQ?

Python 非忙着等待ZeroMQ?,python,zeromq,Python,Zeromq,我有一些使用ZeroMQ库的简单PUB/SUB代码。本质上就是无限期地等待消息返回 然而,这似乎是忙碌的等待,因为在等待接收消息时,pythons的CPU使用率显著提高 有更好的方法吗?我想象这就像在一个套接字上调用receive一样,它不会不断地执行指令和浪费CPU import sys import zmq port = "5556" if len(sys.argv) > 1: port = sys.argv[1] int(port) # Socket to ta

我有一些使用ZeroMQ库的简单
PUB/SUB
代码。本质上就是无限期地等待消息返回

然而,这似乎是忙碌的等待,因为在等待接收消息时,pythons的CPU使用率显著提高

有更好的方法吗?我想象这就像在一个套接字上调用receive一样,它不会不断地执行指令和浪费CPU

import sys
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
for update_nbr in range (5):
    string = socket.recv()
    topic, messagedata = string.split()
    total_value += int(messagedata)
    print ('{} {}'.format(topic, message)
Q:“正在忙着等待ZeroMQ?”

当然


你是说CPU使用率很高吗?我尝试了你的代码,在我的追逐中,它几乎没有使用0.5%的CPU。
...
for update_nbr in range( 5 ):
    print "{0:} A hunt for message started".format( time.ctime() )
    while True: # .............................................. poll() + wait
          if ( 0 == socket.poll( 1000 ) ):
               print "\n.",
          else:
               break # we've got a message .....................
    print "\n{0:} A message came".format( time.ctime() )
    #_________________________________________
    try:
        string = socket.recv( zmq.NOBLOCK )
        topic, messagedata = string.split()
        total_value += int( messagedata )
        print '{} {}'.format( topic, message )

    except:
        print 'EXC:...'

    finally:
        pass
    #_________________________________________