# 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 用队列实现多线程并发的更多相关文章

  1. 【Python数据分析】Python3多线程并发网络爬虫-以豆瓣图书Top250为例

    基于上两篇文章的工作 [Python数据分析]Python3操作Excel-以豆瓣图书Top250为例 [Python数据分析]Python3操作Excel(二) 一些问题的解决与优化 已经正确地实现 ...

  2. python多进程并发和多线程并发和协程

    为什么需要并发编程? 如果程序中包含I/O操作,程序会有很高的延迟,CPU会处于等待状态,这样会浪费系统资源,浪费时间 1.Python的并发编程分为多进程并发和多线程并发 多进程并发:运行多个独立的 ...

  3. python 多进程并发与多线程并发

    本文对python支持的几种并发方式进行简单的总结. Python支持的并发分为多线程并发与多进程并发(异步IO本文不涉及).概念上来说,多进程并发即运行多个独立的程序,优势在于并发处理的任务都由操作 ...

  4. Python 多线程并发程序设计与分析

    多线程并发程序设计与分析 by:授客 QQ:1033553122 1.技术难点分析与总结 难点1:线程运行时,运行顺序不固定 难点2:同一段代码,再不加锁的情况下,可能被多个线程同时执行,这会造成很多 ...

  5. Python Socket多线程并发

    1.SocketServer模块编写的TCP服务器端代码 Socketserver原理图 服务端: import SocketServer #导入SocketServer,多线程并发由此类实现 cla ...

  6. 用Queue控制python多线程并发数量

    python多线程如果不进行并发数量控制,在启动线程数量多到一定程度后,会造成线程无法启动的错误. 下面介绍用Queue控制多线程并发数量的方法(python3). # -*- coding: utf ...

  7. Python+Unittest+Requests+PyMysql+HTMLReport 多线程并发接口化框架

    整体框架使用的是:Python+Unittest+Requests+PyMysql+HTMLReport 多线程并发模式 主要依赖模块 Unittest.Requests.PyMysql.HTMLRe ...

  8. Python多线程并发的误区

    由于项目要做一个并发测试,由于断言的东西较多,决定手写脚本.于是用python写了脚本: def test_method(thread_no): print("%s===test_metho ...

  9. python 多进程开发与多线程开发

    转自: http://tchuairen.blog.51cto.com/3848118/1720965 博文作者参考的博文:  博文1  博文2 我们先来了解什么是进程? 程序并不能单独运行,只有将程 ...

随机推荐

  1. latex如何插入空白行

    1.~\\:一行空白2.\\[行距]:可加入任意间距的空白行 [xpt]

  2. linux 使用systemctl 启动服务报错: Error: No space left on device

    By default, Linux only allocates 8192 watches for inotify, which is ridiculously low. And when it ru ...

  3. javascript中的属性类型

    ECMA-262第5版在定义只有内部才用的特性(attribute)时,描述了属性(property)的各种特性.ECMA-262定义这些特性是为了实现javascript引擎用的,因此在javasc ...

  4. 网页设计编辑利器——jQuery EasyUI所学整理(待编辑)

    1, Messager弹窗信息 方法: $.messager.alert(...), 在网页中间弹出一个窗口 $.messager.confirm(...) 弹出一个确认窗口, 有确定和取消两个按钮, ...

  5. Unity5-CacheServer(资源平台切换之缓存服务器)的部署与使用

    不管你是个人还是团队,如果你的资源很多, 而且经常切换平台, 那你试试这个吧~~兼职爽爆了,秒秒钟实现平台间的转换. cache server主要用于资源修改后的自动重新导入功能,保证所见即所得,方便 ...

  6. 创建和运行Java项目

    ---------siwuxie095                     首先在左侧的工程管理面板 Package Explorer 中,右键->New->Java Project ...

  7. 二叉树垂直遍历 · Binary Tree Vertical Order Traversal

    [抄题]: 给定二叉树,返回其节点值的垂直遍历顺序. (即逐列从上到下).如果两个节点在同一行和同一列中,则顺序应 从左到右. 给定一个二叉树 {3,9,20,#,#,15,7} 3 /\ / \ 9 ...

  8. python设置格式模板

    # -*- coding: utf-8 -*- """ __mktime__ = '${DATE}' __author__ = '${USER}' __filename_ ...

  9. Javascript变长参数和默认参数

    /* javascript 变长参数 * 实参少于形参: 剩下的参数如果没有默认值,将解析为undefined * 实参多于形参: 剩下的实参可以通过 "实参对象"-argumen ...

  10. loadrunner - 问题汇总及解决方案(持续更新)

    1.在此界面点击Run Load Tests提示: ”Can not save the license information because access to the registry is de ...