| 253 | ] |
| 254 | |
| 255 | def run(self): |
| 256 | psutil = import_required( |
| 257 | "psutil", "Tracking resource usage requires `psutil` to be installed" |
| 258 | ) |
| 259 | self.parent = psutil.Process(self.parent_pid) |
| 260 | |
| 261 | pid = current_process() |
| 262 | data = [] |
| 263 | while True: |
| 264 | try: |
| 265 | msg = self.child_conn.recv() |
| 266 | except KeyboardInterrupt: |
| 267 | continue |
| 268 | if msg == "shutdown": |
| 269 | break |
| 270 | elif msg == "collect": |
| 271 | ps = self._update_pids(pid) |
| 272 | while not data or not self.child_conn.poll(): |
| 273 | tic = default_timer() |
| 274 | mem = cpu = 0 |
| 275 | for p in ps: |
| 276 | try: |
| 277 | mem2 = p.memory_info().rss |
| 278 | cpu2 = p.cpu_percent() |
| 279 | except Exception: # could be a few different exceptions |
| 280 | pass |
| 281 | else: |
| 282 | # Only increment if both were successful |
| 283 | mem += mem2 |
| 284 | cpu += cpu2 |
| 285 | data.append((tic, mem / 1e6, cpu)) |
| 286 | sleep(self.dt) |
| 287 | elif msg == "send_data": |
| 288 | self.child_conn.send(data) |
| 289 | data = [] |
| 290 | self.child_conn.close() |
| 291 | |
| 292 | |
| 293 | CacheData = namedtuple( |