Python线程池ThreadPoolExecutor源码分析
先看个例子:
import time
from concurrent.futures import ThreadPoolExecutor
def foo():
print('enter at {} ...'.format(time.strftime('%X')))
time.sleep(5)
print('exit at {} ...'.format(time.strftime('%X')))
executor = ThreadPoolExecutor()
executor.submit(foo)
executor.shutdown()
执行结果:
enter at 16:20:31 ...
exit at 16:20:36 ...
shutdown(wait=True) 方法默认阻塞当前线程,等待子线程执行完毕。即使 shutdown(wait=Fasle)也只是非阻塞的关闭线程池,线程池中正在执行任务的子线程并不会被马上停止,而是会继续执行直到执行完毕。尝试在源码中给新开启的子线程调用t.join(0)来立马强制停止子线程t,也不行,到底是什么原因保证了线程池中的线程在关闭线程池时,线程池中正在执行任务的子线程们不会被关闭呢?
看一下ThreadPoolExecutor源码:
class ThreadPoolExecutor(_base.Executor):
def __init__(self, max_workers=None, thread_name_prefix=''):
"""Initializes a new ThreadPoolExecutor instance.
Args:
max_workers: The maximum number of threads that can be used to
execute the given calls.
thread_name_prefix: An optional name prefix to give our threads.
"""
if max_workers is None:
# Use this number because ThreadPoolExecutor is often
# used to overlap I/O instead of CPU work.
max_workers = (os.cpu_count() or 1) * 5
if max_workers <= 0:
raise ValueError("max_workers must be greater than 0")
self._max_workers = max_workers
self._work_queue = queue.Queue()
self._threads = set()
self._shutdown = False
self._shutdown_lock = threading.Lock()
self._thread_name_prefix = thread_name_prefix
def submit(self, fn, *args, **kwargs):
with self._shutdown_lock:
if self._shutdown:
raise RuntimeError('cannot schedule new futures after shutdown')
f = _base.Future()
# 把目标函数f包装成worker对象,执行worker.run()会调用f()
w = _WorkItem(f, fn, args, kwargs)
# 把worker对象放入到队列中
self._work_queue.put(w)
# 开启一个新的线程不断的从queue中获取worker对象,获取到则调用worker.run()
self._adjust_thread_count()
return f
submit.__doc__ = _base.Executor.submit.__doc__
def _adjust_thread_count(self):
# 当执行del executor时,这个回调方法会被调用,也就是说当executor对象被垃圾回收时调用
def weakref_cb(_, q=self._work_queue):
q.put(None)
num_threads = len(self._threads)
if num_threads < self._max_workers:
thread_name = '%s_%d' % (self._thread_name_prefix or self,
num_threads)
# 把_worker函数作为新线程的执行函数
t = threading.Thread(name=thread_name, target=_worker,
args=(weakref.ref(self, weakref_cb),
self._work_queue))
t.daemon = True
t.start()
self._threads.add(t)
# 这一步很重要,是确保该线程t不被t.join(0)强制中断的关键。具体查看_python_exit函数
_threads_queues[t] = self._work_queue
def shutdown(self, wait=True):
with self._shutdown_lock:
self._shutdown = True
self._work_queue.put(None)
if wait:
for t in self._threads:
t.join()
shutdown.__doc__ = _base.Executor.shutdown.__doc__
submit(func) 干了两件事:
- 把worker放入queue中
- 开启一个新线程不断从queue中取出woker,执行woker.run(),即执行func()
_adjust_thread_count()干了两件事:
开启一个新线程执行_worker函数,这个函数的作用就是不断去queue中取出worker, 执行woker.run(),即执行func()
把新线程跟队列queue绑定,防止线程被join(0)强制中断。
来看一下_worker函数源码:
def _worker(executor_reference, work_queue):
try:
while True:
# 不断从queue中取出worker对象
work_item = work_queue.get(block=True)
if work_item is not None:
# 执行func()
work_item.run()
# Delete references to object. See issue16284
del work_item
continue
# 从弱引用对象中返回executor
executor = executor_reference()
# Exit if:
# - The interpreter is shutting down OR
# - The executor that owns the worker has been collected OR
# - The executor that owns the worker has been shutdown.
# 当executor执行shutdown()方法时executor._shutdown为True,同时会放入None到队列,
# 当work_item.run()执行完毕时,又会进入到下一轮循环从queue中获取worker对象,但是
# 由于shutdown()放入了None到queue,因此取出的对象是None,从而判断这里的if条件分支,
# 发现executor._shutdown是True,又放入一个None到queue中,是来通知其他线程跳出while循环的
# shutdown()中的添加None到队列是用来结束线程池中的某一个线程的,这个if分支中的添加None
# 队列是用来通知其他线程中的某一个线程结束的,这样连锁反应使得所有线程执行完func中的逻辑后都会结束
if _shutdown or executor is None or executor._shutdown:
# Notice other workers
work_queue.put(None)
return
del executor
except BaseException:
_base.LOGGER.critical('Exception in worker', exc_info=True)
可以看出,这个 _worker方法的作用就是在新新线程中不断获得queue中的worker对象,执行worker.run()方法,执行完毕后通过放入None到queue队列的方式来通知其他线程结束。
再来看看_adjust_thread_count()方法中的_threads_queues[t] = self._work_queue这个操作是如何实现防止join(0)的操作强制停止正在执行的线程的。
import atexit
_threads_queues = weakref.WeakKeyDictionary()
_shutdown = False
def _python_exit():
global _shutdown
_shutdown = True
items = list(_threads_queues.items())
for t, q in items:
q.put(None)
# 取出_threads_queues中的线程t,执行t.join()强制等待子线程完成
for t, q in items:
t.join()
atexit.register(_python_exit)
这个atexit模块的作用是用来注册一个函数,当MainThread中的逻辑执行完毕时,会执行注册的这个_python_exit函数。然后执行_python_exit中的逻辑,也就是说t.join()会被执行,强制阻塞。这里好奇,既然是在MainThread结束后执行,那这个t.join()是在什么线程中被执行的呢。其实是一个叫_DummyThread线程的虚拟线程中执行的。
import atexit
import threading
import weakref
import time
threads_queues = weakref.WeakKeyDictionary()
def foo():
print('enter at {} ...'.format(time.strftime('%X')))
time.sleep(5)
print('exit at {} ...'.format(time.strftime('%X')))
def _python_exit():
items = list(threads_queues.items())
print('current thread in _python_exit --> ', threading.current_thread())
for t, _ in items:
t.join()
atexit.register(_python_exit)
if __name__ == '__main__':
t = threading.Thread(target=foo)
t.setDaemon(True)
t.start()
threads_queues[t] = foo
print(time.strftime('%X'))
t.join(timeout=2)
print(time.strftime('%X'))
t.join(timeout=2)
print(time.strftime('%X'))
print('current thread in main -->', threading.current_thread())
print(threading.current_thread(), 'end')
执行结果:
enter at 17:13:44 ...
17:13:44
17:13:46
17:13:48
current thread in main --> <_MainThread(MainThread, started 12688)>
<_MainThread(MainThread, started 12688)> end
current thread in _python_exit --> <_DummyThread(Dummy-2, started daemon 12688)>
exit at 17:13:49 ...
从这个例子可以看到,当线程t开启时foo函数阻塞5秒,在MainThread中2次调用t.join(timeout=2),分别的等待了2秒,总等待时间是4秒,但是当执行第二个t.join(timeout=2)后,线程t依然没有被强制停止,然后主线执行完毕,然后_python_exit方法被调用,在_DummyThread线程中由调用t.join(),继续等待子线程t的执行完毕,直到线程t打印exit at 17:13:49 ...才执行完毕。
总结:
join()是可以被一个线程多次调用的,相当是多次等待的叠加。把_python_exit函数注册到atexit模块后,其他线程即使企图调用t.jion(n)来终止线程t也不起作用,因为_python_exit总是在最后执行时调用t.jion()来保证让线程t执行完毕,而不是被中途强制停止。
Python线程池ThreadPoolExecutor源码分析的更多相关文章
- Java核心复习——线程池ThreadPoolExecutor源码分析
一.线程池的介绍 线程池一种性能优化的重要手段.优化点在于创建线程和销毁线程会带来资源和时间上的消耗,而且线程池可以对线程进行管理,则可以减少这种损耗. 使用线程池的好处如下: 降低资源的消耗 提高响 ...
- 线程池ThreadPoolExecutor源码分析
在阿里编程规约中关于线程池强制了两点,如下: [强制]线程资源必须通过线程池提供,不允许在应用中自行显式创建线程.说明:使用线程池的好处是减少在创建和销毁线程上所消耗的时间以及系统资源的开销,解决资源 ...
- java线程池ThreadPoolExector源码分析
java线程池ThreadPoolExector源码分析 今天研究了下ThreadPoolExector源码,大致上总结了以下几点跟大家分享下: 一.ThreadPoolExector几个主要变量 先 ...
- 【Java并发编程】21、线程池ThreadPoolExecutor源码解析
一.前言 JUC这部分还有线程池这一块没有分析,需要抓紧时间分析,下面开始ThreadPoolExecutor,其是线程池的基础,分析完了这个类会简化之后的分析,线程池可以解决两个不同问题:由于减少了 ...
- Java并发之线程池ThreadPoolExecutor源码分析学习
线程池学习 以下所有内容以及源码分析都是基于JDK1.8的,请知悉. 我写博客就真的比较没有顺序了,这可能跟我的学习方式有关,我自己也觉得这样挺不好的,但是没办法说服自己去改变,所以也只能这样想到什么 ...
- 线程池ThreadPoolExecutor源码解读研究(JDK1.8)
一.什么是线程池 为什么要使用线程池?在多线程并发开发中,线程的数量较多,且每个线程执行一定的时间后就结束了,下一个线程任务到来还需要重新创建线程,这样线程数量特别庞大的时候,频繁的创建线程和销毁线程 ...
- ThreadPoolExecutor(线程池)源码分析
1. 常量和变量 private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0)); // 高3位为线程池的运行状态,低29 ...
- Java调度线程池ScheduledThreadPoolExecutor源码分析
最近新接手的项目里大量使用了ScheduledThreadPoolExecutor类去执行一些定时任务,之前一直没有机会研究这个类的源码,这次趁着机会好好研读一下. 该类主要还是基于ThreadPoo ...
- Java并发包源码学习系列:线程池ThreadPoolExecutor源码解析
目录 ThreadPoolExecutor概述 线程池解决的优点 线程池处理流程 创建线程池 重要常量及字段 线程池的五种状态及转换 ThreadPoolExecutor构造参数及参数意义 Work类 ...
随机推荐
- IDEA 自动化配置
# IDEA maven web项目:http://www.cnblogs.com/Sinte-Beuve/p/5730553.html # IDEA 数据库自动化 ## 功能 ① SQL 代码自动感 ...
- php 将一个二维数组中两个相同的value 相同 指定值相加
array(3) { [0]=> array(7) { ["mlid"]=> int(1) ["num"]=> int(1) ["c ...
- Unpaired/Partially/Unsupervised Image Captioning
这篇涉及到以下三篇论文: Unpaired Image Captioning by Language Pivoting (ECCV 2018) Show, Tell and Discriminate: ...
- 苹果ios用js的Date()出现NaN问题解决办法
原文:苹果ios用js的Date()出现NaN问题解决办法 ios使用如下方法获得NaN,安卓手机则是正常计算,解决方法是换个这个时间的格式 new Date("2017-04-28 23: ...
- Win10《芒果TV》内测版现已支持在国行版 Xbox One 下载体验
Win10<芒果TV>内测版现已支持在国行版 Xbox One 下载体验 关于国行为什么无法从商店下载安装芒果TV内测版(来自微软) 1.截至到目前<芒果TV - Preview&g ...
- UWP应用使用SQLite库的方法
1.打开菜单“工具”-“扩展和更新”,选择“联机”选项,然后搜索“SQLite”,在搜索结果中找到“SQLite for Universal Windows Platform”,然后点击下载进行安装. ...
- memcached对中文key的支持问题
默认的memcached客户端对非ANSI的key存取时会有问题,有2种方式解决: 1 在get和set前将缓存的key进行UrlEncode 2 修改memcached.config文件 <e ...
- 【Python】:用python做下百度2014笔试题
国庆节最后一天,明天就要上班了,闲来无事做做百度2014笔试题,好久没用过C++了,索性就用python简单的写一下,体验下题目难度.题目是从[大卫David]那里copy过来的. 1.给定任意一个正 ...
- C#判断是否相等
判断对象是否相等,因为平时用的一般都是int.bool.string类型的数据是否相等. 同时也是只判断它们的“值”是否相等.于是都是用“==”或是Equal()方法来判断. 但这并不能判断出是否为同 ...
- 自定义QT窗口部件外观之QStyle
自定义QT窗口部件外观 重新定义Qt内置窗口部件的外观常用的方法有两种:一是通过子类化QStyle 类或者预定义的一个样式,例如QWindowStyle,来定制应用程序的观感:二是使用Qt样式表. Q ...