Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: add busy loop utils

NTFSvolumee748e87a7062
diff --git a/cyberdrop_dl/aio.py b/cyberdrop_dl/aio.py
index 159b90d1d..a8ceb8c8b 100644
--- a/cyberdrop_dl/aio.py
+++ b/cyberdrop_dl/aio.py
@@ -436,3 +436,22 @@ async def temp_dir() -> AsyncGenerator[Path]:
yield Path(temp_dir.name)
finally:
await asyncio.to_thread(temp_dir.cleanup)
+
+
+def periodic_sleep(period: int, /) -> Callable[[], Awaitable[None]]:
+ """Yield control to the event loop every n calls
+
+ To use within busy blocking loops"""
+
+ if period <= 0:
+ raise ValueError("period must be a positive integer")
+
+ calls = 0
+
+ async def sleep() -> None:
+ nonlocal calls
+ calls += 1
+ if calls % period == 0:
+ await asyncio.sleep(0)
+
+ return sleep