From ebdb33bd117a12e2cc20d49e918a93a36adb46f2 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Thu, 10 Jan 2019 18:14:57 -0500 Subject: [PATCH] blob_availability fix --- lbrynet/extras/daemon/Daemon.py | 18 ++++++++---------- lbrynet/p2p/SinglePeerDownloader.py | 18 ++++++++---------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/lbrynet/extras/daemon/Daemon.py b/lbrynet/extras/daemon/Daemon.py index b5b725b6b..4fa1373f5 100644 --- a/lbrynet/extras/daemon/Daemon.py +++ b/lbrynet/extras/daemon/Daemon.py @@ -938,7 +938,7 @@ class Daemon(metaclass=JSONRPCServerType): reachable_peers = [] unreachable_peers = [] try: - peers = await d2f(self.jsonrpc_peer_list(blob_hash, search_timeout)) + peers = await self.jsonrpc_peer_list(blob_hash, search_timeout) peer_infos = [{"peer": Peer(x['host'], x['port']), "blob_hash": blob_hash, "timeout": blob_timeout} for x in peers] @@ -946,16 +946,14 @@ class Daemon(metaclass=JSONRPCServerType): dl_peers = [] dl_results = [] for peer_info in peer_infos: - d = downloader.download_temp_blob_from_peer(**peer_info) - dl.append(d) + dl.append(downloader.download_temp_blob_from_peer(**peer_info)) dl_peers.append("%s:%i" % (peer_info['peer'].host, peer_info['peer'].port)) - for dl_peer, (success, download_result) in zip(dl_peers, (await d2f(defer.DeferredList(dl)))): - if success: - if download_result: - reachable_peers.append(dl_peer) - else: - unreachable_peers.append(dl_peer) - dl_results.append(download_result) + for dl_peer, download_result in zip(dl_peers, await asyncio.gather(*dl)): + if download_result: + reachable_peers.append(dl_peer) + else: + unreachable_peers.append(dl_peer) + dl_results.append(download_result) is_available = any(dl_results) except Exception as err: return {'error': "Failed to get peers for blob: %s" % err} diff --git a/lbrynet/p2p/SinglePeerDownloader.py b/lbrynet/p2p/SinglePeerDownloader.py index 682939427..243d84154 100644 --- a/lbrynet/p2p/SinglePeerDownloader.py +++ b/lbrynet/p2p/SinglePeerDownloader.py @@ -4,6 +4,7 @@ import tempfile from twisted.internet import defer, threads, reactor +from lbrynet.extras.compat import d2f from lbrynet.blob.blob_file import BlobFile from lbrynet.p2p.BlobManager import DiskBlobManager from lbrynet.p2p.RateLimiter import DummyRateLimiter @@ -94,16 +95,13 @@ class SinglePeerDownloader: yield connection_manager.stop() defer.returnValue(result) - @defer.inlineCallbacks - def download_temp_blob_from_peer(self, peer, timeout, blob_hash): - tmp_dir = yield threads.deferToThread(tempfile.mkdtemp) - tmp_storage = SQLiteStorage(tmp_dir) - yield tmp_storage.setup() + async def download_temp_blob_from_peer(self, peer, timeout, blob_hash): + tmp_storage = SQLiteStorage(':memory:') + await tmp_storage.open() + tmp_dir = tempfile.mkdtemp() tmp_blob_manager = DiskBlobManager(tmp_dir, tmp_storage) try: - result = yield self.download_blob_from_peer(peer, timeout, blob_hash, tmp_blob_manager) + return await d2f(self.download_blob_from_peer(peer, timeout, blob_hash, tmp_blob_manager)) finally: - yield tmp_blob_manager.stop() - yield tmp_storage.stop() - yield threads.deferToThread(shutil.rmtree, tmp_dir) - defer.returnValue(result) + await tmp_storage.close() + shutil.rmtree(tmp_dir)