gpt4 book ai didi

python - ThreadPoolExecutor 中的 worker 并不是真正的守护进程

转载 作者:太空狗 更新时间:2023-10-29 17:47:33 25 4
gpt4 key购买 nike

我想不通的是,尽管 ThreadPoolExecutor 使用守护进程,但即使主线程退出,它们仍会运行。

我可以在 python3.6.4 中提供一个最小的例子:

import concurrent.futures
import time


def fn():
while True:
time.sleep(5)
print("Hello")


thread_pool = concurrent.futures.ThreadPoolExecutor()
thread_pool.submit(fn)
while True:
time.sleep(1)
print("Wow")

主线程和工作线程都是死循环。因此,如果我使用 KeyboardInterrupt 终止主线程,我预计整个程序也会终止。但实际上工作线程仍然在运行,即使它是一个守护线程。

ThreadPoolExecutor 的源代码确认工作线程是守护线程:

t = threading.Thread(target=_worker,
args=(weakref.ref(self, weakref_cb),
self._work_queue))
t.daemon = True
t.start()
self._threads.add(t)

此外,如果我手动创建一个守护线程,它就像一个魅力:

from threading import Thread
import time


def fn():
while True:
time.sleep(5)
print("Hello")


thread = Thread(target=fn)
thread.daemon = True
thread.start()
while True:
time.sleep(1)
print("Wow")

所以我真的无法理解这种奇怪的行为。

最佳答案

突然……我找到了原因。根据 ThreadPoolExecutor 的更多源代码:

# Workers are created as daemon threads. This is done to allow the interpreter
# to exit when there are still idle threads in a ThreadPoolExecutor's thread
# pool (i.e. shutdown() was not called). However, allowing workers to die with
# the interpreter has two undesirable properties:
# - The workers would still be running during interpreter shutdown,
# meaning that they would fail in unpredictable ways.
# - The workers could be killed while evaluating a work item, which could
# be bad if the callable being evaluated has external side-effects e.g.
# writing to a file.
#
# To work around this problem, an exit handler is installed which tells the
# workers to exit when their work queues are empty and then waits until the
# threads finish.

_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)
for t, q in items:
t.join()

atexit.register(_python_exit)

有一个退出处理程序,它将加入所有未完成的 worker...

关于python - ThreadPoolExecutor 中的 worker 并不是真正的守护进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49992329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com