Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: add explicit shutdown method

NTFSvolumee9422fd92790
diff --git a/cyberdrop_dl/commands/scrape.py b/cyberdrop_dl/commands/scrape.py
index d6b6eab8a..4faa5af01 100644
--- a/cyberdrop_dl/commands/scrape.py
+++ b/cyberdrop_dl/commands/scrape.py
@@ -48,6 +48,7 @@ async def _scrape(manager: Manager, source: URLsSource | RetryScrapeSource) -> N
logger.info("Starting CDL...")
async with ScrapeMapper(manager)() as scrape_mapper:
stats = await scrape_mapper.run(source)
+ scrape_mapper.shutdown()
await _post_runtime(manager)
stats_summary = manager.print_stats(stats)
diff --git a/cyberdrop_dl/scrape_mapper.py b/cyberdrop_dl/scrape_mapper.py
index 6c041513b..5a42f6735 100644
--- a/cyberdrop_dl/scrape_mapper.py
+++ b/cyberdrop_dl/scrape_mapper.py
@@ -110,6 +110,11 @@ class ScrapeMapper:
_seen_urls: set[AbsoluteHttpURL] = dataclasses.field(init=False, default_factory=set, repr=False)
_factory: CrawlerFactory = dataclasses.field(init=False)
_ready: bool = dataclasses.field(init=False, default=False)
+ _shutting_down: bool = dataclasses.field(init=False, default=False)
+
+ def shutdown(self) -> None:
+ "Shutdown the scrape queue and setup handling of KeyboardInterrupt"
+ self._shutting_down = True
def __repr__(self) -> str:
fields = (
@@ -191,15 +196,12 @@ class ScrapeMapper:
try:
with self.tui():
yield
- except asyncio.CancelledError as e:
+ except asyncio.CancelledError:
# This is a KeyboardInterrupt cause we never cancel tasks
- try:
- _ = self.manager.scrape_mapper
- except AttributeError:
- # self.__call__ did not get a change to finish setup
- raise e from None
- else:
- logger.warning("Scraping aborted (KeyboardInterrupt)")
+ if not self._shutting_down:
+ raise
+
+ logger.warning("Scraping aborted ('Ctrl + C' pressed)")
async def __async_init__(self) -> None:
if self._ready:
@@ -222,6 +224,9 @@ class ScrapeMapper:
)
async def run(self, src: URLsSource | RetryScrapeSource | None = None) -> ScrapeStats:
+ if self._shutting_down:
+ raise RuntimeError("Scraper is already shutting down")
+
await self.__async_init__()
if src is None:
return ScrapeStats("")