From 8f8eacf3242ac5a989b26419a01a596b2df3a706 Mon Sep 17 00:00:00 2001 From: mkb79 Date: Wed, 15 Nov 2023 10:58:37 +0100 Subject: [PATCH] fix: Fixing an issue with unawaited coroutines (#166) Fixing an issue with unawaited coroutines when the download command exited abnormal. * Refactor consume function and queue_job function * doc: Update CHANGELOG.md --- CHANGELOG.md | 1 + src/audible_cli/cmds/cmd_download.py | 118 +++++++++++++-------------- 2 files changed, 60 insertions(+), 59 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f719673..f51801c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Bugfix - Avoid tqdm progress bar interruption by logger’s output to console. +- Fixing an issue with unawaited coroutines when the download command exited abnormal. ### Misc diff --git a/src/audible_cli/cmds/cmd_download.py b/src/audible_cli/cmds/cmd_download.py index 10fe248..f8d7232 100644 --- a/src/audible_cli/cmds/cmd_download.py +++ b/src/audible_cli/cmds/cmd_download.py @@ -418,9 +418,9 @@ async def download_aaxc( async def consume(queue, ignore_errors): while True: - item = await queue.get() + cmd, kwargs = await queue.get() try: - await item + await cmd(**kwargs) except Exception as e: logger.error(e) if not ignore_errors: @@ -450,73 +450,73 @@ def queue_job( if get_cover: for cover_size in cover_sizes: - queue.put_nowait( - download_cover( - client=client, - output_dir=output_dir, - base_filename=base_filename, - item=item, - res=cover_size, - overwrite_existing=overwrite_existing - ) - ) + cmd = download_cover + kwargs = { + "client": client, + "output_dir": output_dir, + "base_filename": base_filename, + "item": item, + "res": cover_size, + "overwrite_existing": overwrite_existing + } + queue.put_nowait((cmd, kwargs)) if get_pdf: - queue.put_nowait( - download_pdf( - client=client, - output_dir=output_dir, - base_filename=base_filename, - item=item, - overwrite_existing=overwrite_existing - ) - ) + cmd = download_pdf + kwargs = { + "client": client, + "output_dir": output_dir, + "base_filename": base_filename, + "item": item, + "overwrite_existing": overwrite_existing + } + queue.put_nowait((cmd, kwargs)) if get_chapters: - queue.put_nowait( - download_chapters( - output_dir=output_dir, - base_filename=base_filename, - item=item, - quality=quality, - overwrite_existing=overwrite_existing - ) - ) + cmd = download_chapters + kwargs = { + "output_dir": output_dir, + "base_filename": base_filename, + "item": item, + "quality": quality, + "overwrite_existing": overwrite_existing + } + queue.put_nowait((cmd, kwargs)) if get_annotation: - queue.put_nowait( - download_annotations( - output_dir=output_dir, - base_filename=base_filename, - item=item, - overwrite_existing=overwrite_existing - ) - ) + cmd = download_annotations + kwargs = { + "output_dir": output_dir, + "base_filename": base_filename, + "item": item, + "overwrite_existing": overwrite_existing + } + queue.put_nowait((cmd, kwargs)) if get_aax: - queue.put_nowait( - download_aax( - client=client, - output_dir=output_dir, - base_filename=base_filename, - item=item, - quality=quality, - overwrite_existing=overwrite_existing, - aax_fallback=aax_fallback - ) - ) + cmd = download_aax + kwargs = { + "client": client, + "output_dir": output_dir, + "base_filename": base_filename, + "item": item, + "quality": quality, + "overwrite_existing": overwrite_existing, + "aax_fallback": aax_fallback + } + queue.put_nowait((cmd, kwargs)) if get_aaxc: - queue.put_nowait( - download_aaxc( - client=client, - output_dir=output_dir, - base_filename=base_filename, - item=item, - quality=quality, - overwrite_existing=overwrite_existing - ) - ) + cmd = download_aaxc + kwargs = { + "client": client, + "output_dir": output_dir, + "base_filename": base_filename, + "item": item, + "quality": quality, + "overwrite_existing": overwrite_existing + } + queue.put_nowait((cmd, kwargs)) def display_counter():