码迷,mamicode.com
首页 > 编程语言 > 详细

python 之队列

时间:2018-03-09 13:14:41      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:rand   done   join   +=   span   容器   col   模块   cer   

 

进程和线程模块下都有队列类。

JoinableQueue示例:

import time,random
from multiprocessing import Process,JoinableQueue


def producer(name,q):
    count= 0
    while count<3:
        print(making,,,,)
        time.sleep(2)
        q.put(count)
        print(Producer %s has produced %s baozi%(name,count))
        count += 1
    q.join()   # 直到队列清空,程序才会结束


def consumer(name,q):
    count = 0
    while count <3:
        time.sleep(1)
        if not q.empty():break
        data = q.get()
        print(data)
        print(consumer %s has eat %s baozi%(name,count))
        count +=1
        q.task_done()


if __name__ == __main__:
    # 容器
    q = JoinableQueue()
    # 生产者们
    p = Process(target=producer,args=(A,q,))
    p.start()
    # 消费者们
    c = Process(target=consumer,args=(B,q,))
    c.daemon = True
    c.start()
    p.join()

 

python 之队列

标签:rand   done   join   +=   span   容器   col   模块   cer   

原文地址:https://www.cnblogs.com/stin/p/8533223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!