class_docstr: Background process for tracking resource usage
class_docstr_tok: ["Background","process","for","tracking","resource","usage"]
code: def run(self):
      psutil = import_required(
          "psutil",
          "Tracking resource usage requires `psutil` to be installed",
      )
      self.parent = psutil.Process(self.parent_pid)
  
      pid = current_process()
      data = []
      while True:
          try:
              msg = self.child_conn.recv()
          except KeyboardInterrupt:
              continue
          if msg == "shutdown":
              break
          elif msg == "collect":
              ps = self._update_pids(pid)
              while not data or not self.child_conn.poll():
                  tic = default_timer()
                  mem = cpu = 0
                  for p in ps:
                      try:
                          mem2 = p.memory_info().rss
                          cpu2 = p.cpu_percent()
                      except (
                          Exception
                      ):  # could be a few different exceptions
                          pass
                      else:
                          # Only increment if both were successful
                          mem += mem2
                          cpu += cpu2
                  data.append((tic, mem / 1e6, cpu))
                  sleep(self.dt)
          elif msg == "send_data":
              self.child_conn.send(data)
              data = []
      self.child_conn.close()
  
imports: from __future__ import annotations
  from collections import namedtuple
  from itertools import starmap
  from multiprocessing import Pipe, Process, current_process
  from time import sleep
  from timeit import default_timer
  from dask.callbacks import Callback
  from dask.utils import import_required
label_desc: Use image node_id 4 for calling the _Tracker obj's underlying member method code with example usage: obj.run() without return types