classification
Title: Python should support exporting thread names to the OS
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Arfrever, ZackerySpytz, asvetlov, bra, christian.heimes, flox, jcea, kovid, pitrou, r.david.murray, socketpair
Priority: normal Keywords: patch

Created on 2012-07-30 12:19 by bra, last changed 2019-07-03 23:43 by ZackerySpytz.

Pull Requests
URL Status Linked Edit
PR 14578 open ZackerySpytz, 2019-07-03 23:36
Messages (6)
msg166890 - (view) Author: Attila Nagy (bra) Date: 2012-07-30 12:19
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

#include <pthread.h>
int pthread_setname_np(pthread_t thread, const char *name);

// FreeBSD & OpenBSD: function name is slightly different
void pthread_set_name_np(pthread_t tid, const char *name);

// Mac OS X: it only seems applicable to the current thread (no thread ID)
int pthread_setname_np(const char*);

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.
msg167166 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2012-08-01 20:44
+1

win32 supports thread names, too. http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
msg167459 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-08-04 23:25
I don't think this should be done by default as it will break people's expectations and, perhaps worse, compatibility.
msg167552 - (view) Author: Attila Nagy (bra) Date: 2012-08-06 10:22
"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.
msg167560 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-08-06 13:17
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.
msg230736 - (view) Author: Kovid Goyal (kovid) Date: 2014-11-06 11:58
Just FYI, a pure python2 implementation that monkey patches Thread.start() to set the OS level thread name intelligently.

        import ctypes, ctypes.util, threading
        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
                orig_start = threading.Thread.start
                def new_start(self):
                    orig_start(self)
                    try:
                        name = self.name
                        if not name or name.startswith('Thread-'):
                            name = self.__class__.__name__
                            if name == 'Thread':
                                name = self.name
                        if name:
                            if isinstance(name, unicode):
                                name = name.encode('ascii', 'replace')
                            ident = getattr(self, "ident", None)
                            if ident is not None:
                                pthread_setname_np(ident, name[:15])
                    except Exception:
                        pass  # Don't care about failure to set name
                threading.Thread.start = new_start
History
Date User Action Args
2019-07-03 23:43:24ZackerySpytzsetnosy: + ZackerySpytz

versions: + Python 3.9, - Python 3.6
2019-07-03 23:36:12ZackerySpytzsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request14397
2015-11-30 14:03:13r.david.murraysettitle: Python should support naming threads -> Python should support exporting thread names to the OS
stage: needs patch
versions: + Python 3.6, - Python 3.4
2015-11-29 21:17:54socketpairsetnosy: + socketpair
2014-11-06 11:58:01kovidsetnosy: + kovid
messages: + msg230736
2013-05-19 08:01:04neologixlinkissue18006 superseder
2012-08-07 14:16:32asvetlovsetnosy: + asvetlov
2012-08-06 13:17:16r.david.murraysetmessages: + msg167560
2012-08-06 13:16:34r.david.murraysetmessages: - msg167559
2012-08-06 13:15:59r.david.murraysetmessages: + msg167559
2012-08-06 10:22:18brasetmessages: + msg167552
2012-08-04 23:25:28pitrousetnosy: + pitrou
messages: + msg167459
2012-08-04 20:32:21floxsetnosy: + flox
2012-08-04 20:13:04r.david.murraysetnosy: + r.david.murray
2012-08-01 20:44:22christian.heimessetnosy: + christian.heimes
messages: + msg167166
2012-08-01 00:40:34jceasetnosy: + jcea
2012-07-30 18:30:20Arfreversetnosy: + Arfrever
2012-07-30 18:28:05pitrousetversions: + Python 3.4, - Python 2.7
2012-07-30 12:19:30bracreate