-
-
Notifications
You must be signed in to change notification settings - Fork 758
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 7 commits
8c35889
da82db9
a1b8311
8a0e706
0c020ff
54a72d9
80d189d
dd58e2b
f165f19
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,34 @@ 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]: | ||
| """Wraps the coroutine with _semaphore logic, schedules it on the event loop, and ensures cleanup.""" | ||
|
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. Nit: Use imperative mood for docs "Wrap the coroutine" rather than "Wraps the coroutine". Also,
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. hah the underscore is because of a refactor, but yeah don't have to mention how it's implemented. |
||
| task = asyncio.create_task(self.execute(coro)) | ||
| self._running_tasks.add(task) | ||
| return task | ||
|
|
||
| async def execute[T](self, coro: Awaitable[T]) -> T: | ||
| """Executes the coroutine once there is an available slot in the _semaphore.""" | ||
| async with self._semaphore: | ||
| return await coro | ||
|
|
||
| async def gather(self, return_exceptions: bool = False) -> list[Any]: | ||
| """Waits for all submitted coroutines to finish execution.""" | ||
| if self._running_tasks: | ||
|
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. Nit: I think this check is redundant
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. oh I thought an empty asyncio.gather raised an error, I guess not |
||
| result = await asyncio.gather(*self._running_tasks, return_exceptions=return_exceptions) | ||
| self._running_tasks.clear() | ||
| return result | ||
|
|
||
| return [] | ||
|
|
||
|
|
||
| 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