python 多线程两种实现方式
2015-07-15 23:07
856 查看
目前python 提供了几种多线程实现方式 thread,threading,multithreading ,其中thread模块比较底层,而threading模块是对thread做了一些包装,可以更加方便的被使用。
2.7版本之前python对线程的支持还不够完善,不能利用多核CPU,但是2.7版本的python中已经考虑改进这点,出现了multithreading 模块。threading模块里面主要是对一些线程的操作对象化,创建Thread的class。一般来说,使用线程有两种模式:
A 创建线程要执行的函数,把这个函数传递进Thread对象里,让它来执行;
B 继承Thread类,创建一个新的class,将要执行的代码 写到run函数里面。
本文介绍两种实现方法。
第一种 创建函数并且传入Thread 对象中
t.py 脚本内容
import threading,time
from time import sleep, ctime
def now() :
return str( time.strftime( '%Y-%m-%d
%H:%M:%S' , time.localtime() ) )
def test(nloop, nsec):
print 'start loop', nloop, 'at:', now()
sleep(nsec)
print 'loop', nloop, 'done
at:', now()
def main():
print 'starting at:',now()
threadpool=[]
for i in xrange(10):
th = threading.Thread(target= test,args= (i,2))
threadpool.append(th)
for th in threadpool:
th.start()
for th in threadpool :
threading.Thread.join( th )
print 'all Done at:', now()
if __name__ == '__main__':
main()
执行结果:
thclass.py 脚本内容:
import threading ,time
from time import sleep, ctime
def now() :
return str( time.strftime( '%Y-%m-%d
%H:%M:%S' , time.localtime() ) )
class myThread (threading.Thread) :
"""docstring for myThread"""
def __init__(self, nloop, nsec) :
super(myThread, self).__init__()
self.nloop = nloop
self.nsec = nsec
def run(self):
print 'start loop', self.nloop, 'at:', ctime()
sleep(self.nsec)
print 'loop', self.nloop, 'done
at:', ctime()
def main():
thpool=[]
print 'starting at:',now()
for i in xrange(10):
thpool.append(myThread(i,2))
for th in thpool:
th.start()
for th in thpool:
th.join()
print 'all Done at:', now()
if __name__ == '__main__':
main()
执行结果:
2.7版本之前python对线程的支持还不够完善,不能利用多核CPU,但是2.7版本的python中已经考虑改进这点,出现了multithreading 模块。threading模块里面主要是对一些线程的操作对象化,创建Thread的class。一般来说,使用线程有两种模式:
A 创建线程要执行的函数,把这个函数传递进Thread对象里,让它来执行;
B 继承Thread类,创建一个新的class,将要执行的代码 写到run函数里面。
本文介绍两种实现方法。
第一种 创建函数并且传入Thread 对象中
t.py 脚本内容
import threading,time
from time import sleep, ctime
def now() :
return str( time.strftime( '%Y-%m-%d
%H:%M:%S' , time.localtime() ) )
def test(nloop, nsec):
print 'start loop', nloop, 'at:', now()
sleep(nsec)
print 'loop', nloop, 'done
at:', now()
def main():
print 'starting at:',now()
threadpool=[]
for i in xrange(10):
th = threading.Thread(target= test,args= (i,2))
threadpool.append(th)
for th in threadpool:
th.start()
for th in threadpool :
threading.Thread.join( th )
print 'all Done at:', now()
if __name__ == '__main__':
main()
执行结果:
thclass.py 脚本内容:
import threading ,time
from time import sleep, ctime
def now() :
return str( time.strftime( '%Y-%m-%d
%H:%M:%S' , time.localtime() ) )
class myThread (threading.Thread) :
"""docstring for myThread"""
def __init__(self, nloop, nsec) :
super(myThread, self).__init__()
self.nloop = nloop
self.nsec = nsec
def run(self):
print 'start loop', self.nloop, 'at:', ctime()
sleep(self.nsec)
print 'loop', self.nloop, 'done
at:', ctime()
def main():
thpool=[]
print 'starting at:',now()
for i in xrange(10):
thpool.append(myThread(i,2))
for th in thpool:
th.start()
for th in thpool:
th.join()
print 'all Done at:', now()
if __name__ == '__main__':
main()
执行结果:
相关文章推荐
- My Python笔记
- 【Python】Webpy 源码学习(转)
- 通过Python编写模拟器对方案进行测试和分析的方法_Prj001
- 推荐python的ORM框架peewee(转)
- 学习python之走进Scrapy
- 采集文献不认识单词上传扇贝网
- python技巧总结--持续更新
- Python垃圾回收机制
- Python.Scrapy.14-scrapy-source-code-analysis-part-4
- python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码
- python实现爬虫下载美女图片
- 【Python】容器类
- 【Python 学习手册笔记】列表与字典
- A Neural Network in 11 lines of Python
- VS2012 + SWIG Python
- python字符串/元组/列表/字典互转
- Python.Scrapy.12-scrapy-source-code-analysis-part-2
- Python实现快速多线程ping的方法
- python_metaclass浅析
- Python新式类和经典类的区别