@@ -63,28 +63,26 @@ def _io_thread(self):
63
63
err_wait = select .poll ()
64
64
err_wait .register (self .process .stderr , select .POLLIN | select .POLLHUP )
65
65
66
- with open (self .output_path , "wt" , buffering = 1 ) as f :
66
+ with open (self .output_path , "wt" , buffering = 1 ) as f : # Enable line buffering for immediate output
67
67
f .write ("PROCESS START: %s\n " % time .ctime ())
68
68
f .flush ()
69
69
while not self .done :
70
70
out_changes = out_wait .poll (0.1 )
71
- for fd , _ in out_changes :
72
- if fd == self .process .stdout .fileno ():
73
- while True :
74
- out_line = self .process .stdout .readline ()
75
- if not out_line :
76
- break
77
- f .write (out_line )
78
- f .flush ()
79
- self .output_lines .put (out_line )
71
+ if self .process .stdout .fileno () in [fd for (fd , _ ) in out_changes ]:
72
+ while True :
73
+ out_line = self .process .stdout .readline ()
74
+ if not out_line :
75
+ break
76
+ f .write (out_line )
77
+ f .flush ()
78
+ self .output_lines .put (out_line )
80
79
81
80
err_changes = err_wait .poll (0 )
82
- for fd , _ in err_changes :
83
- if fd == self .process .stderr .fileno ():
84
- err_line = self .process .stderr .readline ()
85
- if err_line :
86
- f .write (f"!!STDERR!! : { err_line } " )
87
- f .flush ()
81
+ if self .process .stderr .fileno () in [fd for (fd , _ ) in err_changes ]:
82
+ err_line = self .process .stderr .readline ()
83
+ if err_line :
84
+ f .write (f"!!STDERR!! : { err_line } " )
85
+ f .flush ()
88
86
f .write ("PROCESS END: %s\n " % time .ctime ())
89
87
f .flush ()
90
88
@@ -96,7 +94,7 @@ def __enter__(self):
96
94
stdout = subprocess .PIPE ,
97
95
stderr = subprocess .PIPE ,
98
96
text = True ,
99
- bufsize = 1 ,
97
+ bufsize = 1 , # Enable line buffering for immediate output from subprocess
100
98
)
101
99
self .io_thread = threading .Thread (target = self ._io_thread )
102
100
self .io_thread .start ()
0 commit comments