Skip to content

Allow cancelling TimesPer.acquire #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions nextcore/common/times_per/times_per.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from __future__ import annotations

from asyncio import Future, get_running_loop
from asyncio import CancelledError, Future, get_running_loop
from contextlib import asynccontextmanager
from logging import getLogger
from queue import PriorityQueue
Expand Down Expand Up @@ -97,7 +97,12 @@ async def acquire(self, *, priority: int = 0, wait: bool = True) -> AsyncIterato
self._pending.put_nowait(item)

logger.debug("Added request to queue with priority %s", priority)
await future
try:
await future
except CancelledError:
logger.debug("Cancelled .acquire, removing from queue.")
self._pending.queue.remove(item)
return
logger.debug("Out of queue, doing request")

self._in_progress += 1
Expand Down