File size: 1,679 Bytes
e0265b9
 
 
c61c435
e0265b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c61c435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from __future__ import annotations

import subprocess
from typing import Any


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


def terminate_process_tree(process: Any, *, timeout: float = 3.0) -> None:
    """Terminate a process and any children it launched."""
    try:
        import psutil

        parent = psutil.Process(process.pid)
        children = parent.children(recursive=True)
        targets = [*children, parent]
        for target in targets:
            try:
                target.terminate()
            except (psutil.NoSuchProcess, psutil.AccessDenied):
                continue
        _gone, alive = psutil.wait_procs(targets, timeout=timeout)
        for target in alive:
            try:
                target.kill()
            except (psutil.NoSuchProcess, psutil.AccessDenied):
                continue
        return
    except Exception:
        pass
    try:
        process.terminate()
    except Exception:
        return
    try:
        process.wait(timeout=timeout)
    except Exception:
        try:
            process.kill()
        except Exception:
            pass