class multiprocessing.JoinableQueue([maxsize])

JoinableQueue, a Queue subclass, is a queue which additionally has task_done() and join() methods.

task_done()

Indicate that a formerly enqueued task is complete. Used by queue consumers. For each get() used to fetch a task, a subsequent call totask_done() tells the queue that the processing on the task is complete.

If a join() is currently blocking, it will resume when all items have been processed (meaning that a task_done() call was received for every item that had been put() into the queue).

Raises a ValueError if called more times than there were items placed in the queue.

join()

Block until all items in the queue have been gotten and processed.

The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer calls task_done() to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, join() unblocks.

这是官网对JoinableQueue的概述,我们通过这个方法就可以实现我们自己的生产者消费者模型,具体的实现思路请看我的分析<<项目开发中使用并发模型常见问题的整理与思考>>

code如下:

import multiprocessing

def printAll(queue, out_queue):
while 1:
t = queue.get()
print(t)
s = "生产{0}".format(t)
queue.task_done()
out_queue.put(s) if __name__ == "__main__":
queue = multiprocessing.JoinableQueue()
num_consumer = multiprocessing.cpu_count() * 2
out_queue = multiprocessing.Queue() for i in range(250):
queue.put(i) for _ in range(num_consumer):
p = multiprocessing.Process(target=printAll, args=(queue, out_queue))
p.start() queue.join() # 阻塞队列直到队列为空。
result = [] print("数量是: {}".format(out_queue.qsize())) while out_queue.qsize() != 0:
result.append(out_queue.get()) for i in result:
print(i)

简单地实现了我要的结果,具体可以再项目中应用上。

python JoinableQueue在生产者消费者项目中的简单应用的更多相关文章

  1. Jwt在Java项目中的简单实际应用

    1.什么是jwt 双方之间传递安全信息的简洁的.URL安全的表述性声明规范.JWT作为一个开放的标准(RFC 7519),定义了一种简洁的,自包含的方法用于通信双方之间以Json对象的形式安全的传递信 ...

  2. 进程,线程,GIL,Python多线程,生产者消费者模型都是什么鬼

    1. 操作系统基本知识,进程,线程 CPU是计算机的核心,承担了所有的计算任务: 操作系统是计算机的管理者,它负责任务的调度.资源的分配和管理,统领整个计算机硬件:那么操作系统是如何进行任务调度的呢? ...

  3. python 进程锁 生产者消费者模型 队列 (进程其他方法,守护进程,数据共享,进程隔离验证)

    #######################总结######### 主要理解 锁      生产者消费者模型 解耦用的   队列 共享资源的时候 是不安全的 所以用到后面的锁 守护进程:p.daem ...

  4. joinablequeue模块 生产者消费者模型 Manager模块 进程池 管道

    一.生产者消费者 主要是为解耦(借助队列来实现生产者消费者模型) import queue  # 不能进行多进程之间的数据传输 (1)from multiprocessing import Queue ...

  5. Python+Selenium进行UI自动化测试项目中,常用的小技巧1:读取excel表,转化成字典(dict)输出

    从今天开始我将会把在项目中遇到的问题,以及常用的一些技巧来分享出来,以此来促进自己的学习和提升自己:更加方便我以后的查阅. 现在要说的是:用Python来读取excel表的数据,返回字典(dict), ...

  6. 生产者消费者模式中条件判断是使用while而不是if

    永远在循环(loop)里调用 wait 和 notify,不是在 If 语句现在你知道wait应该永远在被synchronized的背景下和那个被多线程共享的对象上调用,下一个一定要记住的问题就是,你 ...

  7. Python+Selenium进行UI自动化测试项目中,常用的小技巧4:日志打印,longging模块(控制台和文件同时输出)

    在前段时间,为了给项目中加入日志功能,就想到了 logging 模块,百度logging一大推,都是各种复制的,并没有找到自己想要的结果:我的目的很简单,就是:在把日志写入文件的同时在控制台输出,更加 ...

  8. Python+Selenium进行UI自动化测试项目中,常用的小技巧3:写入excel表(python,xlsxwriter)

    我们在项目中可能用到excel表生成,下面的代码就是对excel表的操作: import xlsxwriter import datetime class write_excel(): def __i ...

  9. Python+Selenium进行UI自动化测试项目中,常用的小技巧2:读取配置文件(configparser,.ini文件)

    在自动化测试项目中,可能会碰到一些经常使用的但 很少变化的配置信息,下面就来介绍使用configparser来读取配置信息config.ini 读取的信息(config.ini)如下: [config ...

随机推荐

  1. Java中导出到Excel实现_aspose.cells

    参考http://183615215-qq-com.iteye.com/blog/1858208 包下载:http://pan.baidu.com/s/1o6ju0ZK,将lib的jar包导入到工程中 ...

  2. ubunut系统清理系统根目录下缓存文件夹.cache超大导致磁盘不足

    在使用中突然发现系统超慢,没有做什么特别的操作. 只好重启下电脑,重启后提示系统空间不足1G.挨个查看文件夹大小,没有发现问题,然后就用Ctrl + H显示隐藏文件夹后再继续逐个查看大小,发现.cac ...

  3. 6.0、Android Studio性能优化工具

    显示图像包含四个步骤.简单来说,CPU对比显示列表,GPU渲染图片显示,内存存储图片和数据,电池提供点力能源.每个部分的硬件都有限制,超过这个限制会导致应用运行较慢,显示性能差,或者耗电. 为了查找造 ...

  4. 一个iOS6系统bug+一个iOS7系统bug

    先看实际工作中遇到的两个bug:(1)iPhone Qzone有一个导航栏背景随着页面滑动而渐变的体验,当页面滑动到一定距离时,会改变导航栏上title文本的颜色,但是有一个莫名其妙的bug,如下:

  5. python脚本程序,传入参数*要用单引号'*'

    *号作为python脚本的传入参数时,必须用单引号'',才能正确传入.如python test.py 2014 '*' age python test.py 2014 * age是错误的. 比如 te ...

  6. C语言实现4种常用排序

    实在没事搞,反正面试也要用到,继续来写4种排序算法.因为那天用java写了排序,突然想到我是要面试IOS,起码也得用C写.C竟然忘干净了,方法都不会写了.囧啊! 下面用C实现4种排序算法:快速排序.冒 ...

  7. Cocos2D:塔防游戏制作之旅(五)

    打开HelloWorldLayer.h文件,添加以下实例变量(在@interface行的花括号之后): NSMutableArray *towerBases; 将HelloWorldLayer.m文件 ...

  8. Linux多线程实践(8) --Posix条件变量解决生产者消费者问题

    Posix条件变量 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr); int pthread_co ...

  9. 内存管理Memory&nbsp;OC——第九天

    1.   内存管理方式         垃圾回收机制:(Garbage Collection),有系统管理内存,开发人员需要管理         注:OC从2.0之后就开始支持垃圾回收机制,但是只适用 ...

  10. cas 单点登录(SSO)实验之二: cas-client

    cas 单点登录(SSO)实验之二: cas-client 参考文章: http://my.oschina.net/indestiny/blog/200768#comments http://wenk ...