From 431e907eb6df1970cf53c4151e75b9221d2c83b1 Mon Sep 17 00:00:00 2001 From: Robert Colfin Date: Fri, 13 Dec 2024 10:50:47 -0500 Subject: [PATCH 1/2] Adding async_output when already running async the run call to asyncio.run will raise an exception. --- ffmpeg_streaming/_media.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ffmpeg_streaming/_media.py b/ffmpeg_streaming/_media.py index e8aab5b..61ab9e9 100644 --- a/ffmpeg_streaming/_media.py +++ b/ffmpeg_streaming/_media.py @@ -69,6 +69,16 @@ def method(*args, **kwargs): return method + async def async_output(self, output: str = None, clouds: CloudManager = None, + run_command: bool = True, ffmpeg_bin: str = 'ffmpeg', monitor: callable = None, **options): + """ + @TODO: add documentation + """ + self.output(output, clouds, run_command=False, ffmpeg_bin=ffmpeg_bin, monitor=monitor, **options) + + if run_command: + await self.async_run(ffmpeg_bin="ffmpeg") + def output(self, output: str = None, clouds: CloudManager = None, run_command: bool = True, ffmpeg_bin: str = 'ffmpeg', monitor: callable = None, **options): """ From 4cd5a3408fb995ca70530b8fdb29ea6f3d6e767f Mon Sep 17 00:00:00 2001 From: Robert Colfin Date: Fri, 13 Dec 2024 14:53:08 -0500 Subject: [PATCH 2/2] Updating run to be more resilient in the case there is an existing loop running. --- ffmpeg_streaming/_media.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ffmpeg_streaming/_media.py b/ffmpeg_streaming/_media.py index 61ab9e9..b609e2e 100644 --- a/ffmpeg_streaming/_media.py +++ b/ffmpeg_streaming/_media.py @@ -126,7 +126,13 @@ def run(self, ffmpeg_bin, monitor: callable = None, **options): @TODO: add documentation """ if async_run := options.pop('async_run', True): - asyncio.run(self.async_run(ffmpeg_bin, monitor, **options)) + try: + # Raise a RuntimeError if no loop is running: + asyncio.get_running_loop() + return asyncio.create_task(self.async_run(ffmpeg_bin, monitor, **options)) + except RuntimeError: + # No loop is running: + asyncio.run(self.async_run(ffmpeg_bin, monitor, **options)) else: self._run(ffmpeg_bin, monitor, **options)