File size: 2,525 Bytes
1df74c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import sys
import subprocess
import threading


class ProcessMonitor:
    def __init__(self):
        self.process = None
        self.stdout = ""
        self.stderr = ""
        self.lock = threading.Lock()

    def start_process(self, command):
        self.process = subprocess.Popen(
            command,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            bufsize=1,
            universal_newlines=True,
        )

        # Set pipes to non-blocking mode
        fd_out = self.process.stdout.fileno()
        fd_err = self.process.stderr.fileno()

        if sys.platform != "win32":
            import fcntl

            fl_out = fcntl.fcntl(fd_out, fcntl.F_GETFL)
            fl_err = fcntl.fcntl(fd_err, fcntl.F_GETFL)
            fcntl.fcntl(fd_out, fcntl.F_SETFL, fl_out | os.O_NONBLOCK)
            fcntl.fcntl(fd_err, fcntl.F_SETFL, fl_err | os.O_NONBLOCK)

        # Start threads to read stdout and stderr
        threading.Thread(target=self._read_stdout).start()
        threading.Thread(target=self._read_stderr).start()

    def _read_stdout(self):
        while self.process is not None and self.process.poll() is None:
            try:
                output = self.process.stdout.read()
                if output:
                    with self.lock:
                        self.stdout += output
            except:
                pass

    def _read_stderr(self):
        while self.process is not None and self.process.poll() is None:
            try:
                error = self.process.stderr.read()
                if error:
                    with self.lock:
                        self.stderr += error
            except:
                pass

    def get_output(self):
        with self.lock:
            return self.stdout, self.stderr

    def stop_process(self):
        if self.process:
            self.process.terminate()
            self.process = None


if __name__ == "__main__":
    import time

    pm = ProcessMonitor()
    pm.start_process(
        [
            "python",
            "-u",
            "-c",
            "import time; [print(i) or time.sleep(1) for i in range(5)]",
        ]
    )

    while pm.process and pm.process.poll() is None:
        stdout, stderr = pm.get_output()
        if stdout:
            print("STDOUT:", stdout)
        if stderr:
            print("STDERR:", stderr)
        time.sleep(1)

    stdout, stderr = pm.get_output()
    print("Final STDOUT:", stdout)
    print("Final STDERR:", stderr)