async+队列queue.Queue()
import queue
import time
import random
import threading
import asyncio
import logging
logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(levelname)s -->%(funcName)s at line %(lineno)d: \n %(message)s')
log= logging.getLogger()
q_init = queue.Queue(maxsize=5)
async def jobs(item):
time.sleep(random.randint(1,15))
status = random.randint(0, 1)
if status == 0:
return ("success",item)
else:
return ("failed",item) async def do_work(item):
logging.info("do something %s,time start %s" % (item, time.asctime()))
a =await jobs(item)
return a def ma(checker):
new_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_loop)
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(do_work(checker))
loop.run_until_complete(asyncio.wait([task]))
loop.close()
st = task.result()
print(st)
return st def worker_consumer(q_init):
while True:
if q_init.empty():
logging.info("queue is empty stop current thread worker by break " )
break
checker = q_init.get()
st=ma(checker)
if st[0] in ["success", "failed"]:
logging.info("%s task finished status is %s" % (st[1],st[0]))
q_init.task_done() def producer():
item = 1
while item:
q_init.put(item)
item += 1
if item > 50:
break if __name__ == '__main__':
producer = [threading.Thread(target=producer)]
consumer = [threading.Thread(target=worker_consumer, args=(q_init,)) for i in range(5)]
consumer_pool = []
p_pool=[]
for p in producer:
p.start()
for k in consumer:
k.start()
consumer_pool.append(k)
for m in consumer:
m.join()
q_init.join()
结果:
2019-12-17 12:39:37,845 - INFO -->worker_consumer at line 40:
3 task finished status is failed
2019-12-17 12:39:38,844 - INFO -->worker_consumer at line 40:
1 task finished status is failed
2019-12-17 12:39:39,858 - INFO -->worker_consumer at line 40:
6 task finished status is success
2019-12-17 12:39:40,846 - INFO -->worker_consumer at line 40:
4 task finished status is success
2019-12-17 12:39:40,854 - INFO -->worker_consumer at line 40:
7 task finished status is success
2019-12-17 12:39:41,847 - INFO -->worker_consumer at line 40:
5 task finished status is success
2019-12-17 12:39:42,865 - INFO -->worker_consumer at line 40:
9 task finished status is success
2019-12-17 12:39:45,854 - INFO -->worker_consumer at line 40:
11 task finished status is success
2019-12-17 12:39:49,861 - INFO -->worker_consumer at line 40:
8 task finished status is failed
2019-12-17 12:39:49,868 - INFO -->worker_consumer at line 40:
12 task finished status is failed
2019-12-17 12:39:50,847 - INFO -->worker_consumer at line 40:
2 task finished status is success
2019-12-17 12:39:55,860 - INFO -->worker_consumer at line 40:
10 task finished status is success
2019-12-17 12:39:57,856 - INFO -->worker_consumer at line 40:
16 task finished status is failed
2019-12-17 12:39:59,861 - INFO -->worker_consumer at line 40:
13 task finished status is failed
2019-12-17 12:40:02,867 - INFO -->worker_consumer at line 40:
19 task finished status is failed
2019-12-17 12:40:02,874 - INFO -->worker_consumer at line 40:
15 task finished status is failed
2019-12-17 12:40:03,876 - INFO -->worker_consumer at line 40:
14 task finished status is success
2019-12-17 12:40:05,867 - INFO -->worker_consumer at line 40:
17 task finished status is success
2019-12-17 12:40:07,873 - INFO -->worker_consumer at line 40:
20 task finished status is success
2019-12-17 12:40:10,863 - INFO -->worker_consumer at line 40:
18 task finished status is success
2019-12-17 12:40:11,875 - INFO -->worker_consumer at line 40:
23 task finished status is failed
2019-12-17 12:40:12,886 - INFO -->worker_consumer at line 40:
22 task finished status is success
2019-12-17 12:40:14,881 - INFO -->worker_consumer at line 40:
26 task finished status is success
2019-12-17 12:40:16,890 - INFO -->worker_consumer at line 40:
28 task finished status is success
2019-12-17 12:40:17,881 - INFO -->worker_consumer at line 40:
21 task finished status is failed
2019-12-17 12:40:20,869 - INFO -->worker_consumer at line 40:
25 task finished status is failed
2019-12-17 12:40:20,880 - INFO -->worker_consumer at line 40:
24 task finished status is failed
2019-12-17 12:40:24,896 - INFO -->worker_consumer at line 40:
27 task finished status is failed
2019-12-17 12:40:28,891 - INFO -->worker_consumer at line 40:
29 task finished status is failed
2019-12-17 12:40:29,875 - INFO -->worker_consumer at line 40:
31 task finished status is success
2019-12-17 12:40:31,884 - INFO -->worker_consumer at line 40:
30 task finished status is failed
2019-12-17 12:40:31,897 - INFO -->worker_consumer at line 40:
34 task finished status is success
2019-12-17 12:40:31,905 - INFO -->worker_consumer at line 40:
33 task finished status is failed
2019-12-17 12:40:32,883 - INFO -->worker_consumer at line 40:
32 task finished status is failed
2019-12-17 12:40:32,891 - INFO -->worker_consumer at line 40:
36 task finished status is failed
2019-12-17 12:40:36,894 - INFO -->worker_consumer at line 40:
39 task finished status is success
2019-12-17 12:40:38,880 - INFO -->worker_consumer at line 40:
35 task finished status is failed
2019-12-17 12:40:38,911 - INFO -->worker_consumer at line 40:
38 task finished status is success
2019-12-17 12:40:39,894 - INFO -->worker_consumer at line 40:
40 task finished status is failed
2019-12-17 12:40:40,897 - INFO -->worker_consumer at line 40:
41 task finished status is success
2019-12-17 12:40:44,902 - INFO -->worker_consumer at line 40:
44 task finished status is success
2019-12-17 12:40:45,915 - INFO -->worker_consumer at line 40:
46 task finished status is success
2019-12-17 12:40:46,886 - INFO -->worker_consumer at line 40:
42 task finished status is failed
2019-12-17 12:40:46,901 - INFO -->worker_consumer at line 40:
37 task finished status is success
2019-12-17 12:40:48,895 - INFO -->worker_consumer at line 40:
48 task finished status is failed
2019-12-17 12:40:49,902 - INFO -->worker_consumer at line 40:
45 task finished status is success
2019-12-17 12:40:49,902 - INFO -->worker_consumer at line 35:
queue is empty stop current thread worker by break
2019-12-17 12:40:50,922 - INFO -->worker_consumer at line 40:
47 task finished status is success
2019-12-17 12:40:50,922 - INFO -->worker_consumer at line 35:
queue is empty stop current thread worker by break
2019-12-17 12:40:51,929 - INFO -->worker_consumer at line 40:
43 task finished status is success
2019-12-17 12:40:51,930 - INFO -->worker_consumer at line 35:
queue is empty stop current thread worker by break
2019-12-17 12:40:52,900 - INFO -->worker_consumer at line 40:
50 task finished status is failed
2019-12-17 12:40:52,900 - INFO -->worker_consumer at line 35:
queue is empty stop current thread worker by break
2019-12-17 12:40:59,905 - INFO -->worker_consumer at line 40:
49 task finished status is success
2019-12-17 12:40:59,905 - INFO -->worker_consumer at line 35:
queue is empty stop current thread worker by break Process finished with exit code 0
async+队列queue.Queue()的更多相关文章
- 【c#】队列(Queue)和MSMQ(消息队列)的基础使用
首先我们知道队列是先进先出的机制,所以在处理并发是个不错的选择.然后就写两个队列的简单应用. Queue 命名空间 命名空间:System.Collections,不在这里做过多的理论解释,这个东西非 ...
- python中利用队列asyncio.Queue进行通讯详解
python中利用队列asyncio.Queue进行通讯详解 本文主要给大家介绍了关于python用队列asyncio.Queue通讯的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细 ...
- Nodejs事件引擎libuv源码剖析之:高效队列(queue)的实现
声明:本文为原创博文,转载请注明出处. 在libuv中,有一个只使用简单的宏封装成的高效队列(queue),现在我们就来看一下它是怎么实现的. 首先,看一下queue中最基本的几个宏: typede ...
- Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)
Counter(计数器) 是一个字典的子类,存储形式同样为字典,其中存储的键为字典的元素,值为元素出现的次数,在使用之前我们需要先导入文件 import collections 初始化一个计数器 im ...
- 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)
Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuq ...
- Berkeley DB的数据存储结构——哈希表(Hash Table)、B树(BTree)、队列(Queue)、记录号(Recno)
Berkeley DB的数据存储结构 BDB支持四种数据存储结构及相应算法,官方称为访问方法(Access Method),分别是哈希表(Hash Table).B树(BTree).队列(Queue) ...
- jQuery源代码学习之七—队列模块queue
一.jQuery种的队列模块 jQuery的队列模块主要是为动画模块EFFECTS提供支持,(不过到现在为了支持动画队列的inprogress的出入队还是搞不太清楚),单独抽取出一个命名空间是为了使程 ...
- 纸上谈兵:队列(queue)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 队列(queue)是一个简单而常见的数据结构.队列也是有序的元素集合.队列最大的特 ...
- python-Day3-set 集合-counter计数器-默认字典(defaultdict) -可命名元组(namedtuple)-有序字典(orderedDict)-双向队列(deque)--Queue单项队列--深浅拷贝---函数参数
上节内容回顾:C语言为什么比起他语言块,因为C 会把代码变异成机器码Pyhton 的 .pyc文件是什么python 把.py文件编译成的.pyc文件是Python的字节码, 字符串本质是 字符数组, ...
随机推荐
- BOM笔记
目录 BOM (浏览器对象模型) 简介 window对象 子对象 history 子对象 lacation 子对象 navigator 子对象 screen 子对象 frames BOM (浏览器对象 ...
- 巨杉Tech | 十分钟快速搭建 Wordpress 博客系统
介绍 很多互联网应用程序开发人员第一个接触到的网站项目就是博客系统.而全球使用最广的Wordpress常常被用户用来快速搭建个人博客网站.默认情况下,Wordpress一般在后台使用MySQL关系型数 ...
- 用Object.prototype.toString.call(obj)检测对象类型原因分析
用Object.prototype.toString.call(obj)检测对象类型原因分析 更新时间:2018年10月11日 08:46:33 投稿:laozhang 我要评论 在本 ...
- 转载:TDM协议
转自http://www.wangdali.net/i2s/ 1. PCM简介 PCM (Pulse Code Modulation) 是通过等时间隔(即采样率时钟周期)采样将模拟信号数字化的方法.图 ...
- 使用tableExport.js直接导出web页面上的table
1,需要导入两个js文件,一个tableExport.js,另一个是jquery.base64.js,前一个文件是导出数据和核心类库,后一个是为了避免导出中文时乱码的js文件,如果你导出的数据没有中文 ...
- django入门与实践(续)
完善博客 博客页面设计 页面概要 1.博客主页面 主页面内容 文章标题列表,超链接 发表博客按钮(超链接) 列表编写思路 取出数据库中所有文章对象 将文章对象们打包成列表,传递到前端 前端页面把文章以 ...
- 【算法学习记录-排序题】【PAT A1012】The Best Rank
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- DSP---TI CCSv5.5.x-Windows安装
TI CCSv5.5.x(正式版)-Windows版本 国内2013年9月13日首发安装CCSv5.5图示 *请关掉防火墙及杀毒软件进行安装 第一步 第二步 安装程序检测到挂起的重新启动,这可能在安装 ...
- UVA 1267 Network(DFS)
题目链接:https://vjudge.net/problem/UVA-1267 首先我们要把这样一棵无根树转换成有根树,那么树根我们可以直接使用$VOD$. 还有一个性质:如果深度为$d$的一个节点 ...
- 按需引入element-ui时修改.babelrc报错
刚开始学习element-ui时用的都是完整引入,图省事,这次准备按需引入,以减小项目体积, 乙烯类npm 之后,到了修改 .babelrc 文件这一步(PS:vue-cli 2.0版本会有这个文件, ...