2013-09-16 29 views
1

返回我有下面的代碼片段,使用多模塊:如何使queue.get儘可能快

import time 

from multiprocessing import Process, Queue 

q = Queue() 

def feeder(): 
    x=0 
    while True: 
     q.put(x) 
     time.sleep(1) 
     x+=1 

p=Process(target=feeder).start() 

while True: 
    print q.get() 

現在q.get打印每秒,爲新的數據到達隊列。我希望q.get儘可能快地返回(即通過提出錯誤或通過返回無或通過其他方法)。

回答

1

你看過the documentation for .get嗎?

while True: 
    try: 
     print q.get(block=False) # or q.get_nowait() 
    except Queue.Empty: 
     pass 
+0

是的,我不知道我怎麼能錯過它。我想我專注於這些例子。謝謝 – yemu

相關問題