From 703c391f99b2120087b3bd8c87c53b61997ba285 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Tue, 19 Oct 2021 22:06:41 -0300 Subject: [PATCH] schedule the download task instead --- lbry/extras/daemon/components.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lbry/extras/daemon/components.py b/lbry/extras/daemon/components.py index 4f3692519..44f128f5d 100644 --- a/lbry/extras/daemon/components.py +++ b/lbry/extras/daemon/components.py @@ -391,6 +391,7 @@ class BackgroundDownloader(Component): self.download_loop_delay_seconds = 60 self.finished_iteration = asyncio.Event() self.requested_blobs = deque(maxlen=10) + self.ongoing_download: typing.Optional[asyncio.Task] = None @property def component(self) -> 'BackgroundDownloader': @@ -404,10 +405,10 @@ class BackgroundDownloader(Component): if self.component_manager.has_component(DHT_COMPONENT): node = self.component_manager.get_component(DHT_COMPONENT) self.requested_blobs = node.protocol.data_store.requested_blobs - if self.requested_blobs: + if self.requested_blobs and (not self.ongoing_download or self.ongoing_download.done()): blob_hash = self.requested_blobs.pop() - await self.download_blobs(blob_hash) - self.finished_iteration.set() + self.ongoing_download = asyncio.create_task(self.download_blobs(blob_hash)) + self.ongoing_download.add_done_callback(lambda _: self.finished_iteration.set()) self.finished_iteration.clear() await asyncio.sleep(self.download_loop_delay_seconds) @@ -428,6 +429,8 @@ class BackgroundDownloader(Component): self.task = asyncio.create_task(self.loop()) async def stop(self): + if self.ongoing_download and not self.ongoing_download.done(): + self.ongoing_download.cancel() self.task.cancel()