生产者消费者模型

一:多线程实现

# -*- coding:utf-8 -*-
import time
from queue import Queue
from threading import Thread

def producer(name,q):
    # 生产5个
    for i in range(1,6):
        # 传输延迟
        time.sleep(0.5)
        data = 'data-%s'%i
        q.put(data)
        print('生产者 %s 生产了数据:%s'%(name,data))

def consumer(name,q):
    while 1:
        data = q.get()
        if data is None:
            break
        print('消费者 %s 收到数据:%s'%(name,data))
        # 处理数据延迟
        time.sleep(0.5)

if __name__ == '__main__':
    # 队列对象
    q = Queue()
    # 3个生产者5个消费者
    pro_name = ['whw','www','wan']
    con_name = ['aaa','bbb','ccc','ddd','eee']
    pro_lis = []
    # 建立 开启线程
    for i in pro_name:
        t = Thread(target=producer,args=(i,q))
        pro_lis.append(t)
        # 记得start!
        t.start()
    for i in con_name:
        t = Thread(target=consumer,args=(i,q))
        # 记得start
        t.start()
    # 等生产者生产完
    for i in pro_lis:
        i.join()
    # 往队列中添加消费者数量的None
    for i in range(len(con_name)):
        q.put(None)

效果:

二:多进程实现

# -*- coding:utf-8 -*-
import random
import time
from multiprocessing import Process,Queue

def producer(name,q):
    for i in range(1,7):
        # 接收数据延迟
        time.sleep(random.random())
        data = 'data-%s'%i
        q.put(data)
        print('生产者 %s 生产了数据:%s'%(name,data))

def consumer(name,q):
    while 1:
        data = q.get()
        if data is None:
            print('\033[31;1m %s消费完了 \033[0m' %name)
            break
        print('消费者 %s 收到数据:%s'%(name,data))
        # 处理数据延迟
        time.sleep(random.uniform(0,1))

if __name__ == '__main__':
    # Queue对象
    q = Queue()

    # 3个生产者 4个消费者
    pro_name = ['whw','naruto','sasuke']
    con_name = ['aaa','bbb','ccc','ddd']
    # 生产者对象列表
    pro_lis = []

    # 开进程 加对象
    for i in pro_name:
        p = Process(target=producer,args=(i,q))
        p.start()
        pro_lis.append(p)

    for i in con_name:
        p = Process(target=consumer,args=(i,q))
        p.start()

    # 等待生产者结束
    for i in pro_lis:
        i.join()

    # 往队列添加消费者数量的None
    for i in range(len(con_name)):
        q.put(None)

效果:

 

posted on 2019-04-18 20:22  江湖乄夜雨  阅读(168)  评论(0编辑  收藏  举报