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作用,并给出实例,需要的朋友可以参考下 ...
随机推荐
- node版本控制工具nvm安装教程
一.安装nvm 查看node对应NPM:https://nodejs.org/en/about/previous-releases 1.卸载node,后删除node文件夹里的所有内容 2:安装nvm管 ...
- hadoop-3.0.0-cdh6.3.2源码编译实践
1.编译过程 参考:https://blog.mygallop.cn/2020/10/centos/hadoop-cdh6-compile/ 2.问题记录 CDH6.3.2 Hadoop源码位置发生变 ...
- Docker从认识到实践再到底层原理(四-1)|Docker镜像仓库|超详细详解
前言 那么这里博主先安利一些干货满满的专栏了! 首先是博主的高质量博客的汇总,这个专栏里面的博客,都是博主最最用心写的一部分,干货满满,希望对大家有帮助. 高质量博客汇总 然后就是博主最近最花时间的一 ...
- Program文件的作用
Program.cs文件分析 Program.cs文件是至关重要的一个文件,它包含应用程序启动的代码,还可以配置所需要的服务和应用管道的中间件. 需要掌握: 6.0版本前后生成的Program.cs文 ...
- typora beta版本 typora免费版 typora 0.11.18 下载
壹 ❀ 引 typora从1.0.0正式版开始就不再免费了,可能有一些开了自动检测更新的同学,在某次打开typora就看到了购买以及试用天数的弹窗,但typora正式之前的beta版依旧免费,这里就分 ...
- 将CH340G的USB2TTL扩展出RTS, DTR口
关于 要测试串口中的RTS和DTR, 最常见的USB2TTL基于CH340G, 并没有引出RTS, 然而这个IC是支持这些信号的, 只是PCB上将这些pin留空了. 这块板子的PCB 电路 详细信息可 ...
- haproxy ssl证书配置
通常情况下,web应用程序的ssl证书放置于nginx的服务器,但很多时候前面会加一次负载均衡,使用HAProxy可以实现https的证书安全,从客户浏览器到HAProxy代理服务器之间为ssl加密传 ...
- Java并发编程实例--14.在一个同步类中安排独立属性
当你使用synchronized关键字去保护一个代码块时,你必须传入一个对象的引用. 正常来讲,你讲使用this关键字去引用执行这个方法的对象,但是你可以使用其他对象的引用. 通常的,这些对象将会是专 ...
- 跨越千年医学对话:用AI技术解锁中医古籍知识,构建能够精准问答的智能语言模型,成就专业级古籍解读助手(LLAMA)
跨越千年医学对话:用AI技术解锁中医古籍知识,构建能够精准问答的智能语言模型,成就专业级古籍解读助手(LLAMA) 介绍:首先在 Ziya-LLaMA-13B-V1基线模型的基础上加入中医教材.中医各 ...
- win32 - Shell菜单项的创建
#include <windows.h> #include <shobjidl_core.h> #include <windowsx.h> #include < ...