There are two reasons:
* PyThread_get_thread_native_id() was only added recently (Python 3.8)
* PyThread_get_thread_native_id() is not portable and not available on
all platforms: the function is only declared if the
PY_HAVE_THREAD_NATIVE_ID macro is defined.
Maybe PyThreadState.thread_id could use
PyThread_get_thread_native_id() if avaialble, or
PyThread_get_thread_ident() otherwise. But that sounds like an
Another approach would be to add a *new* structure member, like
thread_native_id. It would not exist if the PY_HAVE_THREAD_NATIVE_ID
macro is not defined.
#if defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__)
|| defined(__OpenBSD__) || defined(__NetBSD__) || defined(_WIN32) ||
PyAPI_FUNC(unsigned long) PyThread_get_thread_native_id(void);
(void) pthread_threadid_np(NULL, &native_id);
native_id = syscall(SYS_gettid);
native_id = pthread_getthreadid_np();
native_id = getthrid();
native_id = thread_self();
native_id = _lwp_self();
return (unsigned long) native_id;
Night gathers, and now my watch begins. It shall not end until my death.
On Fri, Apr 16, 2021 at 10:47 PM Gabriele <firstname.lastname@example.org> wrote:
> Hi all.
> My apologies if this is a topic that's been discussed already, but I
> wasn't able to locate anything in the archive on the subject. I was
> wondering if there's a fundamental reason for using
> PyThread_get_thread_ident instead of PyThread_get_thread_native_id for
> the value of the thread_id field of the PyThreadState object.
> The reason why I'm asking is that I would like to have easy access to
> the native TID on Linux to identify the /proc/stat file associated
> with the thread from an external process. At the moment I have to
> resort to calling process_vm_readv to copy the struct pthread over to
> local VM and then guess where the tid field might be. So, if there's
> no fundamental reason for thread_id to be PyThread_get_thread_ident, I
> would like to propose to change it to PyThread_get_thread_native_id
> "Egli è scritto in lingua matematica, e i caratteri son triangoli,
> cerchi, ed altre figure
> geometriche, senza i quali mezzi è impossibile a intenderne umanamente parola;
> senza questi è un aggirarsi vanamente per un oscuro laberinto."
> -- G. Galilei, Il saggiatore.
> Python-Dev mailing list -- email@example.com
> To unsubscribe send an email to firstname.lastname@example.org
> Message archived at https://email@example.com/message/3DOB6VUTAIJKK4SUGBYWL4QPWI2E5Q2T/
> Code of Conduct: http://python.org/psf/codeofconduct/