From 278de926e3cd2dceec14f43383cd3a5a512f4d47 Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Fri, 28 May 2021 18:24:50 +0200 Subject: [PATCH] move timeout to StreamProvider --- backend/stream_providers.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/backend/stream_providers.py b/backend/stream_providers.py index 7bd3180..f8f5abc 100755 --- a/backend/stream_providers.py +++ b/backend/stream_providers.py @@ -76,6 +76,14 @@ class StreamProvider(): proxy = str(proxy) if len(proxy) > 5: self.proxy = "socks5://" + proxy + async def run(self): + data = None + try: + future = asyncio.to_thread(self.stream) + data = await asyncio.wait_for(future, timeout=5) + except Exception as e: + self.logger.info(e) + return data class StreamlinkRunner(StreamProvider): def stream(self): @@ -93,8 +101,6 @@ class StreamlinkRunner(StreamProvider): except Exception as e: self.logger.info(e) return StreamData(None, None, None, None, False) - async def run(self): - return await asyncio.to_thread(self.stream) class YoutubeRunner(StreamProvider): def stream(self): @@ -139,8 +145,6 @@ class YoutubeRunner(StreamProvider): except Exception as e: self.logger.info(e) return StreamData(best_url, thumbnail, title, description, True) - async def run(self): - return await asyncio.to_thread(self.stream) class MetaRunner(StreamProvider): def stream(self): @@ -153,8 +157,6 @@ class MetaRunner(StreamProvider): except Exception as e: self.logger.info(e) return StreamData(None, data.get("og:image"), data.get("og:title"), data.get("og:description"), False) - async def run(self): - return await asyncio.to_thread(self.stream) async def get_ytdl(upstream, proxy, logger): result = None @@ -195,12 +197,8 @@ async def get_any(upstream, proxy, logger): tasks.append(asyncio.create_task(get_ytdl(upstream, proxy, logger))) tasks.append(asyncio.create_task(get_meta(upstream, proxy, logger))) result = StreamData(None, None, None, None, False) - for task in asyncio.as_completed(tasks, timeout=5.0): - temp_result = None - try: - temp_result = await task - except Exception as e: - logger.info(e) + for task in asyncio.as_completed(tasks): + temp_result = await task if isinstance(temp_result, StreamData): result.update("upstream", temp_result.upstream(), temp_result.override) result.update("thumbnail", temp_result.thumbnail(), temp_result.override)