Get thread pid

Alejandro alejandro.weinstein at gmail.com
Thu Jan 29 11:04:49 EST 2009


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.



More information about the Python-list mailing list