question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tracking Child Processes (tree of child processes)

See original GitHub issue

I’ve noticed that when using subprocess.run on my dask tasks, the workers still report low CPU usage, when the child process is in fact maxing out my CPU usage. Does Dask track the forked/child process CPU usage under the worker CPU usage? If not, I think it should since child processes can occur when interfacing dask workers against programs written in non-python.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
CMCDragonkaicommented, Mar 19, 2019

This is natively supported in the psutil package: https://unix.stackexchange.com/a/339071/56970

pid=2235; python3 -c "import psutil
for c in psutil.Process($pid).children(True):
  print(c.pid)"

The True ensures that it gets it recursively.

https://psutil.readthedocs.io/en/latest/#psutil.Process.children

Another way is to realise that process group on Linux will always encapsulate the entire process tree when launched by a shell. See: https://en.wikipedia.org/wiki/Process_group This means that on Linux, if you start each worker on its own process group. Then you can just query the PGID and get all processes part of that group. This is also useful for distribution of signals. However this may not be portable to Windows or other operating systems, whereas process tree via direct descendants may be more standard among all OS.

I wrote a gist about this process groups: https://gist.github.com/CMCDragonkai/f58afb7e39fcc422097849b853caa140

0reactions
fbriolcommented, Nov 19, 2020

I tried to find a solution to follow the workers’ child processes.

To do this, the class handling the system monitoring must track the children; otherwise, the CPU usage is always 0 for all children (no previous time exists to calculate the usage). Initially, the children’s set is empty. Then we have to add new children and remove the old ones. In short, it adds a little bit of calculation. In my test case, I didn’t see any problem with the monitoring performance.

--- 8,14 ----
  class SystemMonitor:
      def __init__(self, n=10000):
          self.proc = psutil.Process()
+         self.children = set()
  
          self.time = deque(maxlen=n)
          self.cpu = deque(maxlen=n)
*************** class SystemMonitor:
*** 45,50 ****
--- 46,60 ----
          with self.proc.oneshot():
              cpu = self.proc.cpu_percent()
              memory = self.proc.memory_info().rss
+             children = set(self.proc.children(True)) - self.children
+             if children:
+                 self.children.update(children)
+             self.children = set(item for item in self.children
+                                 if item.is_running())
+             for item in self.children:
+                 with item.oneshot():
+                     cpu += item.cpu_percent()
+                     memory += item.memory_info().rss
          now = time()
  
          self.cpu.append(cpu)

I can open a PR to insert this change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Track Child Processes Using strace | Baeldung on Linux
A quick and practical guide to tracking child processes with strace.
Read more >
unix - I need to trace all child processes created by given ...
Use exec strace -s 9999 -f -e trace=execve -p [pid of process] >& strace.log . Then parse it with some simple perl script...
Read more >
linux - Find child processes created by a Command?
Is there a way in Linux to check the child processes created by a command? I am running a command and it completes...
Read more >
Cb Response: How many child processes are shown in...
The process tree can only display up to 15 child processes; either 15 unsuppressed, 15 suppressed, or 15 of both types. For processes...
Read more >
ProcExp: Process Explorer: Viewing Parent and Child Processes
The unique capabilities of Process Explorer make it useful for tracking down DLL-version problems or ... Notice the Parent / Child Process Tree...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found