You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Python should support exporting thread names to the OS #59705
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2012-07-30.12:19:30.305>labels= ['type-feature', 'library', '3.10', '3.11']
title='Python should support exporting thread names to the OS'updated_at=<Date2022-01-18.21:32:45.293>user='https://bugs.python.org/bra'
#include<pthread.h>intpthread_setname_np(pthread_tthread, constchar*name);
// FreeBSD & OpenBSD: function name is slightly differentvoidpthread_set_name_np(pthread_ttid, constchar*name);
// Mac OS X: it only seems applicable to the current thread (no thread ID)intpthread_setname_np(constchar*);
It would be very useful if Python set the name parameter with the above pthread calls, so the user/developer could see which threads do what in ps or top.
I don't think this should be done by default as it will break people's expectations and, perhaps worse, compatibility.
bra commented on Aug 6, 2012
bramannequin
MannequinAuthor
More actions
"I don't think this should be done by default as it will break people's expectations and, perhaps worse, compatibility."
I won't mind another thread naming API, if somebody does this. :)
But personally I expected python to name my threads (and if the OS supports it, on that level), I was actually surprised to see that it doesn't, mostly because I remembered a patch for FreeBSD, which did this years ago, so I thought it has been merged into mainline since then.
It is indeed the compatibility that is the worse issue. The problem is what people have gotten used to and may have coded their applications to expect/deal with. I agree with you that most people would *not* find it surprising to see the name reflected in the OS, but I don't think the convenience of that is worth introducing a potential backward incompatibility.
On the other hand, I think this might be an appropriate place to use a global control, so that getting thread names out to the OS would require adding just a single line of code to any given application. I know of an application that does this. It chose to implement it as a global change, and that makes sense to me.
kovid commented on Nov 6, 2014
kovidmannequin
Mannequin
More actions
Just FYI, a pure python2 implementation that monkey patches Thread.start() to set the OS level thread name intelligently.
importctypes, ctypes.util, threadinglibpthread_path=ctypes.util.find_library("pthread")
iflibpthread_path:
libpthread=ctypes.CDLL(libpthread_path)
ifhasattr(libpthread, "pthread_setname_np"):
pthread_setname_np=libpthread.pthread_setname_nppthread_setname_np.argtypes= [ctypes.c_void_p, ctypes.c_char_p]
pthread_setname_np.restype=ctypes.c_intorig_start=threading.Thread.startdefnew_start(self):
orig_start(self)
try:
name=self.nameifnotnameorname.startswith('Thread-'):
name=self.__class__.__name__ifname=='Thread':
name=self.nameifname:
ifisinstance(name, unicode):
name=name.encode('ascii', 'replace')
ident=getattr(self, "ident", None)
ifidentisnotNone:
pthread_setname_np(ident, name[:15])
exceptException:
pass# Don't care about failure to set namethreading.Thread.start=new_start
Zackery, here's an initial draft implementation for Windows 10+ that's based on the interface you created in PR 14578. It calls WinAPI SetThreadDescription(), which sets the thread's name directly in the kernel thread object (i.e. ETHREAD.ThreadName). This function was added to the API in Windows 10 version 1607 (10.0.14393), so Windows 8.1 systems and older Windows 10 systems aren't supported. I'd rather not implement the exception-based approach for older Windows systems. It doesn't really name the thread and only works when a debugger is attached to the process.
This initial draft blindly assumes that the filesystem encoding is UTF-8. That's been the case in Windows since Python 3.6, unless configured to use the legacy ANSI encoding, e.g. by setting PYTHONLEGACYWINDOWSFSENCODING. PyThread_set_thread_name() could be changed to use CP_ACP instead of CP_UTF8 when legacy mode is enabled.
I set the truncation limit to 64 characters. It could be lowered to 15 to match what you implemented for POSIX, but I think that's too short. The limit could also effectively be removed by truncating to the system limit of 32766 characters. That would be simpler since the code wouldn't have to worry about surrogate pairs.
staticPyObject*_thread__set_thread_name_impl(PyObject*module, PyObject*name)
{
interror=PyThread_set_thread_name(PyBytes_AS_STRING(name));
#ifdefMS_WINDOWS// For Python code, ignore a not-implemented error, which means// it's a Windows 8.1 system or older Windows 10 system.if (error== (int)E_NOTIMPL) {
error=0;
}
#endifif (error) {
Py_DECREF(name);
PyErr_SetString(ThreadError, "setting the thread name failed");
returnNULL;
}
Py_DECREF(name);
Py_RETURN_NONE;
}
Activity
bra commentedon Jul 30, 2012
Python class Thread has a "name" argument, which sets the name of the given thread. This name is used only internally, while there is a possibility to set this on an OS-level.
Related discussion:
http://stackoverflow.com/questions/2369738/can-i-set-the-name-of-a-thread-in-pthreads-linux
It would be very useful if Python set the name parameter with the above pthread calls, so the user/developer could see which threads do what in ps or top.
tiran commentedon Aug 1, 2012
+1
win32 supports thread names, too. http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
pitrou commentedon Aug 4, 2012
I don't think this should be done by default as it will break people's expectations and, perhaps worse, compatibility.
bra commentedon Aug 6, 2012
"I don't think this should be done by default as it will break people's expectations and, perhaps worse, compatibility."
I won't mind another thread naming API, if somebody does this. :)
But personally I expected python to name my threads (and if the OS supports it, on that level), I was actually surprised to see that it doesn't, mostly because I remembered a patch for FreeBSD, which did this years ago, so I thought it has been merged into mainline since then.
bitdancer commentedon Aug 6, 2012
It is indeed the compatibility that is the worse issue. The problem is what people have gotten used to and may have coded their applications to expect/deal with. I agree with you that most people would *not* find it surprising to see the name reflected in the OS, but I don't think the convenience of that is worth introducing a potential backward incompatibility.
On the other hand, I think this might be an appropriate place to use a global control, so that getting thread names out to the OS would require adding just a single line of code to any given application. I know of an application that does this. It chose to implement it as a global change, and that makes sense to me.
kovid commentedon Nov 6, 2014
Just FYI, a pure python2 implementation that monkey patches Thread.start() to set the OS level thread name intelligently.
[-]Python should support naming threads[/-][+]Python should support exporting thread names to the OS[/+]vstinner commentedon Sep 22, 2020
bpo-18006 "Set thread name in linux kernel" was marked as a duplicate of this issue:
"""
In linux (Since 2.6.9) we can use syscall
to set thread name to the kernel.
(...)
"""
eryksun commentedon Nov 15, 2021
Zackery, here's an initial draft implementation for Windows 10+ that's based on the interface you created in PR 14578. It calls WinAPI SetThreadDescription(), which sets the thread's name directly in the kernel thread object (i.e. ETHREAD.ThreadName). This function was added to the API in Windows 10 version 1607 (10.0.14393), so Windows 8.1 systems and older Windows 10 systems aren't supported. I'd rather not implement the exception-based approach for older Windows systems. It doesn't really name the thread and only works when a debugger is attached to the process.
This initial draft blindly assumes that the filesystem encoding is UTF-8. That's been the case in Windows since Python 3.6, unless configured to use the legacy ANSI encoding, e.g. by setting PYTHONLEGACYWINDOWSFSENCODING. PyThread_set_thread_name() could be changed to use CP_ACP instead of CP_UTF8 when legacy mode is enabled.
I set the truncation limit to 64 characters. It could be lowered to 15 to match what you implemented for POSIX, but I think that's too short. The limit could also effectively be removed by truncating to the system limit of 32766 characters. That would be simpler since the code wouldn't have to worry about surrogate pairs.
Python/thread_nt.h:
Modules/_threadmodule.c:
21 remaining items
pythongh-59705: Implement _thread.set_name() on Windows
pythongh-59705: Implement _thread.set_name() on Windows
vstinner commentedon Jan 9, 2025
I wrote PR gh-128675 to implement
set_name()and_get_name()on Windows withSetThreadDescription()andGetThreadDescription().pythongh-59705: Document OS thread name change
gh-59705: Document OS thread name change (#128800)
gh-59705: Implement _thread.set_name() on Windows (#128675)
pythongh-59705: Make PYTHREAD_NAME_MAXLEN macro private
gh-59705: Make PYTHREAD_NAME_MAXLEN macro private (#128945)
pythongh-59705: Implement _thread.set_name() on Windows (python#128675)
pythongh-59705: Make PYTHREAD_NAME_MAXLEN macro private (python#128945)
vstinner commentedon Jan 24, 2025
The implementation is now complete, I close the issue. Thanks for helping me to implement this feature!
pythongh-59705: Fix solaris detection in test_set_name
gh-59705: Fix solaris detection in test_threading.test_set_name() (#1…