有没有一种方法可以在Python中设置线程的标题/名称?

moz*_*ors 5 python multithreading linux-kernel python-multithreading

我想在Python中设置线程的标题(在pstop中看到的标题),以使其在处理跟踪程序中可见。使用进程时,进程的所有线程总是被调用python或称为文件名/usr/bin/python,而脚本是通过./script调用的。

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

有没有一种方法可以实现此目标而无需安装第三方的东西?任何指导表示赞赏。

Nic*_*erg 11

尝试这个:

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()
Run Code Online (Sandbox Code Playgroud)

new_thread.name你的答案在哪里。