Python 用队列实现多线程并发
# Python queue队列,实现并发,在网站多线程推荐最后也一个例子,比这货简单,但是不够规范 # encoding: utf-8
__author__ = 'yeayee.com' # 由本站增加注释,可随意Fork、Copy from queue import Queue # Queue在3.x中改成了queue
import random
import threading
import time class Producer(threading.Thread):
"""
Producer thread 制作线程
"""
def __init__(self, t_name, queue): # 传入线程名、实例化队列
threading.Thread.__init__(self, name=t_name) # t_name即是threadName
self.data = queue """
run方法 和start方法:
它们都是从Thread继承而来的,run()方法将在线程开启后执行,
可以把相关的逻辑写到run方法中(通常把run方法称为活动[Activity]);
start()方法用于启动线程。
""" def run(self):
for i in range(5): # 生成0-4五条队列
print("%s: %s is producing %d to the queue!" % (time.ctime(), self.getName(), i)) # 当前时间t生成编号d并加入队列
self.data.put(i) # 写入队列编号
time.sleep(random.randrange(10) / 5) # 随机休息一会
print("%s: %s producing finished!" % (time.ctime(), self.getName)) # 编号d队列完成制作 class Consumer(threading.Thread):
"""
Consumer thread 消费线程,感觉来源于COOKBOOK
"""
def __init__(self, t_name, queue):
threading.Thread.__init__(self, name=t_name)
self.data = queue def run(self):
for i in range(5):
val = self.data.get()
print("%s: %s is consuming. %d in the queue is consumed!" % (time.ctime(), self.getName(), val)) # 编号d队列已经被消费
time.sleep(random.randrange(10))
print("%s: %s consuming finished!" % (time.ctime(), self.getName())) # 编号d队列完成消费 def main():
"""
Main thread 主线程
"""
queue = Queue() # 队列实例化
producer = Producer('Pro.', queue) # 调用对象,并传如参数线程名、实例化队列
consumer = Consumer('Con.', queue) # 同上,在制造的同时进行消费
producer.start() # 开始制造
consumer.start() # 开始消费
"""
join()的作用是,在子线程完成运行之前,这个子线程的父线程将一直被阻塞。
join()方法的位置是在for循环外的,也就是说必须等待for循环里的两个进程都结束后,才去执行主进程。
"""
producer.join()
consumer.join()
print('All threads terminate!') if __name__ == '__main__':
main() """运行结果: Thu Feb 4 11:05:48 2016: Pro. is producing 0 to the queue!
Thu Feb 4 11:05:48 2016: Pro. is producing 1 to the queue!
Thu Feb 4 11:05:48 2016: Con. is consuming. 0 in the queue is consumed!
Thu Feb 4 11:05:49 2016: Pro. is producing 2 to the queue!
Thu Feb 4 11:05:50 2016: Pro. is producing 3 to the queue!
Thu Feb 4 11:05:51 2016: Pro. is producing 4 to the queue!
Thu Feb 4 11:05:52 2016: Con. is consuming. 1 in the queue is consumed!
Thu Feb 4 11:05:53 2016: <bound method Producer.getName of <Producer(Pro., started 6864)>> producing finished!
Thu Feb 4 11:06:00 2016: Con. is consuming. 2 in the queue is consumed!
Thu Feb 4 11:06:06 2016: Con. is consuming. 3 in the queue is consumed!
Thu Feb 4 11:06:06 2016: Con. is consuming. 4 in the queue is consumed!
Thu Feb 4 11:06:12 2016: Con. consuming finished!
All threads terminate! """
Python 用队列实现多线程并发的更多相关文章
- 【Python数据分析】Python3多线程并发网络爬虫-以豆瓣图书Top250为例
基于上两篇文章的工作 [Python数据分析]Python3操作Excel-以豆瓣图书Top250为例 [Python数据分析]Python3操作Excel(二) 一些问题的解决与优化 已经正确地实现 ...
- python多进程并发和多线程并发和协程
为什么需要并发编程? 如果程序中包含I/O操作,程序会有很高的延迟,CPU会处于等待状态,这样会浪费系统资源,浪费时间 1.Python的并发编程分为多进程并发和多线程并发 多进程并发:运行多个独立的 ...
- python 多进程并发与多线程并发
本文对python支持的几种并发方式进行简单的总结. Python支持的并发分为多线程并发与多进程并发(异步IO本文不涉及).概念上来说,多进程并发即运行多个独立的程序,优势在于并发处理的任务都由操作 ...
- Python 多线程并发程序设计与分析
多线程并发程序设计与分析 by:授客 QQ:1033553122 1.技术难点分析与总结 难点1:线程运行时,运行顺序不固定 难点2:同一段代码,再不加锁的情况下,可能被多个线程同时执行,这会造成很多 ...
- Python Socket多线程并发
1.SocketServer模块编写的TCP服务器端代码 Socketserver原理图 服务端: import SocketServer #导入SocketServer,多线程并发由此类实现 cla ...
- 用Queue控制python多线程并发数量
python多线程如果不进行并发数量控制,在启动线程数量多到一定程度后,会造成线程无法启动的错误. 下面介绍用Queue控制多线程并发数量的方法(python3). # -*- coding: utf ...
- Python+Unittest+Requests+PyMysql+HTMLReport 多线程并发接口化框架
整体框架使用的是:Python+Unittest+Requests+PyMysql+HTMLReport 多线程并发模式 主要依赖模块 Unittest.Requests.PyMysql.HTMLRe ...
- Python多线程并发的误区
由于项目要做一个并发测试,由于断言的东西较多,决定手写脚本.于是用python写了脚本: def test_method(thread_no): print("%s===test_metho ...
- python 多进程开发与多线程开发
转自: http://tchuairen.blog.51cto.com/3848118/1720965 博文作者参考的博文: 博文1 博文2 我们先来了解什么是进程? 程序并不能单独运行,只有将程 ...
随机推荐
- centos 搭建owncloud私有云
ownCloud是一个免费开源的软件,用于为分享文件,日历,联系人,书签和个人音频/视频.非常容易安装和管理. 1:搭建环境配置:你的服务应该工作在LAMP上.参考下面链接去安装一键LAMP服务. h ...
- SOAP 版本可能不匹配: 出现意外的 Envelope 命名空间 http://schemas.xmlsoap.org/wsdl/
原错误描述:SOAP 版本可能不匹配: 出现意外的 Envelope 命名空间 http://schemas.xmlsoap.org/wsdl/.应为 http://schemas.xmlsoap.o ...
- UNITY地图寻路及服务器解决方案
多边形寻路算法简单介绍 http://liweizhaolili.lofter.com/post/1cc70144_86a939e UNITY3D MMO服务器寻路 http://www.pathen ...
- Unity Shader-简单均值模糊
http://blog.csdn.net/puppet_master/article/details/52547442 与Amplify中的Simple Blur例子实现一样
- C++中public、protected以及private的使用
相比C语言,C++中通过class/struct来定义既包含数据,又包含行为的结构,从而支持了“对象”.现实世界中,一个人(一个对象)通常 拥有一些资产(数据),并且掌握某些技能(行为),并且这些资产 ...
- java代码实现网络远程开机
http://my.oschina.net/kingfire/blog/156764 概述 远程开机(Wake onLAN)是指通过网络实现对服务器或者pc启动运行,现在很多网卡都支持的这个功能. 其 ...
- Java Thread系列(七)死锁
Java Thread系列(七)死锁 当线程需要同时持有多个锁时,有可能产生死锁.考虑如下情形: 线程 A 当前持有互斥所锁 lock1,线程 B 当前持有互斥锁 lock2.接下来,当线程 A 仍然 ...
- Please ensure that adb is correctly located at 。。。。。。。。。。。。
遇到问题描述: 运行Android程序控制台输出 [2012-07-18 16:18:26 - ] The connection to adb is down, and a severe error ...
- 【原创】请避免GO语言中的携程空跑(CPU突然激增)
其实GO语言从1.6版本开始非常不错了,GC性能优化非常到位,并且各种并行设计比从新实现一套C++版本的确是方便不少. 语言包也很多,库也相对稳定,完全可以适用于生产环境. 本文主要是给刚刚入门新手注 ...
- iOS密码输入框的实现
[iOS密码输入框的实现] 就是一个UITextField,把属性 UITextField.secureTextEntry设置为Yes即可.此种UI效果为iOS默认效果.