# 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. synchronized的简单理解

    synchronized能够保证在同一时刻只有一个线程执行该段代码. 使用synchronized能够防止多个线程同时并发访问程序的某些资源. synchronized既可以修饰变量,也可以修饰方法, ...

  2. 【hdu3507】Print Article 【斜率优化dp】

    题意 https://cn.vjudge.net/problem/HDU-3507 分析 斜率优化的模板题 #include <cstdio> #include <cstring&g ...

  3. 110. Balanced Binary Tree (Tree; DFS)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  4. tomcat服务编码集设置

    有三处需要记住修改 截图如下效果图: 第一处,对控制台右键,选择“默认值”找到,并设置为gbk: 第二处,在conf中找到server.xml文件并设置: 第三处:找到bin文件,找到tomcat80 ...

  5. Python 入门学习(贰)文件/文件夹正则表达式批量重命名工具

    基于 Udacity 的 Python 入门课程 Programming Foundations with Python 基于 Python 2.7 思路 Project 2 是一个去除文件名中所有数 ...

  6. PHP - MongoDB连接攻略

    http://blog.163.com/lgh_2002/blog/static/44017526201261111044608/ 使用PHP的Mongo扩展连接MongoDB.通过new Mongo ...

  7. How to Restart Qt Application

    How to restart QtApplication As we know, Restarting Application means to exit current application, t ...

  8. ubuntu 出来菜单栏和任务栏

    http://blog.csdn.net/terence1212/article/details/51340595 命令行输入:sudo apt-get install compizconfig-se ...

  9. myeclipse 快捷键,从步骤开始的大括号定位到匹配方法结束的大括号

    myeclipse 快捷键,从方法开始的大括号定位到匹配方法结束的大括号转至匹配的括号 Ctrl+Shift+P ctr+shift+r   文件名搜索文件 ctr+h           搜索文件里 ...

  10. vue项目 调用百度地图 BMap is not defined

    这次老板新接了一个四点半官网页面,使用vue来写.emm……我感觉整个人都不好了,两天半解决了20个静态页面.还好vue写页面简直飞快,遇到一个vue的新坑,使用百度地图. 研究了好一会,总是报错BM ...