有没有办法在 Python 中设置线程的 title/name?

Is there a way to set title/name of a thread in Python?

我想在 Python 中设置线程的标题(在 pstop 中看到的标题),以使其对进程跟踪程序可见。当使用 /usr/bin/python 并通过 ./script.

调用脚本时,进程的所有线程总是称为 python 或称为文件名

现在,我想更改每个线程的名称。我有一个带有 4 个线程(包括主线程)的简单脚本。我使用 threading 来启动线程。

有没有一种方法可以在不安装 third-party 东西的情况下实现这一点?任何指导表示赞赏。

只需执行以下操作: t = threading.Thread(name='my_thread')

试试这个:

def your_function(arg1, arg2, argn):
    * do stuff *


new_thread = threading.Thread(target=your_function, args=(arg1, arg2, argn))
new_thread.name = 'your name'
new.thread.start()

其中 new_thread.name 是您的答案。