Skip to content

Commit

Permalink
Pass timeout to Popen.wait()
Browse files Browse the repository at this point in the history
  • Loading branch information
jonghwanhyeon committed Jan 9, 2024
1 parent 28aba72 commit bd4750e
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions ffmpeg/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,24 @@ def execute(self, stream: Optional[Union[bytes, IO[bytes]]] = None, timeout: Opt
stderr=subprocess.PIPE,
)

self._executed = True

with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
self._executed = True
futures = [
executor.submit(self._write_stdin, stream),
executor.submit(self._read_stdout),
executor.submit(self._handle_stderr),
executor.submit(self._process.wait),
executor.submit(self._process.wait, timeout),
]
done, pending = concurrent.futures.wait(futures, return_when=concurrent.futures.FIRST_EXCEPTION)
self._executed = False

for future in concurrent.futures.as_completed(futures, timeout=timeout):
for future in done:
exception = future.exception()
if exception is not None:
self._process.terminate()
raise exception
concurrent.futures.wait(pending)

self._executed = False
raise exception

if self._process.returncode == 0:
self.emit("completed")
Expand Down

0 comments on commit bd4750e

Please sign in to comment.