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) 就像在迭代队列上迭代一样,但是由于新数据在迭代后到达它没有看到新的数据。 –