Python多线程的简单实现(生产者消费者模型)
2024-09-29 10:28:44
__author__ = "JentZhang" import time, threading, queue q = queue.Queue(maxsize=) # 声明队列 def Producer(name):
'''生产者'''
count =
while True:
q.put(count) # 往队列中添加数据
print("[%s] 生产了第%s包子\n" % (name, count))
count +=
time.sleep() def Consumer(name):
'''消费者'''
while True:
i = q.get() # 从队列中取数据
print("====[%s] 吃了第%s个包子\n" % (name, i))
time.sleep() '''设置多线程'''
p = threading.Thread(target=Producer, args=("Jent",))
c1 = threading.Thread(target=Consumer, args=("张三",))
c2 = threading.Thread(target=Consumer, args=("李四",)) '''线程开启'''
p.start()
c1.start()
c2.start()
最新文章
- 修改 Docker 默认网桥地址
- OAF_开发系列17_实现OAF数组应用Vector / Hashmap / Hashtable / Arraylist(案例)
- xUtils,butterknife...处理findviewbyid
- Java多态与C++中多态的实现
- light oj 1393 - Crazy Calendar 博弈论
- linux自己带的apache重新启动
- multipath.conf
- Xcode开启gcc/g++
- struts 学习之问一
- sqlprofiler 常用调试方法
- Top 10 Mapping APIs: Google Maps, Microsoft Bing Maps and MapQuest
- C语言之一数三平方
- Android学习笔记:adb 与 adb shell操作 以及中文乱码解决
- 关于阮大神的es6标准入门第一章
- 洛谷 P1129 解题报告
- margin-塌陷问题
- Gatsby上手指南 - 让你的静态网站用react来高逼格的写
- 轻量级ORM框架 Bankinate
- 09-babel
- SpringBoot的第一个web项目
热门文章
- Teradata Delete Database and Drop Database
- Hadoop-2.0 目录简介
- Linux 搜某个文件里关键字的上下500行到执行文件里
- Ubuntu 16——安装——ns2.35和nam
- asp.net执行顺速
- 安装PyQt5时缺少designer.exe的解决办法
- cf Round#273 Div.2
- python爬虫学习之爬取全国各省市县级城市邮政编码
- IM系统的MQ消息中间件选型:Kafka还是RabbitMQ?
- Javascript高级编程学习笔记(25)—— 函数表达式(3)模仿块级作用域