python实现多线程的两种方式

 更新时间:2016年05月22日 08:24:01   作者:赵浮云的blog  
这篇文章主要为大家详细介绍了python实现多线程的两种方式,感兴趣的朋友可以参考一下

目前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()

以上就是本文的全部内容吗,希望对大家学习python程序设计有所帮助。

相关文章

  • python实现的希尔排序算法实例

    python实现的希尔排序算法实例

    这篇文章主要介绍了python实现的希尔排序算法,实例分析了基于Python实现希尔排序的相关技巧,需要的朋友可以参考下
    2015-07-07
  • 基于python进行桶排序与基数排序的总结

    基于python进行桶排序与基数排序的总结

    今天小编就为大家分享一篇基于python进行桶排序与基数排序的总结,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • Python变量及数据类型用法原理汇总

    Python变量及数据类型用法原理汇总

    这篇文章主要介绍了Python变量及数据类型用法原理汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • python基础之内置函数

    python基础之内置函数

    这篇文章主要介绍了python内置函数,实例分析了Python中返回一个返回值与多个返回值的方法,需要的朋友可以参考下
    2021-10-10
  • PyTorch搭建ANN实现时间序列风速预测

    PyTorch搭建ANN实现时间序列风速预测

    这篇文章主要为大家介绍了PyTorch搭建ANN实现时间序列风速预测,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Python的type函数结果你知道嘛

    Python的type函数结果你知道嘛

    这篇文章主要为大家介绍了Python的type函数结果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • 使用python制作一个截图小工具

    使用python制作一个截图小工具

    这篇文章主要讨论了我们如何使用Python编程语言进行截图,我们看到了如何使用pyautogui 模块与save() 函数和其他模块,如NumPy 和OpenCV ,文中通过代码示例介绍的非常详细,需要的朋友可以参考下
    2023-12-12
  • 查看Python安装路径几种方法

    查看Python安装路径几种方法

    在使用python的时候,有时候会需要找到python包的安装位置,本文主要介绍了查看Python安装路径几种方法,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • Python如何同时读写Excel

    Python如何同时读写Excel

    这篇文章主要介绍了Python如何同时读写Excel问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • 一些Python中的二维数组的操作方法

    一些Python中的二维数组的操作方法

    这篇文章主要介绍了一些Python中的二维数组的操作方法,是Python学习当中的基础知识,需要的朋友可以参考下
    2015-05-05

最新评论