Figuring out the threads for processes ran by python
Posted by jpluimers on 2023/08/17
A while ago I wrote about Figuring out the open network connections for processes ran by python, which explained the TL;DR:
pidof python | tr " " "\n" | xargs -r -n 1 lsof -i -a -e /run/user/1001/gvfs -p
Now I needed thread information as well, so below two examples using ps and pstree. I won’t explain the pidof and xargs stuff here as that was already covered in the above blog-post and I found out that ps already has a built-in way to filter on process name.
The ps solution uses the H, -L or -T argument to show the threads:
ps -F H -L -C python
or:
ps -F H -T -C python
Hshows theSTATcolumn with process state codes-LshowsLWPandNLWPcolumns with Light Weight Process numbers and count (NLWP) of those within the process-TshowsSPIDwith thread IDs-Cfilters on thepythonprocess name- note
-Land-Tare mutually exclusive, but I am not sure why that is
The pstree solution (where I also added parent information) uses the --show-pids argument which you can abbreviate to -p:
pidof python | tr " " "\n" | xargs -r -n 1 pstree --arguments --show-pgids --long --show-parents --thread-names --show-pids
I love that pstree has way more descriptive parameters than ps. Though you can abbreviate them:
-afor--argumentsshows process arguments-gfor--show-pgidsshows process group IDs-lfor--longkeeps long lines instead of defaulting to truncating long lines-sfor--show-parentsshows all parent processes too-tfor--thread-namesshows full thread names (usually process names)-pfor--show-pids
All the above based on:
- [Wayback/Archive] linux view threads per process – Google Search
- Three different answers:
- [Wayback/Archive] command line – Is there a way to see details of all the threads that a process has in Linux? – Unix & Linux Stack Exchange answer with
topandpsby [Wayback/Archive] Daniel Hill - [Wayback/Archive] command line – Is there a way to see details of all the threads that a process has in Linux? – Unix & Linux Stack Exchange answer with
pstreeby [Wayback/Archive] user2496 - [Wayback/Archive] command line – Is there a way to see details of all the threads that a process has in Linux? – Unix & Linux Stack Exchange answer with
topandpsby [Wayback/Archive] Franklin Piat
- [Wayback/Archive] command line – Is there a way to see details of all the threads that a process has in Linux? – Unix & Linux Stack Exchange answer with
- Man pages:
–jeroen






Leave a comment