Code: Select all
thread = threading.Thread(target=lambda: time.sleep(10), name="test-thread")
thread.start()
< /code>
, aber ihre Namen würden nicht von OS angezeigt (beachten Sie die zweite Zeile, die dem Thread entspricht, ist leer): < /p>
$ ps -M 87266
USER PID TT %CPU STAT PRI STIME UTIME COMMAND
burkov 87266 s032 0.0 S 31T 0:00.24 0:00.87 /Library/Frameworks/PythonT.framework/Versions/3.
87266 0.0 S 31T 0:00.00 0:00.00
def set_thread_name(thread: threading.Thread, name: str):
"""Set the OS thread name using pthread_setname_np
Should be called after thread.start()!
"""
try:
libpthread_path = ctypes.util.find_library("pthread")
if libpthread_path:
libpthread = ctypes.CDLL(libpthread_path)
if hasattr(libpthread, "pthread_setname_np"):
pthread_setname_np = libpthread.pthread_setname_np
pthread_setname_np.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
pthread_setname_np.restype = ctypes.c_int
# Get current thread ID
thread_id = thread.ident
pthread_setname_np(thread_id, name.encode('ascii', 'replace')[:15])
except Exception:
pass # Ignore failures
thread = threading.Thread(target=lambda: time.sleep(10), name="test-thread")
thread.start()
set_thread_name(thread, "test-thread")
< /code>
Wieder ohne Erfolg. Können Sie vorschlagen, wie ich das beheben kann?