Python thread id above

In my Python script, I start a bunch of different threads. I want to track memory and processor usage for each of these threads. For this, I use top and ps -eLf .

But it turns out that the identifier returned by thread.start_new_thread() is different from the identifier of the thread displayed by top and other similar programs. Is there a way to get this PID using a Python script? That way, I can determine which PID belongs to which thread.

+8
python multithreading linux ctypes
source share
3 answers

Thanks to this post , I got Python threads to report their respective thread IDs. First do grep -r 'SYS_gettid' /usr/include/' . I got the line: #define SYS_gettid __NR_gettid With further grepping on grep -r '__NR_gettid' /usr/include/ I got a bunch of matching lines:

 /usr/include/x86_64-linux-gnu/asm/unistd_32.h:#define __NR_gettid 224 /usr/include/x86_64-linux-gnu/asm/unistd_64.h:#define __NR_gettid 186 /usr/include/asm-generic/unistd.h:#define __NR_gettid 178 

Now choose the one that fits your architecture. Mine was 186. Now include this code in all your Python stream scripts to get the stream ID, as the OS shows:

 import ctypes tid = ctypes.CDLL('libc.so.6').syscall(186) 
+12
source share

Here's a patch to replace the python stream id with a TID , as shown in htop :

 def patch_thread_identifier(): """Replace python thread identifier by TID.""" # Imports import threading, ctypes # Define get tid function def gettid(): """Get TID as displayed by htop.""" libc = 'libc.so.6' for cmd in (186, 224, 178): tid = ctypes.CDLL(libc).syscall(cmd) if tid != -1: return tid # Get current thread current = threading.current_thread() # Patch _get_ident threading._get_ident = gettid # Update active dictionary threading._active[gettid()] = threading._active.pop(current.ident) # Set new identifier for the current thread current._set_ident() # Done print 'threading._get_ident patched!' 
+4
source share

What operating system are you using?

With the exception of very old versions of Linux, each thread must have the same PID (process identifier). The thread.start_new_thread () identifier is internal to python and is used to identify a specific thread of execution.

For more information on Linux streaming, see the pthreads manual page on Linux.

0
source share

All Articles