2
我從另一個計算器後 Python Workers and QueuesPython的多處理和隊列
from multiprocessing import Process
from Queue import Queue
class Worker(Process):
def __init__(self, queue):
super(Worker, self).__init__()
self.queue= queue
def run(self):
print 'Worker started'
# do some initialization here
print 'Computing things!'
for data in iter(self.queue.get, None):
print(data)
if __name__ == '__main__':
request_queue = Queue()
for i in range(4):
Worker(request_queue).start()
for data in range(100):
request_queue.put(data)
# Sentinel objects to allow clean shutdown: 1 per worker.
for i in range(4):
request_queue.put(None)
爲什麼這個過程中掛起,不處理隊列內容採取的代碼片段?
對我的作品!,有什麼錯誤? –
不應該打印出0-99的數字嗎?我沒有得到打印出來的數字,它掛在線上--- (it.self.queue.get,None) 就像在迭代隊列上迭代一樣,但是由於新數據在迭代後到達它沒有看到新的數據。 –