Get information about a process and its children
(pid: int)
| 41 | |
| 42 | |
| 43 | def get_process_tree_info(pid: int) -> str: |
| 44 | """Get information about a process and its children""" |
| 45 | import psutil # noqa: PLC0415 - lazy: ~21ms saved on normal execution paths |
| 46 | |
| 47 | try: |
| 48 | process = psutil.Process(pid) |
| 49 | info = [f"Process {pid} ({process.name()})"] |
| 50 | info.append(f"Status: {process.status()}") |
| 51 | info.append(f"CPU Times: {process.cpu_times()}") |
| 52 | info.append(f"Memory: {process.memory_info()}") |
| 53 | |
| 54 | # Get child processes |
| 55 | children = process.children(recursive=True) |
| 56 | if children: |
| 57 | info.append("\nChild processes:") |
| 58 | for child in children: |
| 59 | info.append(f" Child {child.pid} ({child.name()})") |
| 60 | info.append(f" Status: {child.status()}") |
| 61 | info.append(f" CPU Times: {child.cpu_times()}") |
| 62 | info.append(f" Memory: {child.memory_info()}") |
| 63 | |
| 64 | return "\n".join(info) |
| 65 | except KeyboardInterrupt as ki: |
| 66 | handle_keyboard_interrupt(ki) |
| 67 | raise |
| 68 | except: |
| 69 | return f"Could not get process info for PID {pid}" |
| 70 | |
| 71 | |
| 72 | def kill_process_tree(pid: int) -> None: |
no test coverage detected