python queue join task_done的概念及实例解析
import threading
import queue
import time # 创建队列,用于存储数据
q = queue.Queue()
icnt = 0
def producer():
while True:
global icnt
icnt = icnt + 1
#print("producer icnt is:%d "%icnt)
data = "hello world"
print("producer is:",icnt)
q.put(data) # 生产者线程函数,向队列存入数据
#q.join()
data = "hello queue aaa "
print("producer is:aaa ",icnt)
q.put(data) # 生产者线程函数,向队列存入数据
q.join()
time.sleep(1) def consumer():
while True:
data = q.get() # 消费者线程,从队列取出数据
q.task_done()
print("consumer is:",data) # 创建并启动生产者、消费者线程
t1 = threading.Thread(target=producer)
t2 = threading.Thread(target=consumer)
t1.start()
t2.start() # 等待线程结束 t1.join()
t2.join()
运行结果:
producer is: 1
producer is:aaa 1
consumer is: hello world
consumer is: hello queue aaa
producer is: 2
producer is:aaa 2
consumer is: hello world
consumer is: hello queue aaa
源码二
import threading
import queue
import time # 创建队列,用于存储数据
q = queue.Queue()
icnt = 0
def producer():
while True:
global icnt
icnt = icnt + 1
#print("producer icnt is:%d "%icnt)
data = "hello world"
print("producer is:",icnt)
q.put(data) # 生产者线程函数,向队列存入数据
q.join()
data = "hello queue aaa "
print("producer is:aaa ",icnt)
q.put(data) # 生产者线程函数,向队列存入数据
q.join()
time.sleep(1) def consumer():
while True:
data = q.get() # 消费者线程,从队列取出数据
q.task_done()
print("consumer is:",data) # 创建并启动生产者、消费者线程
t1 = threading.Thread(target=producer)
t2 = threading.Thread(target=consumer)
t1.start()
t2.start() # 等待线程结束 t1.join()
t2.join()
运行结果:
producer is: 1
consumer is: hello world
producer is:aaa 1
consumer is: hello queue aaa
producer is: 2
consumer is: hello world
producer is:aaa 2
consumer is: hello queue aaa
python queue join task_done的概念及实例解析的更多相关文章
- Python Queue队列
queue is especially useful in threaded programming when information must be exchanged safely between ...
- Python Queue实现生产与消费
Python Queue模块详解 from:https://blog.linuxeye.com/334.html Python中,队列是线程间最常用的交换数据的形式.Queue模块是提供队列操作的模块 ...
- python --- queue模块使用
1. 什么是队列? 学过数据结构的人都知道,如果不知道队列,请Google(或百度). 2. 在python中什么是多生产者,多消费模型? 简单来说,就是一边生产(多个生产者),一边消费(多个消费者) ...
- Python -- queue队列模块
一 简单使用 --内置模块哦 import Queuemyqueue = Queue.Queue(maxsize = 10) Queue.Queue类即是一个队列的同步实现.队列长度可为无限或者有限. ...
- python Queue在两个地方
其一: Source code: Lib/queue.py The queue module implements multi-producer, multi-consumer queues. It ...
- python queue - 同步队列类
参考 官网 queue 模块 queue 模块实现多生产者,多消费者队列. 当必须在 ==多个线程之间安全地交换信息== 时,它在线程编程中特别有用. 此模块中的Queue类实现了所有必需的锁定语义. ...
- python Queue(队列学习)
Python 的Queue模块中提供了同步的.线程安全的队列类,包括FIFO(先入先出)队列Queue,LIFO(后入先出)队列LifoQueue,和优先级队列PriorityQueue.这些队列都实 ...
- 简单介绍一下python Queue中常用的方法
Queue.qsize() 返回队列的大小 Queue.empty() 如果队列为空,返回True,反之False Queue.full() 如果队列满了,返回True,反之FalseQueue.fu ...
- Python Queue(队列)
Queue模块实现了多生产者.多消费者队列.当必须在多个线程之间安全地交换信息时,它在线程编程中特别有用,实现了所有必需的锁定语义. 一.该模块实现了三种类型的队列,它们的区别仅在于检索条目的顺序: ...
- Python中的exec、eval使用实例
Python中的exec.eval使用实例 这篇文章主要介绍了Python中的exec.eval使用实例,本文以简洁的方式总结了Python中的exec.eval作用,并给出实例,需要的朋友可以参考下 ...
随机推荐
- SpringBoot接入阿里云oss
1.pom中添加阿里云oss坐标 <?xml version="1.0" encoding="utf-8"?> <dependencies&g ...
- 性能暴增70%!AMD线程撕裂者RPO 7000将于10月19日发布: 96核心Zen 4史无前例
据wccftech最新报道,AMD的下一代Ryzen Threadripper(线程撕裂者)PRO 7000"Storm Peak"CPU将于10月19日作为终极工作站解决方案亮相 ...
- P8512 [Ynoi Easy Round 2021] TEST_152 题解
题目链接:[Ynoi Easy Round 2021] TEST_152 题目比较抽象,翻译一下.就是有 \(n\) 个操作,每个操作为 \((l_i,r_i,v_i)\) 表示把长为 \(m\) 序 ...
- Spring Boot 1.5.x 结合 JUnit5 进行接口测试
在Spring Boot 1.5.x中,默认使用Junit4进行测试.而在对Controller进行接口测试的时候,使用 @AutoConfigureMockMvc 注解是不能注入 MockMvc 对 ...
- DP的各种优化小结
动态规划算法(简称动规,DP),是IO中最为常见的,也是最为重要的算法之一.这也就意味着,在各种题目与比赛中它会有很多稀奇古怪的算法和优化,时不时地在你的面前出现一个TLE,MLE和RE来搞你的心态. ...
- Codeforces Round #887 (Div. 2) A-D
比赛链接 A 代码 #include <bits/stdc++.h> using namespace std; using ll = long long; int a[507]; bool ...
- MySQL基础之DCL语句
DCL(Data Control Language)语句:数据控制语句. 用途:控制数据库.表.字段.用户的访问权限和安全级别. 常用关键字:grant.revoke等 一般用于管理数据库和用户的权限 ...
- Swoole从入门到入土(28)——协程[核心API]
本节专门介绍swoole提供的协程机制中核心的API 类方法: 1) set():协程设置,设置协程相关选项. Swoole\Coroutine::set(array $options); 2) ge ...
- nodejs+express4实现文件上传下载删除和列表展示功能
0.效果展示 1.创建项目 创建文件夹:express_file_upload npm init # 入口文件选择server.js 安装插件 npm install express npm inst ...
- 编译原理LR分析
LR(0)分析存在问题及解决办法 当LR(0)含有互相冲突的项目时,则需要向前展 望符号串,检查下一个输入符号的状态 例 项目集I={X→α· bβ,A→α·,B→α· } 当面临输入符号b时,应该选 ...