多线程的建立与使用


目录

  1. 生成线程的三种方法
  2. 单线程与多线程对比
  3. 守护线程的设置

1 生成线程的三种方法

三种方式分别为:

  1. 创建一个Thread实例,传给它一个函数
  2. 创建一个Thread实例,传给它一个可调用的类实例
  3. 派生Thread的子类,并创建子类的实例
# There are three ways to create a thread
# The first is create a thread instance, and pass a function
# The second one is create a thread instance, and pass the callable instance(obj)
# The third one is create a subclass of Thread, then generate an instance

1.1 创建Thread实例并传递一个函数

在Thread类实例化时,将函数(及其参数)传入,下面的例子中,在实例化Thread类的时候传入了一个函数loop及其参数,生成了一个线程的实例,再启动线程。

 # Method one: pass function
from threading import Thread
from time import sleep, ctime loops = [4, 2] def loop(nloop, nsec):
print('start loop', nloop, 'at', ctime())
sleep(nsec)
print('loop', nloop, 'done at:', ctime()) def main():
print('starting at:', ctime())
threads = []
nloops = range(len(loops)) # Create all threads
for i in nloops:
t = Thread(target=loop, args=(i, loops[i]))
threads.append(t)
for i in threads:
i.start()
for i in threads:
i.join()
print('All DONE at:', ctime()) if __name__ == '__main__':
main()

1.2 创建Thread实例并传递一个可调用的类实例

同样,在Thread类实例化时,类(及其参数)传入,下面的例子中,在实例化Thread类的时候传入了一个可调用(__call__函数使类变为可调用)的类实例ThreadFunc,且完成初始化传给target=,此时生成了一个线程的实例,随后启动线程。

 # Method two: pass object

 from threading import Thread
from time import sleep, ctime loops = [4, 2] class ThreadFunc(object):
def __init__(self, func, args, name=''):
self.name = name
self.func = func
self.args = args def __call__(self):
self.func(*self.args) def loop(nloop, nsec):
print('start loop', nloop, 'at', ctime())
sleep(nsec)
print('loop', nloop, 'done at:', ctime()) def main():
print('starting at:', ctime())
threads = []
nloops = range(len(loops)) # Create all threads
for i in nloops:
t = Thread(target=ThreadFunc(loop, (i, loops[i]), loop.__name__))
threads.append(t)
for i in threads:
i.start()
for i in threads:
i.join()
print('All DONE at:', ctime()) if __name__ == '__main__':
main()

1.3 派生Thread子类并生成子类实例

以Thread为基类,派生出一个子类,在子类中重定义run方法,最终生成一个线程实例进行调用。下面的例子中,生成了一个子类MyThread,同时重定义run函数,在初始化时接收一个func参数作为调用函数。

 # Method three: by subclass

 from threading import Thread
from time import sleep, ctime loops = [4, 2] class MyThread(Thread):
def __init__(self, func, args, name=''):
Thread.__init__(self)
self.name = name
self.func = func
self.args = args def run(self):
self.func(*self.args) def loop(nloop, nsec):
print('start loop', nloop, 'at', ctime())
sleep(nsec)
print('loop', nloop, 'done at:', ctime()) def main():
print('starting at:', ctime())
threads = []
nloops = range(len(loops)) # Create all threads
for i in nloops:
t = MyThread(loop, (i, loops[i]), loop.__name__)
threads.append(t)
for i in threads:
i.start()
for i in threads:
i.join()
print('All DONE at:', ctime()) if __name__ == '__main__':
main()

2 单线程与多线程对比

利用单线程与多线程分别进行斐波那契,阶乘与累加操作,此处加入了sleep进行计算延迟,这是由于Python解释器的GIL特性使得Python对于计算密集型的函数并没有优势,而对于I/O密集型的函数则优化性能较好。

 from threading import Thread
import time
from time import ctime class MyThread(Thread):
"""
Bulid up a Module to make this subclass more general
And get return value by add a function named 'getResult()'
"""
def __init__(self, func, args, name=''):
Thread.__init__(self)
self.name = name
self.func = func
self.args = args def getResult(self):
return self.res def run(self):
print('Starting', self.name, 'at:', ctime())
# Call function here and calculate the running time
self.res = self.func(*self.args)
print(self.name, 'finished at:', ctime()) def fib(x):
time.sleep(0.005)
if x < 2:
return 1
return (fib(x-2) + fib(x-1)) def fac(x):
time.sleep(0.1)
if x < 2:
return 1
return (x * fac(x-1)) def sumx(x):
time.sleep(0.1)
if x < 2:
return 1
return (x + sumx(x-1)) funcs = [fib, fac, sumx]
n = 12 def main():
nfuncs = range(len(funcs))
print('***SINGLE THREADS')
for i in nfuncs:
print('Starting', funcs[i].__name__, 'at:', ctime())
print(funcs[i](n))
print(funcs[i].__name__, 'finished at:', ctime()) print('\n***MULTIPLE THREADS')
threads = []
for i in nfuncs:
t = MyThread(funcs[i], (n,), funcs[i].__name__)
threads.append(t)
for i in nfuncs:
threads[i].start()
for i in nfuncs:
threads[i].join()
print(threads[i].getResult())
print('All DONE') if __name__ == '__main__':
main()

第 1-24 行,导入所需模块,并派生线程的子类,定义一个返回函数用于返回结果,

第 26-45 行,分别定义斐波那契,阶乘与累加函数,

最后在主函数中分别运行两种模式的计算,得到结果

***SINGLE THREADS
Starting fib at: Tue Aug 1 20:17:47 2017
233
fib finished at: Tue Aug 1 20:17:50 2017
Starting fac at: Tue Aug 1 20:17:50 2017
479001600
fac finished at: Tue Aug 1 20:17:51 2017
Starting sumx at: Tue Aug 1 20:17:51 2017
78
sumx finished at: Tue Aug 1 20:17:52 2017 ***MULTIPLE THREADS
Starting fib at: Tue Aug 1 20:17:52 2017
Starting fac at: Tue Aug 1 20:17:52 2017
Starting sumx at: Tue Aug 1 20:17:52 2017
sumx finished at: Tue Aug 1 20:17:53 2017
fac finished at: Tue Aug 1 20:17:53 2017
fib finished at: Tue Aug 1 20:17:54 2017
233
479001600
78
All DONE

从结果中可以看出单线程耗时5秒,而多线程耗时2秒,优化了程序的运行速度。

Note: 再次注明,Python的多线程并未对计算性能有所提升,此处是由于加入了sleep的等待,因此使得Python的GIL发挥其优势。

守护线程的设置

守护线程一般属于后台无限循环的程序,主线程会在所有非守护线程结束之后,自动关闭还在运行的守护线程,而不会等待它的无限循环完成。守护线程的设置只要将线程实例的daemon设置为True即可,默认是False。

 import threading
import time
import random class MyThread(threading.Thread):
def __init__(self, count):
threading.Thread.__init__(self)
print('%s: There are %d numbers need to be counted' % (self.name, count))
self.count = count def run(self):
name = self.name
for i in range(0, self.count):
print('%s counts %d' % (name, i))
time.sleep(1)
print('%s finished' % name) def main():
print('-------Starting-------')
count = random.randint(4, 7)
t_1 = MyThread(count*count)
t_2 = MyThread(count)
t_1.daemon = True
t_2.daemon = False
t_1.start()
t_2.start()
time.sleep(3)
print('---Main Thread End---') if __name__ == '__main__':
main()

第 5-16 行,派生一个线程子类,run函数实现每秒计数一次的功能

第 19-29 行,在主函数中,分别生成两个线程实例,其中t_1计数量较大,设为守护线程,t_2计数量较小,设为非守护线程,线程均不挂起,主线程在3秒后会结束。

运行得到结果

-------Starting-------
Thread-1: There are 36 numbers need to be counted
Thread-2: There are 6 numbers need to be counted
Thread-1 counts 0
Thread-2 counts 0
Thread-1 counts 1
Thread-2 counts 1
Thread-1 counts 2
Thread-2 counts 2
Thread-1 counts 3
---Main Thread End---
Thread-2 counts 3
Thread-1 counts 4
Thread-2 counts 4
Thread-1 counts 5
Thread-2 counts 5
Thread-1 counts 6
Thread-2 finished

运行主函数后可以看到,两个线程启动后,计数3次时,主线程已经结束,而这时由于t_2是非守护线程,因此主线程挂起等待t_2的计数结束之后,杀掉了还在运行的守护线程t_1,并且退出了主线程。

相关阅读


1. 基本概念

2. threading 模块

参考链接


《Python 核心编程 第3版》

最新文章

  1. VS2012 还原默认设置
  2. .NET开发者如何愉快的进行微信公众号开发
  3. tyvj[1087]sumsets
  4. Javascript的匿名函数与自执行
  5. [Java 基础] 使用java.util.zip包压缩和解压缩文件
  6. Eclipse新建工程编译R cannot be resolved to a variable问题
  7. extjs grid 分页
  8. Thread线程初探
  9. linux后端运行
  10. android 中文件加密 解密 算法实战
  11. linux 磁盘空间扩容 vg(+pv) lv(+空间) lv(缩减磁盘空间)
  12. 防止微信浏览器video标签全屏的问题
  13. 3分钟搞掂Set集合
  14. wget Mac OS 下安装
  15. 半夜两点灵光一现想出来的一个demo
  16. Python3学习笔记16-错误和异常
  17. smarty详细使用教程(韩顺平smarty模板技术笔记)
  18. 微信小程序 - 骨架屏
  19. PHP垃圾回收机制防止内存溢出
  20. 《Tomcat日志系统详解》

热门文章

  1. Linux认知之旅【01 与Linux第一次亲密接触】!
  2. 遍历列表,打印:我叫name,今年age岁,家住dizhi,电话phone(我是通过下标取键得到对应值,有哪位大神来个更简单的)
  3. Kickstart配置文件解析
  4. django通用视图之TemplateView和ListView简单介绍
  5. Android 之高仿微信主界面
  6. HDU 1939 HE IS OFFSIDE
  7. 【bzoj2662】[BeiJing wc2012]冻结 分层图Spfa
  8. hdu 2553 N皇后问题 (DFS)
  9. 洛谷 P3747 [六省联考2017]相逢是问候 解题报告
  10. vue项目--favicon设置以及动态修改favicon