# 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. springmvc web.xml配置之 -- ContextLoaderListener

    首先回归一下web.xml的常用配置,看一个示例: <context-param> <param-name>contextConfigLocation</param-na ...

  2. 【BZOJ4154】Generating Synergy【kd树】

    题意 给定一棵以1为根的有根树,初始所有节点颜色为1,每次将距离节点a不超过l的a的子节点染成c,或询问点a的颜色 分析 我们以dfs序为横坐标,深度为纵坐标,建kd树.我们每次更新,都是在kd树中更 ...

  3. 307. Range Sum Query - Mutable查询求和的范围(可变)

    [抄题]: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inc ...

  4. static变量和static函数

    在C语言编程中,static的一个作用是信息屏蔽! 比方说,你自己定义了一个文件 -- 该文件中有一系列的函数以及变量的声明和定义! 你希望该文件中的一些函数和变量只能被该文件中的函数使用,那么,你可 ...

  5. 8-cin cout PK scanf printf(速度快慢问题对比)

    我们在c++ 中使用cin cout很方便但速度很慢,导致有些题目用cin就超时而用scanf则就ac了,那到底改用谁? cin慢是有原因的,其实默认的时候,cin与stdin总是保持同步的,也就是说 ...

  6. Chrome 扩展 Vue Devtools

    Vue.js devtools是基于google chrome浏览器的一款调试vue.js应用的开发者浏览器扩展,可以在浏览器开发者工具下调试代码. 1)首先在github下载devtools源码,地 ...

  7. ST-LINK驱动的安装

    1.下载ST-LINK驱动ST-LINK_USB_V2_1_Driver 双击dpinst_amd64.exe来安装. 成功会显示: 2.进入MDK5里面去配置ST-LINK 通过魔术棒选项: a.D ...

  8. shell 用环境变量的值修改properties文件

    假设有如下属性文件 demo.properties user.name=test user.password=123456 ............................... 需求:先需要 ...

  9. windows平台使用spark-submit以client方式提交spark应用到standalone集群

    1.spark应用打包,我喜欢打带依赖的,这样省事. 2.使用spark-submit.bat 提交应用,代码如下: for /f "tokens=1,2 delims==" %% ...

  10. struct 和union的区别

    union ( 共用体):构造数据类型,也叫联合体  用途:使几个不同类型的变量共占一段内存(相互覆盖) struct ( 结构体 ):是一种构造类型 用途: 把不同的数据组合成一个整体——自定义数据 ...