Seite 1 von 1

Thread Dump of a python process

Verfasst: Donnerstag 9. Februar 2006, 12:10
von Joan
Hi,
Excuse me but I don't undestand deutsch language... and not very well english too :?

I would to know if there're some way to have a dump of all the threads started by a python process. I want to see the TID corresponding of each thread because I need them to get the CPU time of each thread by the command top.

Thanks

Verfasst: Donnerstag 9. Februar 2006, 15:51
von N317V
I haven`t dealt with threads myself, yet. Maybe this will help You:
http://docs.python.org/lib/node442.html

Possibly You can find some more help here:
http://www.python.org/doc/NonEnglish.html#spanish

Verfasst: Freitag 10. Februar 2006, 12:49
von Joan
Thank you for your info. Now I have read that a simple call os.getpid() returned the linux identifier of the thread in latest python versions, but I'm using Python 2.3 and 2.4 and this call returns always the same id :( :(
I only want to take the pid of the thread but isn't as easier as it seems.

Verfasst: Samstag 11. Februar 2006, 00:07
von BlackJack
`os.getpid()` returns the *process id* of the current program in any Python version. Would be very strange if it's *not* the same id within one process. ;-)

A thread is something different than a process. And it's not guaranteed that it will show up in `top` as a separate "process". This depends on the thread library and if the kernel lists threads as processes. I don't know if there is a portable and reliable way to see the times of specific threads.

Verfasst: Samstag 11. Februar 2006, 01:44
von modelnine
`os.getpid()` returns the *process id* of the current program in any Python version. Would be very strange if it's *not* the same id within one process.
What os.getpid() returns also depends on the thread library you are using. LinuxThreads returns a separate process ID for each thread, NPTL (which is standard in pretty much any linux-distribution as of today) doesn't represent threads as separate processes anymore, so you'll not see them as such.

What you might do is parse the output of ps auxm, which prints separate lines for each running thread (and also outputs the time spent in the corresponding thread), but I wouldn't know of any way to map this to actual Python threads...

Verfasst: Montag 13. Februar 2006, 16:27
von Joan
Thank you for your answers.
What I wanna do it could be easy if I could know which pid represents each thread.
When I run my python process it has a PID, for example 11341. Now I can go to the /proc/11341 folder and I can show all the threads of this process in the task folder, for example I can show:
dr-xr-xr-x 3 root root 0 feb 13 16:20 899
dr-xr-xr-x 3 root root 0 feb 13 16:20 900
dr-xr-xr-x 3 root root 0 feb 13 16:20 901
dr-xr-xr-x 3 root root 0 feb 13 16:20 902
dr-xr-xr-x 3 root root 0 feb 13 16:20 903
dr-xr-xr-x 3 root root 0 feb 13 16:20 904
dr-xr-xr-x 3 root root 0 feb 13 16:20 905
dr-xr-xr-x 3 root root 0 feb 13 16:20 906
dr-xr-xr-x 3 root root 0 feb 13 16:20 907
dr-xr-xr-x 3 root root 0 feb 13 16:20 908
dr-xr-xr-x 3 root root 0 feb 13 16:20 909

and I could know what CPU time it's consumed by each thread by the command top:
top -b -n 1 -p $list

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
899 root 16 0 149m 12m 8232 S 0.0 1.3 0:00.44 run.py
900 root 16 0 149m 12m 8232 S 0.0 1.3 0:00.02 run.py
901 root 15 0 149m 12m 8232 S 0.0 1.3 0:00.00 run.py
902 root 19 0 149m 12m 8232 S 0.0 1.3 0:00.00 run.py
903 root 19 0 149m 12m 8232 S 0.0 1.3 0:00.00 run.py
904 root 15 0 149m 12m 8232 S 0.0 1.3 0:00.00 run.py
905 root 21 0 149m 12m 8232 S 0.0 1.3 0:00.01 run.py
906 root 23 0 149m 12m 8232 S 0.0 1.3 0:00.00 run.py
907 root 15 0 149m 12m 8232 S 0.0 1.3 0:00.00 run.py
908 root 21 0 149m 12m 8232 S 0.0 1.3 0:00.00 run.py
909 root 22 0 149m 12m 8232 S 0.0 1.3 0:00.00 run.py

and I show all the threads of my process and their CPU's time... But my problem is that I can't know wich thread is corresponding with each PID. In this case I know that the thread 899 of my process consumes 440 ms. but I don't know wich is this thred. :?