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:':
if os.path.exists(self.path): def _readit():
with open(self.path, 'r+b') as header_file: if os.path.exists(self.path):
self.io.seek(0) with open(self.path, 'r+b') as header_file:
self.io.write(header_file.read()) self.io.seek(0)
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:
flags = 'r+b' if os.path.exists(self.path) else 'w+b' def _close():
with open(self.path, flags) as header_file: flags = 'r+b' if os.path.exists(self.path) else 'w+b'
header_file.write(self.io.getbuffer()) with open(self.path, flags) as header_file:
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