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 我们先来了解什么是进程? 程序并不能单独运行,只有将程 ...
随机推荐
- fabric本地一键部署LAMP
一.添加普通用户jeff执行sudo时无需输入密码 $ sudo vim /etc/sudoers ---------------------------------------> ## All ...
- uva10288 Coupons 【概率 分数】
题目: 题意: 一共n种不同的礼券,每次得到每种礼券的概率相同.求期望多少次可以得到所有n种礼券.结果以带分数形式输出.1<= n <=33. 思路: 假设当前已经得到k种,获得新的一种的 ...
- 79. Word Search (Array; DFS,Back-Track)
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- Oracle_in_not-in_distinct_minsu的用法
create table a( id int, username ) ); create table b( id int, username ) ); ,'小明'); ,'小红'); ,'小君'); ...
- Flannel网络部署
一.Flannel网络部署 为Flannel生成证书 [root@linux-node1 ssl]# vim flanneld-csr.json { "CN": "fla ...
- SuSE 网卡配置模板
heidsoft:/etc/sysconfig/network # cat ifcfg.template ## This is a template for a network interface c ...
- NOSQL之Redis、MongDB、Habase、Cassandra的介绍与比较
一.Redis介绍 1.1Redis优点 (1)Redis拥有非常丰富的数据结构: (2)Redis提供事务的功能,可以保证一串命令的原子性,中间不会被任何打断. (3)数据存储在内存中,读写 ...
- mongodb的安装与启动(centos7)
公司迁移mongodb,查了半天资料,各种考证,各种资讯,最后我选择很野蛮的直接把库和日志直接scp过来了....... 记录下如何安装和启动mongodb: 1.解压mongodb: [root@i ...
- ScrollView嵌套ListView只显示一行之计算的高度不正确的解决办法(转)
ScrollView嵌套ListView只显示一行之计算的高度不正确的解决办法 分类: android应用开发2013-12-19 09:40 1045人阅读 评论(3) 收藏 举报 AndroidS ...
- 斜杠反斜杠,去空格\xa0,连接函数join()
1斜杠反斜杠 斜杠:/.反斜杠:\. 反斜杠\,在windows系统中用来表示目录. 而在unix系统中,/表示目录.由于web遵循unix命名,所以在网址(URL)中,/表示目录. 在unix系统中 ...