python 多线程应用介绍


Posted in Python onDecember 19, 2012

python可以方便地支持多线程。可以快速创建线程、互斥锁、信号量等等元素,支持线程读写同步互斥。美中不足的是,python的运行在python 虚拟机上,创建的多线程可能是虚拟的线程,需要由python虚拟机来轮询调度,这大大降低了python多线程的可用性。我们经今天用了经典的生产者和消费者的问题来说明下python的多线程的运用 上代码:

#encoding=utf-8 
import threading 
import random 
import time 
from Queue import Queue 

class Producer(threading.Thread): 

def __init__(self, threadname, queue): 
threading.Thread.__init__(self, name = threadname) 
self.sharedata = queue 

def run(self): 
for i in range(20): 
print self.getName(),'adding',i,'to queue' 
self.sharedata.put(i) 
time.sleep(random.randrange(10)/10.0) 
print self.getName(),'Finished' 


# Consumer thread 

class Consumer(threading.Thread): 


def __init__(self, threadname, queue): 
threading.Thread.__init__(self, name = threadname) 
self.sharedata = queue 


def run(self): 

for i in range(20): 
print self.getName(),'got a value:',self.sharedata.get() 
time.sleep(random.randrange(10)/10.0) 
print self.getName(),'Finished' 


# Main thread 

def main(): 

queue = Queue() 
producer = Producer('Producer', queue) 
consumer = Consumer('Consumer', queue) 
print 'Starting threads ...' 
producer.start() 
consumer.start() 
producer.join() 
consumer.join() 
print 'All threads have terminated.' 
if __name__ == '__main__': 
main()

你亲自运行下这断代码,可能有不一样的感觉!理解以后可以用python cookielib 再结果python urllib 写一个多线程下载网页的脚本应该没什么问题

Python 相关文章推荐
Python去除列表中重复元素的方法
Mar 20 Python
Python中用PIL库批量给图片加上序号的教程
May 06 Python
Python中表示字符串的三种方法
Sep 06 Python
在cmd中运行.py文件: python的操作步骤
May 12 Python
Anaconda2下实现Python2.7和Python3.5的共存方法
Jun 11 Python
Python机器学习k-近邻算法(K Nearest Neighbor)实例详解
Jun 25 Python
使用Python处理BAM的方法
Sep 28 Python
window7下的python2.7版本和python3.5版本的opencv-python安装过程
Oct 24 Python
浅析PyCharm 的初始设置(知道)
Oct 12 Python
python实现文件+参数发送request的实例代码
Jan 05 Python
如何用python开发Zeroc Ice应用
Jan 29 Python
Python内置的数据类型及使用方法
Apr 13 Python
Python多线程学习资料
Dec 19 #Python
python搭建简易服务器分析与实现
Dec 15 #Python
Python笔记(叁)继续学习
Oct 24 #Python
python笔记(2)
Oct 24 #Python
python笔记(1) 关于我们应不应该继续学习python
Oct 24 #Python
Python的一些用法分享
Oct 07 #Python
Python天气预报采集器实现代码(网页爬虫)
Oct 07 #Python
You might like
php基础知识:类与对象(1)
2006/12/13 PHP
php中autoload的用法总结
2013/11/08 PHP
thinkPHP分页功能实例详解
2017/05/05 PHP
CSS心形加载的动画源码的实现
2021/03/09 HTML / CSS
jquery.ui.draggable中文文档
2009/11/24 Javascript
js 巧妙去除数组中的重复项
2010/01/25 Javascript
跟着JQuery API学Jquery 之三 筛选
2010/04/09 Javascript
IE中的File域无法清空使用jQuery重设File域
2014/04/24 Javascript
JavaScript极简入门教程(三):数组
2014/10/25 Javascript
JavaScript中的变量作用域介绍
2014/12/31 Javascript
jQuery图片左右滚动代码 有左右按钮实例
2016/06/20 Javascript
AngularJS中directive指令使用之事件绑定与指令交互用法示例
2016/11/22 Javascript
JavaScript实现大图轮播效果
2017/01/11 Javascript
AngularJS中$http的交互问题
2017/03/29 Javascript
Vue实现手机扫描二维码预览页面效果
2020/05/28 Javascript
解决Vue使用bus总线时,第一次路由跳转时数据没成功传递问题
2020/07/28 Javascript
Python简单计算文件夹大小的方法
2015/07/14 Python
Python标准库笔记struct模块的使用
2018/02/22 Python
python实现kmp算法的实例代码
2019/04/03 Python
Python爬虫 urllib2的使用方法详解
2019/09/23 Python
Python 实现训练集、测试集随机划分
2020/01/08 Python
Python实现图像的垂直投影示例
2020/01/17 Python
PyCharm上安装Package的实现(以pandas为例)
2020/09/18 Python
HTML5 中新的全局属性(整理)
2013/07/31 HTML / CSS
Tech21美国/加拿大:英国NO.1防摔保护壳品牌
2018/01/20 全球购物
俄罗斯EPL钻石珠宝店:ЭПЛ
2019/10/22 全球购物
NULL是什么,它是怎么定义的
2015/05/09 面试题
历史系毕业生自荐信
2013/10/28 职场文书
应届毕业生的自我鉴定
2013/11/13 职场文书
法学毕业生自我鉴定
2014/01/31 职场文书
小学毕业感言50字
2014/02/16 职场文书
祖国在我心中演讲稿400字
2014/05/04 职场文书
淘宝店策划方案
2014/06/07 职场文书
2014年监理工作总结范文
2014/11/17 职场文书
初中信息技术教学计划
2015/01/22 职场文书
毕业论文致谢范文
2015/05/14 职场文书