Skip to content

Commit da4b191

Browse files
committed
changed: Popen run _monitor thread only if needed
1 parent e34e455 commit da4b191

1 file changed

Lines changed: 24 additions & 23 deletions

File tree

src/ffmpegio/ffmpegprocess.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ def __init__(
165165

166166
# run progress monitor
167167
self._progmon = None if progress is None else ProgressMonitorThread(progress)
168+
self._monitor = None
168169

169170
# start FFmpeg process
170171
exec(
@@ -185,27 +186,21 @@ def __init__(
185186
self._progmon.start()
186187

187188
# start the process monitor to perform the cleanup when FFmpeg terminates
188-
# if auto-close mode not set, audo-close only if stream handlers are not given
189-
if close_stdin is None:
190-
close_stdin = stdin is None
191-
if close_stdout is None:
192-
close_stdout = stdout is None
193-
if close_stderr is None:
194-
close_stderr = stderr is None
195-
if self._progmon:
196-
if on_exit:
197-
try:
198-
on_exit = (self._progmon.join, *on_exit)
199-
except:
200-
on_exit = (self._progmon.join, on_exit)
201-
else:
202-
on_exit = self._progmon.join
203-
204-
self._monitor = _Thread(
205-
target=monitor_process,
206-
args=(self, close_stdin, close_stdout, close_stderr, on_exit),
207-
)
208-
self._monitor.start()
189+
if self._progmon or on_exit:
190+
if self._progmon:
191+
if on_exit:
192+
try:
193+
on_exit = (self._progmon.join, *on_exit)
194+
except:
195+
on_exit = (self._progmon.join, on_exit)
196+
else:
197+
on_exit = self._progmon.join
198+
199+
self._monitor = _Thread(
200+
target=monitor_process,
201+
args=(self, on_exit),
202+
)
203+
self._monitor.start()
209204

210205
def wait(self, timeout=None):
211206
"""Wait for FFmpeg process to terminate; returns self.returncode
@@ -230,12 +225,18 @@ def wait(self, timeout=None):
230225
def terminate(self):
231226
"""Terminate the FFmpeg process"""
232227
super().terminate()
233-
self._monitor.join()
228+
try:
229+
self._monitor.join()
230+
except:
231+
pass
234232

235233
def kill(self):
236234
"""Kill the FFmpeg process"""
237235
super().kill()
238-
self._monitor.join()
236+
try:
237+
self._monitor.join()
238+
except:
239+
pass
239240

240241
def send_signal(self, sig: int):
241242
"""Sends the signal signal to the FFmpeg process

0 commit comments

Comments
 (0)