代码:
# -*- coding: UTF-8 -*-
from multiprocessing import Process, JoinableQueue
import os
class WebPage(Process):
def __init__(self, queue):
super(WebPage, self).__init__()
self.queue = queue
def run(self):
print 'process begin'
while not self.queue.empty():
data = self.queue.get()
print data
self.queue.task_done()
print 'process over:%s' % os.getpid()
class WebPage2(Process):
def __init__(self, queue):
super(WebPage2, self).__init__()
self.queue = queue
def run(self):
print 'process begin'
for data in iter(self.queue.get, None):
print data
print 'process over:%s' % os.getpid()
def run():
queue = JoinableQueue()
for news_id in range(10):
queue.put('put:%s'%news_id)
worker_list = list()
for i in range(2):
worker = WebPage(queue)
worker_list.append(worker)
worker.start()
queue.join()
def run2():
queue = JoinableQueue()
worker_list = list()
for i in range(5):
worker = WebPage(queue)
worker_list.append(worker)
worker.start()
queue.join()
for news_id in range(10):
queue.put('put:%s' % news_id)
for i in range(5):
queue.put(None)
for w in worker_list:
w.join()
if __name__=="__main__":
run2()
run2可以更简单的控制队列的长度,run1实现起来比较麻烦。