我有一个从另一个stackoverflow帖子中获取的代码片段 Python Workers and Queues
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 :(得分:0)
发现我的错误。我不知道有两个队列。
改变
from multiprocessing import Process
from Queue import Queue
到
from multiprocessing import Process,Queue
现在我有了预期的行为