From 90e7cd39c6f8716f55fb21bfd2a624c795fd7a4f Mon Sep 17 00:00:00 2001 From: timofey Date: Fri, 4 Nov 2022 07:53:11 +0000 Subject: [PATCH] reduce run_in_executor calls --- ptvp35/__init__.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/ptvp35/__init__.py b/ptvp35/__init__.py index a9a8907..1df2ee4 100644 --- a/ptvp35/__init__.py +++ b/ptvp35/__init__.py @@ -231,28 +231,32 @@ class DbConnection: with open(self.__path, 'w') as file: self.db2io(db, file) - async def _copy(self): - with concurrent.futures.ThreadPoolExecutor() as pool: - await self.__loop.run_in_executor(pool, self._copy_sync) - - async def _finish_recovery(self): - await self._copy() + def _finish_recovery_sync(self): + self._copy_sync() self.__path_recover.unlink() self.__path_backup.unlink() - async def _build_file(self, db: dict): - with open(self.__path_backup, "w") as file, concurrent.futures.ThreadPoolExecutor() as pool: - self.__initial_size = await self.__loop.run_in_executor(pool, self.db2io, db, file) + def _build_file_sync(self, db: dict): + with open(self.__path_backup, "w") as file: + self.__initial_size = self.db2io(db, file) self.__path_recover.touch() - await self._finish_recovery() + self._finish_recovery_sync() - async def _rebuild_file(self, db: dict): + async def _build_file(self, db: dict): + with concurrent.futures.ThreadPoolExecutor() as pool: + await self.__loop.run_in_executor(pool, self._build_file_sync, db) + + def _rebuild_file_sync(self, db: dict): if self.__path_recover.exists(): - await self._finish_recovery() + self._finish_recovery_sync() self.__path.touch() with open(self.__path) as file: - await self.__loop.run_in_executor(None, self.io2db, file, db) - await self._build_file(db) + self.io2db(file, db) + self._build_file_sync(db) + + async def _rebuild_file(self, db: dict): + with concurrent.futures.ThreadPoolExecutor() as pool: + await self.__loop.run_in_executor(pool, self._rebuild_file_sync, db) async def _reload(self): self.__file.close()