Get thread pid

A

Alejandro

Hi:

I have Python program running under Linux, that create several
threads, and I want to now the corresponding PID of the threads.

In each of the threads I have

def run(self):
pid = os.getpid()
logger.critical('process ID: %s', pid)

However, the reported PID is the father number, not the PID of the new
thread. Is there a way to get the PID of the thread?

To illustrate this further, this is the output of pstree when the PID
of the main Python thread is 9197:

$pstree -p 9197
python(9197)€ˆ€{python}(9555)
†€{python}(9556)
†€{python}(9557)
†€{python}(9558)
†€{python}(9559)
†€{python}(9560)
†€{python}(9561)
†€{python}(9562)
†€{python}(9563)
„€{python}(9564

I want each thread to report its PID number, in this case 9555, 9556,
etc., but with os.getpid() is get 9197.

Regards,
Alejandro.
 
G

Gabriel Genellina

En Thu, 29 Jan 2009 14:04:49 -0200, Alejandro
I have Python program running under Linux, that create several
threads, and I want to now the corresponding PID of the threads.

In each of the threads I have

def run(self):
pid = os.getpid()
logger.critical('process ID: %s', pid)

However, the reported PID is the father number, not the PID of the new
thread. Is there a way to get the PID of the thread?

Using "pid" for a thread identifier is confusing; I'd call it tid instead.
(getpid() used to return a thread id in old Linux kernels, but that was a
mess).

Try using Thread.ident (requires Python 2.6). I'd expect it to return
gettid() but I've not checked it; from the docs, it might be a synthesized
number as well.
<http://docs.python.org/library/threading.html#threading.Thread.ident>

In case it doesn't work, you can use ctypes to perform a gettid syscall.
 
O

Ove Svensson

Alejandro said:
Hi:

I have Python program running under Linux, that create several
threads, and I want to now the corresponding PID of the threads.

In each of the threads I have

def run(self):
pid = os.getpid()
logger.critical('process ID: %s', pid)

However, the reported PID is the father number, not the PID of the new
thread. Is there a way to get the PID of the thread?

Pid is a process identifier. Threads are not processes. All your threads
execute within the context if a single process, hence they should have
the same pid. Threads may have a thread id but it is not the same as the
pid.
 
A

Alejandro

Pidis a process identifier. Threads are not processes. All your threads
execute within the context if a single process, hence they should have
the samepid. Threads may have athreadid but it is not the same as thepid.

According to this document (http://heather.cs.ucdavis.edu/~matloff/
Python/PyThreads.pdf), at least in Linux, threads are process:

"Here each thread really is a process, and for example will show up on
Unix systems when one runs the appropriate ps process-list command,
say ps axH. The threads manager is then the OS."

If you look at my original post, pstree does show different PIDs for
the threads.

Regards,
Alejandro.
 
A

Alejandro

May I ask why you want to get the TID?

htop shows the TID of each thread. Knowing the TID allows me to know
which thread is hogging the CPU. If there is a better way to do this,
or there is something fundamentally wrong with this approach, please
let me know.

Alejandro.
 
O

Ove Svensson

Alejandro said:
htop shows the TID of each thread. Knowing the TID allows me to know
which thread is hogging the CPU. If there is a better way to do this,
or there is something fundamentally wrong with this approach, please
let me know.

Alejandro.

Assuming we are talking about a posix (pthread) architecture,
you can't really say anything about the thread id (TID). Pthreads
identifies each thread with the anonymous type pthread_t. If you
want to write a portable application, you must not assume anything
regarding the real type. It could be an integer, pointer, struct
or something else. Just because some operating systems use an
integer for the pthread_t doesn't mean that others do the same.

It is true that the operating system must have some way to identify
each thread (within each process), but it is not visible or accessible
to the application.

If you have a threading *framework* on top of pthreads, which uses
some TID, that is then either purely an artificial id or a non-portable
architecture specific access to the operating system id. For example,
python thread.get_ident() returns the `thread identifier' of the
current thread. That TID can be *anything*. You must not assume that
id has any meaning (other than being unique per thread). The python
class threading.Thread has methods setName() and getName() for
setting and fetching the *name* of a thread. That is a pure application
name and has nothing to do with the id used by the operating system.

The reason that python modules thread and threading doesn't return
any real TID is simply that there isn't any TID that it can return.
At least not in any portable way.

If you find a way to get to the real TID, that will be specific to
your architecture.

If htop (or any other application) returns a TID, that is either
artificial or architecture specific.
 
A

Alejandro

If you find a way to get to the real TID, that will be specific to
your architecture.

If htop (or any other application) returns a TID, that is either
artificial or architecture specific.

Right know I only need the TID for debugging under Linux. I will keep
this information in mind though.

Alejandro.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top