From 73048463b4825bc4b315bb78471b08a44e4f659e Mon Sep 17 00:00:00 2001 From: Tohru <65994850+Tohrusky@users.noreply.github.com> Date: Sun, 22 Dec 2024 16:46:45 +0000 Subject: [PATCH] fix: not use async in both uploading and downloading (#21) --- animepipeline/encode/finalrip.py | 30 +++++++++++++++++++++--------- animepipeline/loop.py | 6 ++---- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/animepipeline/encode/finalrip.py b/animepipeline/encode/finalrip.py index 046c5e6..b9732de 100644 --- a/animepipeline/encode/finalrip.py +++ b/animepipeline/encode/finalrip.py @@ -216,12 +216,24 @@ async def download_completed_task(self, video_key: str, save_path: Union[str, Pa get_task_progress_response = await self._get_task_progress(GetTaskProgressRequest(video_key=video_key)) - response = await self.client.get( - url=get_task_progress_response.data.encode_url, # type: ignore - timeout=60 * 60, - ) - if response.status_code != 200: - raise IOError(f"Error downloading file: {response.text}") - - with open(save_path, mode="wb") as v: - v.write(response.content) + try: + logger.info(f"Downloading completed task: {save_path} ...") + t0 = time.time() + + def _download_file() -> None: + with open(save_path, mode="wb") as v: + response = httpx.get( + url=get_task_progress_response.data.encode_url, # type: ignore + timeout=60 * 60, + ) + if response.status_code != 200: + raise IOError(f"Error downloading file: {response.text}") + v.write(response.content) + + _download_file() + del _download_file + gc.collect() + logger.info(f"Download completed task Successfully! path: {save_path}, time: {time.time() - t0:.2f}s") + except Exception as e: + logger.error(f"Error in downloading completed task: {save_path}: {e}") + raise e diff --git a/animepipeline/loop.py b/animepipeline/loop.py index c3d527e..41e39e4 100644 --- a/animepipeline/loop.py +++ b/animepipeline/loop.py @@ -204,14 +204,12 @@ async def pipeline_finalrip(self, task_info: TaskInfo) -> None: ) logger.info(f'FinalRip Task Started for "{task_info.name}" EP {task_info.episode}') except Exception as e: - logger.error(f"Failed to start finalrip task: {e}") - - # wait video cut done - await asyncio.sleep(30) + logger.error(f"Failed to start FinalRip task: {e}") # check task progress while not await self.finalrip_client.check_task_completed(bt_downloaded_path.name): await asyncio.sleep(30) + logger.info(f'FinalRip encode task completed for "{task_info.name}" EP {task_info.episode}') # download temp file to bt_downloaded_path's parent directory temp_saved_path: Path = bt_downloaded_path.parent / (bt_downloaded_path.name + "-encoded.mkv")