From 0dbcc56a0809334f514f6071562faae666ff79a3 Mon Sep 17 00:00:00 2001 From: Tim Daniel Metzler Date: Mon, 7 Oct 2024 15:49:44 +0200 Subject: [PATCH] Make list pools async --- e2xauthoring/managers/taskpoolmanager.py | 42 +++++++++++++++--------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/e2xauthoring/managers/taskpoolmanager.py b/e2xauthoring/managers/taskpoolmanager.py index 341e30e..a34d6a4 100644 --- a/e2xauthoring/managers/taskpoolmanager.py +++ b/e2xauthoring/managers/taskpoolmanager.py @@ -1,3 +1,4 @@ +import asyncio import os import shutil @@ -13,14 +14,16 @@ class TaskPoolManager(BaseManager): "pools", help="The relative directory where the pools are stored" ) - def __get_n_tasks(self, name) -> int: - return len( - [ - d - for d in os.listdir(os.path.join(self.base_path, name)) - if not d.startswith(".") - ] - ) + async def __get_n_tasks(self, name) -> int: + base_path = os.path.join(self.base_path, name) + + # Offload os.listdir to a thread + directory_list = await asyncio.to_thread(os.listdir, base_path) + + # Filter out directories that start with a dot ('.') + task_count = len([d for d in directory_list if not d.startswith(".")]) + + return task_count def turn_into_repository(self, pool): path = os.path.join(self.base_path, pool) @@ -54,15 +57,22 @@ def remove(self, name): assert os.path.exists(path), f"The task pool {name} does not exist" shutil.rmtree(path) - def list(self): + async def list(self): if not os.path.exists(self.base_path): self.log.warning("The pool directory does not exist.") os.makedirs(self.base_path, exist_ok=True) - return [ - TaskPool( - name=pool_dir, - n_tasks=self.__get_n_tasks(pool_dir), - is_repo=is_version_controlled(os.path.join(self.base_path, pool_dir)), + pool_dirs = await asyncio.to_thread(self.listdir, self.base_path) + tasks = [] + for pool_dir in pool_dirs: + n_tasks = await self.__get_n_tasks(pool_dir) + is_repo = await asyncio.to_thread( + is_version_controlled, os.path.join(self.base_path, pool_dir) + ) + tasks.append( + TaskPool( + name=pool_dir, + n_tasks=n_tasks, + is_repo=is_repo, + ) ) - for pool_dir in self.listdir(self.base_path) - ] + return tasks