34

当我为 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
4

5 回答 5

28

首先安装prctl 模块。(在 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

注意:python3 用户可能希望使用pyprctl

于 2015-12-18T20:15:40.273 回答
10

Prctl 模块很好并且提供了很多特性,但是依赖于 libcap-dev 包。Libcap2 很可能已安装,因为它是许多软件包的依赖项(例如 systemd)。因此,如果您只需要设置线程名称,请使用 libcap2 而不是 ctypes。

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

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
于 2018-08-03T08:32:21.357 回答
8

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

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% 安全的,但它对我有用。

于 2016-02-08T22:24:30.830 回答
0

另一种解决方案(实际上是一个肮脏的解决方案,因为它设置了进程名称,而不是线程名称)是使用setproctitlepypi 中的模块。

您可以按如下方式安装pip install setproctitle和使用它:

import setproctitle
import threading
import time

def a_loop():
    setproctitle.setproctitle(threading.currentThread().name)
    # you can otherwise explicitly declare the name:
    # setproctitle.setproctitle("A loop")
    while True:
        print("Looping")
        time.sleep(99)

t = threading.Thread(target=a_loop, name="ExampleLoopThread")
t.start()
于 2018-09-05T14:07:19.943 回答
0

在我找到一个工具——py -spy来在运行时显示 python 线程后,我感到很困惑。

安装: pip3 install -i https://pypi.doubanio.com/simple/py-spy

用法:py-spy dump --pid 进程号

例如,py-spy dump --pid 1234 可以显示python进程1234的所有线程堆栈,名称,id

于 2021-12-31T03:54:00.093 回答