load/dump header file using executor

This commit is contained in:
Victor Shyba 2020-04-25 04:24:19 -03:00 committed by Lex Berezhny
parent 7170e69b22
commit 58f77b2a1c

View file

@ -51,10 +51,12 @@ class Headers:
async def open(self): async def open(self):
self.io = BytesIO() self.io = BytesIO()
if self.path != ':memory:': if self.path != ':memory:':
def _readit():
if os.path.exists(self.path): if os.path.exists(self.path):
with open(self.path, 'r+b') as header_file: with open(self.path, 'r+b') as header_file:
self.io.seek(0) self.io.seek(0)
self.io.write(header_file.read()) self.io.write(header_file.read())
await asyncio.get_event_loop().run_in_executor(None, _readit)
bytes_size = self.io.seek(0, os.SEEK_END) bytes_size = self.io.seek(0, os.SEEK_END)
self._size = bytes_size // self.header_size self._size = bytes_size // self.header_size
max_checkpointed_height = max(self.checkpoints.keys() or [-1]) + 1000 max_checkpointed_height = max(self.checkpoints.keys() or [-1]) + 1000
@ -69,9 +71,11 @@ class Headers:
async def close(self): async def close(self):
if self.io is not None: if self.io is not None:
def _close():
flags = 'r+b' if os.path.exists(self.path) else 'w+b' flags = 'r+b' if os.path.exists(self.path) else 'w+b'
with open(self.path, flags) as header_file: with open(self.path, flags) as header_file:
header_file.write(self.io.getbuffer()) header_file.write(self.io.getbuffer())
await asyncio.get_event_loop().run_in_executor(None, _close)
self.io.close() self.io.close()
self.io = None self.io = None