@@ -63,26 +63,27 @@ 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" ) as f :
66
+ with open (self .output_path , "wt" , buffering = 1 ) as f :
67
67
f .write ("PROCESS START: %s\n " % time .ctime ())
68
+ f .flush ()
68
69
while not self .done :
69
70
changes = out_wait .poll (0.1 )
70
- if changes :
71
- out_line = self .process .stdout .readline ()
72
- if not out_line :
73
- # stdout closed (otherwise readline should have at least \n)
74
- continue
75
- f .write (out_line )
76
- self .output_lines .put (out_line )
77
-
78
- changes = err_wait .poll (0 )
79
- if changes :
80
- err_line = self .process .stderr .readline ()
81
- if not err_line :
82
- # stderr closed (otherwise readline should have at least \n)
83
- continue
84
- f .write (f"!!STDERR!! : { err_line } " )
71
+ for fd , _ in 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 )
80
+ elif fd == self .process .stderr .fileno ():
81
+ err_line = self .process .stderr .readline ()
82
+ if err_line :
83
+ f .write (f"!!STDERR!! : { err_line } " )
84
+ f .flush ()
85
85
f .write ("PROCESS END: %s\n " % time .ctime ())
86
+ f .flush ()
86
87
87
88
def __enter__ (self ):
88
89
self .done = False
@@ -92,6 +93,7 @@ def __enter__(self):
92
93
stdout = subprocess .PIPE ,
93
94
stderr = subprocess .PIPE ,
94
95
text = True ,
96
+ bufsize = 1 ,
95
97
)
96
98
self .io_thread = threading .Thread (target = self ._io_thread )
97
99
self .io_thread .start ()
0 commit comments