File size: 673 Bytes
e0265b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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