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类继承用法实例分析
Oct 10 Python
Python实现截屏的函数
Jul 26 Python
基于Python实现的ID3决策树功能示例
Jan 02 Python
浅谈flask截获所有访问及before/after_request修饰器
Jan 18 Python
Python之csv文件从MySQL数据库导入导出的方法
Jun 21 Python
TensorFlow打印tensor值的实现方法
Jul 27 Python
使用Python快速制作可视化报表的方法
Feb 03 Python
详解js文件通过python访问数据库方法
Mar 03 Python
Pytorch之view及view_as使用详解
Dec 31 Python
Ubuntu18.04安装 PyCharm并使用 Anaconda 管理的Python环境
Apr 08 Python
Python 日期时间datetime 加一天,减一天,加减一小时一分钟,加减一年
Apr 16 Python
如何学习Python time模块
Jun 03 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微信公众平台开发之获取用户基本信息
2015/08/17 PHP
php+ajax实现异步上传文件或图片功能
2017/07/18 PHP
js函数调用常用方法详解
2012/12/03 Javascript
调整小数的格式保留小数点后两位
2014/05/14 Javascript
jquery实现翻动fadeIn显示的方法
2015/03/05 Javascript
JavaScript中的对象与JSON
2015/07/03 Javascript
JS+CSS实现的简单折叠展开多级菜单效果
2015/09/12 Javascript
js+canvas绘制矩形的方法
2016/01/28 Javascript
bootstrap多种样式进度条展示
2016/12/20 Javascript
JS实现线性表的顺序表示方法示例【经典数据结构】
2017/04/11 Javascript
JS库之wow.js使用方法
2017/09/14 Javascript
js canvas画布实现高斯模糊效果
2018/11/27 Javascript
vue+openlayers绘制省市边界线
2020/12/24 Vue.js
[01:16:13]DOTA2-DPC中国联赛 正赛 SAG vs Dragon BO3 第一场 2月22日
2021/03/11 DOTA
编写Python脚本来实现最简单的FTP下载的教程
2015/05/04 Python
Flask框架中密码的加盐哈希加密和验证功能的用法详解
2016/06/07 Python
Python开发中爬虫使用代理proxy抓取网页的方法示例
2017/09/26 Python
Python弹出输入框并获取输入值的实例
2019/06/18 Python
python3中类的继承以及self和super的区别详解
2019/06/26 Python
pip install python 快速安装模块的教程图解
2019/10/08 Python
简单了解Python读取大文件代码实例
2019/12/18 Python
Tensorflow实现多GPU并行方式
2020/02/03 Python
Python创建空列表的字典2种方法详解
2020/02/13 Python
django 扩展user用户字段inlines方式
2020/03/30 Python
Idea安装python显示无SDK问题解决方案
2020/08/12 Python
如何在python中处理配置文件代码实例
2020/09/27 Python
CSS3的颜色渐变效果的示例代码
2017/09/29 HTML / CSS
html5将图片转换成base64的实例代码
2016/09/21 HTML / CSS
Perry Ellis官网:美国男士品味服装
2016/12/09 全球购物
英国潮流网站:END.(全球免邮)
2017/01/16 全球购物
Nebula美国官网:便携式投影仪
2019/03/15 全球购物
迪卡侬中国官网:Decathlon中国
2020/08/10 全球购物
编辑求职信样本
2013/12/16 职场文书
工作违纪检讨书
2014/02/17 职场文书
Python 实现绘制子图及子图刻度的变换等问题
2021/05/31 Python
python实现股票历史数据可视化分析案例
2021/06/10 Python