asyncio progress for victor

This commit is contained in:
Lex Berezhny 2018-10-16 11:57:43 -04:00
parent d2083bc17e
commit 21702ec43c
2 changed files with 15 additions and 18 deletions

View file

@ -238,34 +238,33 @@ class HeadersComponent(Component):
return os.stat(self.headers_file).st_size return os.stat(self.headers_file).st_size
return 0 return 0
@defer.inlineCallbacks async def get_remote_height(self):
def get_remote_height(self):
ledger = SimpleNamespace() ledger = SimpleNamespace()
ledger.config = { ledger.config = {
'default_servers': conf.settings['lbryum_servers'], 'default_servers': conf.settings['lbryum_servers'],
'data_path': conf.settings['lbryum_wallet_dir'] 'data_path': conf.settings['lbryum_wallet_dir']
} }
net = Network(ledger) net = Network(ledger)
net.start() first_connection = net.on_connected.first
yield net.on_connected.first asyncio.ensure_future(net.start())
remote_height = yield net.get_server_height() await first_connection
yield net.stop() remote_height = await net.get_server_height()
defer.returnValue(remote_height) await net.stop()
return remote_height
@defer.inlineCallbacks async def should_download_headers_from_s3(self):
def should_download_headers_from_s3(self):
if conf.settings['blockchain_name'] != "lbrycrd_main": if conf.settings['blockchain_name'] != "lbrycrd_main":
defer.returnValue(False) return False
self._check_header_file_integrity() self._check_header_file_integrity()
s3_headers_depth = conf.settings['s3_headers_depth'] s3_headers_depth = conf.settings['s3_headers_depth']
if not s3_headers_depth: if not s3_headers_depth:
defer.returnValue(False) return False
local_height = self.local_header_file_height() local_height = self.local_header_file_height()
remote_height = yield self.get_remote_height() remote_height = await self.get_remote_height()
log.info("remote height: %i, local height: %i", remote_height, local_height) log.info("remote height: %i, local height: %i", remote_height, local_height)
if remote_height > (local_height + s3_headers_depth): if remote_height > (local_height + s3_headers_depth):
defer.returnValue(True) return True
defer.returnValue(False) return False
def _check_header_file_integrity(self): def _check_header_file_integrity(self):
# TODO: temporary workaround for usability. move to txlbryum and check headers instead of file integrity # TODO: temporary workaround for usability. move to txlbryum and check headers instead of file integrity
@ -297,7 +296,7 @@ class HeadersComponent(Component):
if os.path.exists(self.old_file): if os.path.exists(self.old_file):
log.warning("Moving old headers from %s to %s.", self.old_file, self.headers_file) log.warning("Moving old headers from %s to %s.", self.old_file, self.headers_file)
os.rename(self.old_file, self.headers_file) os.rename(self.old_file, self.headers_file)
self._downloading_headers = yield self.should_download_headers_from_s3() self._downloading_headers = yield f2d(self.should_download_headers_from_s3())
if self._downloading_headers: if self._downloading_headers:
try: try:
yield self.fetch_headers_from_s3() yield self.fetch_headers_from_s3()
@ -315,7 +314,7 @@ def d2f(deferred):
def f2d(future): def f2d(future):
return defer.Deferred(future) return defer.Deferred(asyncio.ensure_future(future))
class WalletComponent(Component): class WalletComponent(Component):

View file

@ -1,5 +1,3 @@
from twisted.internet import asyncioreactor
asyncioreactor.install()
import json import json
import asyncio import asyncio
import tempfile import tempfile