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

  

05-11 18:26