如何使用Python实现多线程

发布时间:2021-04-13 10:06:05 作者:小新
来源:亿速云 阅读:173

这篇文章将为大家详细讲解有关如何使用Python实现多线程,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

本文实例讲述了Python实现多线程的两种方式。分享给大家供大家参考,具体如下:

目前python 提供了几种多线程实现方式 thread,threading,multithreading ,其中thread模块比较底层,而threading模块是对thread做了一些包装,可以更加方便的被使用。

2.7版本之前python对线程的支持还不够完善,不能利用多核CPU,但是2.7版本的python中已经考虑改进这点,出现了multithreading  模块。threading模块里面主要是对一些线程的操作对象化,创建Thread的class。一般来说,使用线程有两种模式:

本文介绍两种实现方法。

第一种 创建函数并且传入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()

执行结果:

starting at: 2018-08-29 09:34:16
start loop 0 at: 2018-08-29 09:34:16
start loop 1 at: 2018-08-29 09:34:16
start loop 2 at: 2018-08-29 09:34:16start loop 3 at: 2018-08-29 09:34:16

start loop 4 start loopat:  52018-08-29 09:34:16
at: 2018-08-29 09:34:16
start loop 6 at: start loop2018-08-29 09:34:16
7 at: 2018-08-29 09:34:16
start loop 8 start loopat: 9  2018-08-29 09:34:16at:
 2018-08-29 09:34:16
looploop 1 done at: 2018-08-29 09:34:18
 0 done at: 2018-08-29 09:34:18
loop 3 done at: 2018-08-29 09:34:18
looploop 5 done at: 2018-08-29 09:34:18
 2 done at: 2018-08-29 09:34:18
loop 4 done at: 2018-08-29 09:34:18
looploop 7 done at: 2018-08-29 09:34:18
 6 done at: 2018-08-29 09:34:18
looploop  98  done at:done at:  2018-08-29 09:34:182018-08-29 09:34:18

all Done at: 2018-08-29 09:34:18

第二种 创建继承自Thread的类

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

执行结果:

starting at: 2018-08-29 09:36:02
start loop 0 at: Wed Aug 29 09:36:02 2018
start loop 1 at: Wed Aug 29 09:36:02 2018
start loop 2 at: Wed Aug 29 09:36:02 2018
start loop 3 at: Wed Aug 29 09:36:02 2018
start loop 4 at: Wed Aug 29 09:36:02 2018
 start loop 5 at: Wed Aug 29 09:36:02 2018
start loop 6 at: Wed Aug 29 09:36:02 2018
start loop 7 at: Wed Aug 29 09:36:02 2018
start loop 8 at: Wed Aug 29 09:36:02 2018
start loop 9 at: Wed Aug 29 09:36:02 2018
loop 0 done at: Wed Aug 29 09:36:04 2018
loop 1 done at: Wed Aug 29 09:36:04 2018
looploop  23  done at:done at:  Wed Aug 29 09:36:04 2018Wed Aug 29 09:36:04 2018

loop 4 done at: Wed Aug 29 09:36:04 2018
loop 5 done at: Wed Aug 29 09:36:04 2018
loop 6 done at: Wed Aug 29 09:36:04 2018
looploop loop 8 9 7 done at: done at: done at: Wed Aug 29 09:36:04 2018 Wed Aug 29 09:36:04 2018
Wed Aug 29 09:36:04 2018

all Done at: 2018-08-29 09:36:04

关于“如何使用Python实现多线程”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. python实现自动化报表:用于Excel或多线程等
  2. PHP多线程的使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python 多线程

上一篇:python如何实现Zabbix-API监控

下一篇:如何使用Python实现一个栈判断括号是否平衡

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》