From 9137c13fe863840a83ed5afd8da10a6aa9582bef Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Fri, 5 Feb 2021 15:09:50 +0100 Subject: db/manager: use only one worker thread for write operations There is no point in having more, as the database cannot have more than one writer at any moment. --- alot/db/manager.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/alot/db/manager.py b/alot/db/manager.py index aa67ed40..56f71bd1 100644 --- a/alot/db/manager.py +++ b/alot/db/manager.py @@ -4,6 +4,7 @@ import abc import asyncio +from concurrent.futures import ThreadPoolExecutor from contextlib import closing from functools import partialmethod import logging @@ -232,16 +233,18 @@ class DBManager: async def _db_write_task(self): # this task serialises write operations on the database and # sends them off to a thread so they do not block the event loop - while True: - cur_item = await self._write_queue.get() - if cur_item is None: - self._write_queue.task_done() - break + # one workers, as there can be only one DB writer at any moment + with ThreadPoolExecutor(max_workers = 1) as executor: + while True: + cur_item = await self._write_queue.get() + if cur_item is None: + self._write_queue.task_done() + break - logging.debug('submitting write task: %s', cur_item) + logging.debug('submitting write task: %s', cur_item) - await self._loop.run_in_executor(None, cur_item.apply) - self._write_queue.task_done() + await self._loop.run_in_executor(executor, cur_item.apply) + self._write_queue.task_done() def tags_add(self, query, tags): """ -- cgit v1.2.3