python多进程队列的2种实现方法

代码:

# -*- 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实现起来比较麻烦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值