-
-
Notifications
You must be signed in to change notification settings - Fork 765
Clean improvements #3535
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
base: main
Are you sure you want to change the base?
Clean improvements #3535
Changes from all commits
8c35889
da82db9
a1b8311
8a0e706
0c020ff
54a72d9
80d189d
dd58e2b
f165f19
927b612
2dd4129
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,37 @@ async def wait(self) -> None: | |
| await self._event.wait() | ||
|
|
||
|
|
||
| class AsyncExecutor: | ||
| """Execute coroutines with a pool limit for concurrent execution.""" | ||
|
|
||
| def __init__(self, *, limit: int): | ||
| self._semaphore = asyncio.Semaphore(limit) | ||
| self._running_tasks = set() | ||
|
|
||
| def submit[T](self, coro: Awaitable[T]) -> asyncio.Task[T]: | ||
| """Schedule the coroutine on the event loop, and ensure cleanup.""" | ||
| task = asyncio.create_task(self.execute(coro)) | ||
| self._running_tasks.add(task) | ||
| return task | ||
|
|
||
| async def execute[T](self, coro: Awaitable[T]) -> T: | ||
| """Execute the coroutine once there is an available slot in the pool.""" | ||
| async with self._semaphore: | ||
| return await coro | ||
|
|
||
| async def gather(self, return_exceptions: bool = False) -> list[Any]: | ||
| """Wait for all submitted coroutines to finish execution.""" | ||
| result = await asyncio.gather(*self._running_tasks, return_exceptions=return_exceptions) | ||
| self._running_tasks.clear() | ||
| return result | ||
|
|
||
| def cancel_all(self) -> None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we wait for the tasks to be cancelled or is it fine to fire and forget?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have anything to do if we wait? Tasks that haven't started yet are cleared, and tasks that are already ongoing get cancelled. But if the task e.g. made an API call to Discord, we have no choice but let it play out. Is there something beyond cancelling it that we can realistically do? In the case of the clean cog, I think it's ok even if we let tasks that already started finish. |
||
| """Cancel all running tasks.""" | ||
| for task in self._running_tasks: | ||
| task.cancel() | ||
| self._running_tasks.clear() | ||
|
|
||
|
|
||
| def lock( | ||
| namespace: Hashable, | ||
| resource_id: ResourceId, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be a public interface for cancelling the tasks? Might be needed if the bot is shutting down.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point