Python 线程名称是否反映了打开线程的数量?
Do Python thread names reflect the number of open threads?
如果我看到如下堆栈跟踪:
Exception in thread Thread-101:
Traceback (most recent call last):
(...)
"Thread-101"是否一定意味着有 101 个活动/打开/性能下降的线程?或者这些名称是否像数据库中的 ID 一样工作,其中数字总是上升,即使旧记录(线程)被删除(关闭)?
来自文档:https://docs.python.org/3/library/threading.html#threading.Thread.name
A string used for identification purposes only. It has no semantics.
Multiple threads may be given the same name. The initial name is set by the constructor.
另外,为了调侃自己,你可以试试这个
>>> from threading import Thread
>>> t1 = Thread()
>>> t1
<Thread(Thread-1, initial)>
>>> t2 = Thread()
>>> t2
<Thread(Thread-2, initial)>
>>> t2.setName('Thread-1')
>>> t2
<Thread(Thread-1, initial)>
>>> t1
<Thread(Thread-1, initial)>
>>> t1.name
'Thread-1'
>>> t2.name
'Thread-1'
您可以看到我已将两个线程命名为相同的名称,因此线程名称作为任何类型的索引都会出现 window。
如果我看到如下堆栈跟踪:
Exception in thread Thread-101:
Traceback (most recent call last):
(...)
"Thread-101"是否一定意味着有 101 个活动/打开/性能下降的线程?或者这些名称是否像数据库中的 ID 一样工作,其中数字总是上升,即使旧记录(线程)被删除(关闭)?
来自文档:https://docs.python.org/3/library/threading.html#threading.Thread.name
A string used for identification purposes only. It has no semantics.
Multiple threads may be given the same name. The initial name is set by the constructor.
另外,为了调侃自己,你可以试试这个
>>> from threading import Thread
>>> t1 = Thread()
>>> t1
<Thread(Thread-1, initial)>
>>> t2 = Thread()
>>> t2
<Thread(Thread-2, initial)>
>>> t2.setName('Thread-1')
>>> t2
<Thread(Thread-1, initial)>
>>> t1
<Thread(Thread-1, initial)>
>>> t1.name
'Thread-1'
>>> t2.name
'Thread-1'
您可以看到我已将两个线程命名为相同的名称,因此线程名称作为任何类型的索引都会出现 window。