From 6aea9d7044b10044c90921ecfb1d8faef25ad9e8 Mon Sep 17 00:00:00 2001 From: timotheyca Date: Wed, 2 Feb 2022 20:39:31 +0300 Subject: [PATCH] async copy --- ptvp35/__init__.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/ptvp35/__init__.py b/ptvp35/__init__.py index c09dddd..794d13b 100644 --- a/ptvp35/__init__.py +++ b/ptvp35/__init__.py @@ -159,7 +159,7 @@ class Db: async def _reload_if_oversized(self): if self.__file.tell() > 2 * self.__initial_size: - await self.__loop.run_in_executor(None, self._reload) + await self._reload() async def _dump_buffer(self): if self.__buffer.tell(): @@ -188,32 +188,33 @@ class Db: while True: await self._background_cycle() - def _rebuild_file(self, db: {}): + async def _finish_recovery(self): + await self.__loop.run_in_executor(None, shutil.copy, self.__path_backup, self.__path) + self.__path_recover.unlink() + self.__path_backup.unlink() + + async def _rebuild_file(self, db: {}): if self.__path_recover.exists(): - shutil.copy(self.__path_backup, self.__path) - self.__path_recover.unlink() - self.__path_backup.unlink() + await self._finish_recovery() self.__path.touch() with open(self.__path) as file: self.io2db(file, db) with open(self.__path_backup, "w") as file: self.__initial_size = self.db2io(db, file) self.__path_recover.touch() - shutil.copy(self.__path_backup, self.__path) - self.__path_recover.unlink() - self.__path_backup.unlink() + await self._finish_recovery() - def _reload(self): + async def _reload(self): self.__file.close() - self._rebuild_file({}) + await self._rebuild_file({}) self.__file = open(self.__path, "a") - def _load_from_file(self): - self._rebuild_file(self.__mmdb) + async def _load_from_file(self): + await self._rebuild_file(self.__mmdb) async def _initialize_mmdb(self): self.__mmdb = {} - await self.__loop.run_in_executor(None, self._load_from_file) + await self._load_from_file() async def _initialize_queue(self): self.__queue = asyncio.Queue() @@ -240,9 +241,7 @@ class Db: with open(self.__path_backup, "w") as file: self.__initial_size = self.db2io(self.__mmdb, file) self.__path_recover.touch() - shutil.copy(self.__path_backup, self.__path) - self.__path_recover.unlink() - self.__path_backup.unlink() + await self._finish_recovery() def _uninitialize(self): self.__mmdb = None