| from __future__ import annotations | |
| import subprocess | |
| def set_process_tree_paused(process: subprocess.Popen, paused: bool) -> bool: | |
| """Suspend/resume a trainer process tree when psutil is available.""" | |
| try: | |
| import psutil | |
| parent = psutil.Process(process.pid) | |
| children = parent.children(recursive=True) | |
| targets = [*children, parent] if paused else [parent, *children] | |
| for target in targets: | |
| try: | |
| target.suspend() if paused else target.resume() | |
| except (psutil.NoSuchProcess, psutil.AccessDenied): | |
| continue | |
| return True | |
| except Exception: | |
| return False | |