diff --git a/loq.toml b/loq.toml index 338fa617..1fa43723 100644 --- a/loq.toml +++ b/loq.toml @@ -12,7 +12,7 @@ max_lines = 750 # Source files that still need exceptions above 750 [[rules]] path = "python/src/docket/worker.py" -max_lines = 1479 +max_lines = 1482 [[rules]] path = "python/src/docket/cli/__init__.py" diff --git a/python/src/docket/worker.py b/python/src/docket/worker.py index 4a90f0a0..f69b0813 100644 --- a/python/src/docket/worker.py +++ b/python/src/docket/worker.py @@ -839,8 +839,11 @@ async def process_completed_tasks() -> None: await process_completed_tasks() available_slots = self.concurrency - len(active_tasks) if available_slots <= 0: - await asyncio.sleep( - self.minimum_check_interval.total_seconds() + # Wake as soon as a task frees a slot. + await asyncio.wait( + active_tasks, + timeout=self.minimum_check_interval.total_seconds(), + return_when=asyncio.FIRST_COMPLETED, ) continue sources = [get_new_deliveries] diff --git a/python/tests/concurrency_limits/test_waiter_safeguard.py b/python/tests/concurrency_limits/test_waiter_safeguard.py index b65ad957..621eba13 100644 --- a/python/tests/concurrency_limits/test_waiter_safeguard.py +++ b/python/tests/concurrency_limits/test_waiter_safeguard.py @@ -37,9 +37,12 @@ async def the_task( finished.append(name) async def is_woken(task_key: str) -> bool: + # A parked waiter's state is "scheduled", and the wake sets "queued". + # The worker can claim and finish the woken task before this probe + # runs, so any state past "scheduled" means the wake happened. async with docket.redis() as redis: state = await redis.hget(docket.runs_key(task_key), "state") - return state == b"queued" + return state is not None and state != b"scheduled" add = docket.add