3333__all__ = ["run" , "Popen" , "PIPE" , "DEVNULL" ]
3434
3535
36- def monitor_process (
37- proc , on_exit = None
38- ):
36+ def monitor_process (proc , on_exit = None ):
3937 """thread function to monitor subprocess termination
4038
4139 :param proc: subprocess to be monitored
@@ -51,11 +49,8 @@ def monitor_process(
5149 proc .wait ()
5250 if on_exit is not None :
5351 returncode = proc .returncode
54- try :
55- on_exit (returncode )
56- except :
57- for fcn in on_exit :
58- fcn (returncode )
52+ for fcn in on_exit :
53+ fcn (returncode )
5954
6055
6156class Popen (_sp .Popen ):
@@ -160,21 +155,26 @@ def __init__(
160155 self ._progmon .start ()
161156
162157 # start the process monitor to perform the cleanup when FFmpeg terminates
163- if self ._progmon or on_exit :
158+ if self ._progmon or capture_log or on_exit :
159+ if on_exit is None :
160+ on_exit = []
161+ else :
162+ try :
163+ on_exit = [* on_exit ]
164+ except :
165+ on_exit = [on_exit ]
166+
167+ if capture_log :
168+ on_exit .append (lambda _ : self .stderr .close ())
169+
164170 if self ._progmon :
165- if on_exit :
166- try :
167- on_exit = (self ._progmon .join , * on_exit )
168- except :
169- on_exit = (self ._progmon .join , on_exit )
170- else :
171- on_exit = self ._progmon .join
172-
173- self ._monitor = _Thread (
174- target = monitor_process ,
175- args = (self , on_exit ),
176- )
177- self ._monitor .start ()
171+ on_exit .append (lambda _ : self ._progmon .join ())
172+
173+ self ._monitor = _Thread (
174+ target = monitor_process ,
175+ args = (self , on_exit ),
176+ )
177+ self ._monitor .start ()
178178
179179 def wait (self , timeout = None ):
180180 """Wait for FFmpeg process to terminate; returns self.returncode
0 commit comments