reduce run_in_executor calls

This commit is contained in:
AF 2022-11-04 07:53:11 +00:00
parent 89cbb24386
commit 90e7cd39c6

View File

@ -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()