threading.current_thread() 中的 Dummy 是什么?

What is Dummy in threading.current_thread()?

我正在尝试理解此 How to use QThread correctly in pyqt with moveToThread()?

中的代码

部分有:

mainwin.__init__         : MainThread, 140221684574016,
GenericWorker.__init__   : MainThread, 140221684574016,
GenericWorker.run        : Dummy-1, 140221265458944,
mainwin.addBatch         : Dummy-1, 140221265458944,
mainwin.add              : Dummy-1, 140221265458944,
mainwin.add              : Dummy-1, 140221265458944,
mainwin.add              : Dummy-1, 140221265458944,
mainwin.add              : Dummy-1, 140221265458944,
mainwin.add              : Dummy-1, 140221265458944,
mainwin.add              : Dummy-1, 140221265458944

我很感兴趣,Dummy -1 元素到底是什么,我的理解是这些是工作线程在做一份工作,但它们会保留 "alive" 永远?或者他们会自己被垃圾收集。如果添加批处理完成 10k 次,代码输出是否会有 10k Dummy-1 项? 我之所以问,是因为我在使用子类 Qthreads 时看到了类似的输出(在 Eclispe 中使用 Pydev,而在调试模式下使用 运行 代码)。

我不确定这是否意味着某种泄漏(线程泄漏)最终会消耗大量资源。

QThread 不是 Qt 的 线程,而是一个 class 用于处理每个 OS 线程的本机线程python 的模块,这在 docs:

中提到

The QThread class provides a platform-independent way to manage threads.

所以当使用 threading.current_thread() python 尝试获取本机线程但作为 docs point out (and you can also see it in the source code):

Return the current Thread object, corresponding to the caller’s thread of control. If the caller’s thread of control was not created through the threading module, a dummy thread object with limited functionality is returned.

因为 QThread 创建了一个未被线程模块处理的线程,这将 return 一个代表该线程的虚拟线程。

这些存储在线程模块内的字典中,因此不会被 GC 删除,也不是泄漏。 docs:

中提到了这一点

There is the possibility that “dummy thread objects” are created. These are thread objects corresponding to “alien threads”, which are threads of control started outside the threading module, such as directly from C code. Dummy thread objects have limited functionality; they are always considered alive and daemonic, and cannot be join()ed. They are never deleted, since it is impossible to detect the termination of alien threads.

并且在 source code:

# Dummy thread class to represent threads not started here.
# These aren't garbage collected when they die, nor can they be waited for.
# If they invoke anything in threading.py that calls current_thread(), they
# leave an entry in the _active dict forever after.
# Their purpose is to return *something* from current_thread().
# They are marked as daemon threads so we won't wait for them
# when we exit (conform previous semantics).

因此,总而言之,虚构元素提供了有关未被线程处理的线程的信息,这些元素不受 GC 的影响,因为它们存储在字典中,因此不是内存泄漏。