The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

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
  • H shows the STAT column with process state codes
  • -L shows LWP and NLWP columns with Light Weight Process numbers and count (NLWP) of those within the process
  • -T shows SPID with thread IDs
  • -C filters on the python process name
  • note -L and -T are 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:

  • -a for --arguments shows process arguments
  • -g for --show-pgids shows process group IDs
  • -l for --long keeps long lines instead of defaulting to truncating long lines
  • -s for --show-parents shows all parent processes too
  • -t for --thread-names shows full thread names (usually process names)
  • -p for --show-pids

All the above based on:

–jeroen

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.