当我为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
Nick Craig-W.. 19
首先安装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
关于这个问题已经存在一个问题:https://bugs.python.org/issue15500 (4认同)
@BurnsBA,您需要`pip install python-prctl`才能获得此功能。名为`prctl`的pip模块不具有`set_name`函数。 (2认同)
Grief.. 7
如果prctl
安装在系统中,我使用以下猴子补丁将python Thread的名称传播到系统:
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模块.(在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
安装在系统中,我使用以下猴子补丁将python Thread的名称传播到系统:
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