From 1c1b5096be2a636f3b1720c59fc252cbad733787 Mon Sep 17 00:00:00 2001 From: Kay Kurokawa Date: Tue, 20 Jun 2017 15:03:17 -0400 Subject: [PATCH] use LoopingCall in SingleProgressManager to simplify it --- .../core/client/StandaloneBlobDownloader.py | 41 +++++-------------- lbrynet/core/utils.py | 7 ++++ 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/lbrynet/core/client/StandaloneBlobDownloader.py b/lbrynet/core/client/StandaloneBlobDownloader.py index e4f44ca6e..1e247b0a2 100644 --- a/lbrynet/core/client/StandaloneBlobDownloader.py +++ b/lbrynet/core/client/StandaloneBlobDownloader.py @@ -6,10 +6,10 @@ from lbrynet.core.client.BlobRequester import BlobRequester from lbrynet.core.client.ConnectionManager import ConnectionManager from lbrynet.core.client.DownloadManager import DownloadManager from lbrynet.core.Error import InvalidBlobHashError -from lbrynet.core.utils import is_valid_blobhash +from lbrynet.core.utils import is_valid_blobhash, safe_start_looping_call, safe_stop_looping_call from twisted.python.failure import Failure from twisted.internet import defer - +from twisted.internet import LoopingCall log = logging.getLogger(__name__) @@ -34,34 +34,24 @@ class SingleBlobMetadataHandler(object): class SingleProgressManager(object): def __init__(self, finished_callback, download_manager): self.finished_callback = finished_callback - self.finished = False self.download_manager = download_manager - self._next_check_if_finished = None + + self.checker = LoopingCall(self._check_if_finished) def start(self): - - from twisted.internet import reactor - - assert self._next_check_if_finished is None - self._next_check_if_finished = reactor.callLater(0, self._check_if_finished) + safe_start_looping_call(self.checker,1) return defer.succeed(True) def stop(self): - if self._next_check_if_finished is not None: - self._next_check_if_finished.cancel() - self._next_check_if_finished = None + safe_stop_looping_call(self.checker) return defer.succeed(True) def _check_if_finished(self): - - from twisted.internet import reactor - - self._next_check_if_finished = None - if self.finished is False: - if self.stream_position() == 1: - self.blob_downloaded(self.download_manager.blobs[0], 0) - else: - self._next_check_if_finished = reactor.callLater(1, self._check_if_finished) + if self.stream_position() == 1: + blob_downloaded = self.download_manager.blobs[0] + log.debug("The blob %s has been downloaded. Calling the finished callback", str(blob_downloaded)) + safe_stop_looping_call(self.checker) + self.finished_callback(blob_downloaded) def stream_position(self): blobs = self.download_manager.blobs @@ -74,15 +64,6 @@ class SingleProgressManager(object): assert len(blobs) == 1 return [b for b in blobs.itervalues() if not b.is_validated()] - def blob_downloaded(self, blob, blob_num): - - from twisted.internet import reactor - - log.debug("The blob %s has been downloaded. Calling the finished callback", str(blob)) - if self.finished is False: - self.finished = True - reactor.callLater(0, self.finished_callback, blob) - class DummyBlobHandler(object): def __init__(self): diff --git a/lbrynet/core/utils.py b/lbrynet/core/utils.py index eeb3f6be7..9dd629d92 100644 --- a/lbrynet/core/utils.py +++ b/lbrynet/core/utils.py @@ -49,6 +49,13 @@ def call_later(delay, func, *args, **kwargs): from twisted.internet import reactor return reactor.callLater(delay, func, *args, **kwargs) +def safe_start_looping_call(looping_call, interval_sec): + if not looping_call.running: + looping_call.start(interval_sec) + +def safe_stop_looping_call(looping_call): + if looping_call.running: + looping_call.stop() def generate_id(num=None): h = get_lbry_hash_obj()