-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Fix/#23 timeout handling
- Loading branch information
Showing
4 changed files
with
72 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
from .async_helper import get_all_tasks, wait_tasks_by_name | ||
from .call import call_any_function | ||
from .logger import get_logger | ||
|
||
__all__ = [ | ||
"get_all_tasks", | ||
"wait_tasks_by_name", | ||
"call_any_function", | ||
"get_logger", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import asyncio | ||
from typing import Any | ||
|
||
|
||
def get_all_tasks(*, loop: asyncio.AbstractEventLoop | None = None, only_undone: bool = False) -> set[asyncio.Task]: | ||
""" | ||
Return a set of all tasks running on the given event loop. | ||
Args: | ||
loop `(asyncio.AbstractEventLoop | None)`: The event loop to get tasks from. | ||
If None, the current running loop is used. | ||
only_undone `(bool)`: If True, only tasks that are not done are returned. | ||
Returns | ||
------- | ||
`set[asyncio.Task]`: A set of all tasks running on the given event loop. | ||
""" | ||
_loop: asyncio.AbstractEventLoop | None = loop or safe_get_running_loop() | ||
|
||
if _loop is None: | ||
return set() | ||
|
||
tasks = asyncio.all_tasks(loop=_loop) | ||
|
||
if only_undone: | ||
return {t for t in tasks if not t.done()} | ||
|
||
return tasks | ||
|
||
|
||
async def wait_tasks_by_name(names: list[str]) -> list[asyncio.Future[Any]]: | ||
""" | ||
Wait for all tasks with the given names to complete. | ||
Args: | ||
names `(list[str])`: A list of task names to wait for. | ||
Returns | ||
------- | ||
`list[asyncio.Future[Any]]`: A list of futures representing the results of the completed tasks. | ||
""" | ||
tasks = [t for t in get_all_tasks() if t.get_name() in names] | ||
return await asyncio.gather(*tasks, return_exceptions=True) | ||
|
||
|
||
def safe_get_running_loop() -> asyncio.AbstractEventLoop | None: | ||
""" | ||
Get the running loop safely. | ||
Returns | ||
------- | ||
`asyncio.AbstractEventLoop | None` | ||
The running loop. None if faced RuntimeError. | ||
""" | ||
try: | ||
return asyncio.get_running_loop() | ||
except RuntimeError: | ||
return None |