Skip to content

Commit

Permalink
Make list pools async
Browse files Browse the repository at this point in the history
  • Loading branch information
tmetzl committed Oct 7, 2024
1 parent 4b088d8 commit 0dbcc56
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions e2xauthoring/managers/taskpoolmanager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import os
import shutil

Expand All @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit 0dbcc56

Please sign in to comment.