Python线程名不会显示在ps或htop上

2024-04-26 17:25:46 发布

您现在位置:Python中文网/ 问答频道 /正文

当我为Python线程设置名称时,它不会显示在htop或ps上。ps输出只显示python作为线程名称。有没有什么方法可以设置线程名,以便在类似的系统报告中显示它?

from threading import Thread
import time


def sleeper():
    while True:
        time.sleep(10)
        print "sleeping"

t = Thread(target=sleeper, name="Sleeper01")
t.start()
t.join()

ps-T-p{PID}输出

  PID  SPID TTY          TIME CMD
31420 31420 pts/30   00:00:00 python
31420 31421 pts/30   00:00:00 python

Tags: 方法fromimport名称time系统报告线程
3条回答

首先安装prctl module。(在debian/ubuntu上只需键入sudo apt-get install python-prctl

from threading import Thread
import time
import prctl

def sleeper():
    prctl.set_name("sleeping tiger")
    while True:
        time.sleep(10)
        print "sleeping"

t = Thread(target=sleeper, name="Sleeper01")
t.start()
t.join()

这个指纹

$ ps -T
  PID  SPID TTY          TIME CMD
22684 22684 pts/29   00:00:00 bash
23302 23302 pts/29   00:00:00 python
23302 23303 pts/29   00:00:00 sleeping tiger
23304 23304 pts/29   00:00:00 ps

如果系统中安装了prctl,我使用以下monkey补丁将python线程的名称传播到系统:

try:
    import prctl
    def set_thread_name(name): prctl.set_name(name)

    def _thread_name_hack(self):
        set_thread_name(self.name)
        threading.Thread.__bootstrap_original__(self)

    threading.Thread.__bootstrap_original__ = threading.Thread._Thread__bootstrap
    threading.Thread._Thread__bootstrap = _thread_name_hack
except ImportError:
    log('WARN: prctl module is not installed. You will not be able to see thread names')
    def set_thread_name(name): pass

执行此代码后,可以像往常一样设置线程的名称:

threading.Thread(target=some_target, name='Change monitor', ...)

这意味着,如果已经为线程设置了名称,就不需要更改任何内容。我不能保证,这是100%安全的,但它对我有效。

Prctl模块很好,提供了许多特性,但依赖于libcap dev包。最有可能安装Libcap2,因为它是许多包(例如systemd)的依赖项。因此,如果只需要设置线程名,那么在ctypes上使用libcap2。

请参阅下面改进的悲伤答案。

LIB = 'libcap.so.2'
try:
    libcap = ctypes.CDLL(LIB)
except OSError:
    print(
        'Library {} not found. Unable to set thread name.'.format(LIB)
    )
else:
    def _name_hack(self):
        # PR_SET_NAME = 15
        libcap.prctl(15, self.name.encode())
        threading.Thread._bootstrap_original(self)

    threading.Thread._bootstrap_original = threading.Thread._bootstrap
    threading.Thread._bootstrap = _name_hack

相关问题 更多 >