| 94 | self.cpu_samples = [] |
| 95 | |
| 96 | def monitor_cpu(): |
| 97 | # Get initial CPU reading to establish baseline |
| 98 | if self.process and self.process.is_running(): |
| 99 | try: |
| 100 | initial_cpu = self.process.cpu_percent() |
| 101 | print(f"Initial CPU reading: {initial_cpu}%") |
| 102 | except (psutil.NoSuchProcess, psutil.AccessDenied): |
| 103 | pass |
| 104 | |
| 105 | while self.monitoring: |
| 106 | try: |
| 107 | if self.process and self.process.is_running(): |
| 108 | cpu = self.process.cpu_percent() |
| 109 | self.cpu_samples.append(cpu) |
| 110 | if ( |
| 111 | len(self.cpu_samples) % 50 == 0 |
| 112 | ): # Log every 5 seconds at 0.1s intervals |
| 113 | print( |
| 114 | f"CPU monitoring: {len(self.cpu_samples)} samples, latest: {cpu}%" |
| 115 | ) |
| 116 | time.sleep(sample_interval) |
| 117 | except (psutil.NoSuchProcess, psutil.AccessDenied): |
| 118 | print("Lost connection to cosdata process during monitoring") |
| 119 | break |
| 120 | |
| 121 | self.monitor_thread = threading.Thread(target=monitor_cpu, daemon=True) |
| 122 | self.monitor_thread.start() |