forked from RealistikOsu/USSR
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A proper job scheduling lib to handle background tasks (#85)
* Proper job scheduling lib to handle background tasks * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * min diff --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
1585295
commit 6a8c376
Showing
12 changed files
with
102 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import logging | ||
import time | ||
|
||
|
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
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import sys | ||
from typing import Any | ||
from typing import Coroutine | ||
from typing import Generator | ||
from typing import TypeVar | ||
from typing import Union | ||
|
||
T = TypeVar("T") | ||
|
||
ACTIVE_TASKS: set[asyncio.Task[Any]] = set() | ||
|
||
|
||
def schedule_job( | ||
coro: Union[ | ||
Generator[Any, None, T], | ||
Coroutine[Any, Any, T], | ||
], | ||
) -> asyncio.Task[T]: | ||
"""\ | ||
Run a coroutine to run in the background. | ||
This function is a wrapper around `asyncio.create_task` that adds the task | ||
to a set of active tasks. This set is used to provide handling of any | ||
exceptions that occur as well as to wait for all tasks to complete before | ||
shutting down the application. | ||
""" | ||
task = asyncio.create_task(coro) | ||
task.add_done_callback(_handle_task_exception) | ||
_register_task(task) | ||
return task | ||
|
||
|
||
def _register_task(task: asyncio.Task[Any]) -> None: | ||
ACTIVE_TASKS.add(task) | ||
|
||
|
||
def _unregister_task(task: asyncio.Task[Any]) -> None: | ||
ACTIVE_TASKS.remove(task) | ||
|
||
|
||
def _handle_task_exception(task: asyncio.Task[Any]) -> None: | ||
_unregister_task(task) | ||
|
||
if task.cancelled(): | ||
return None | ||
|
||
try: | ||
exception = task.exception() | ||
except asyncio.InvalidStateError: | ||
pass | ||
else: | ||
if exception is not None: | ||
sys.excepthook( | ||
type(exception), | ||
exception, | ||
exception.__traceback__, | ||
) | ||
|
||
|
||
async def await_running_jobs( | ||
timeout: float, | ||
) -> tuple[set[asyncio.Task[Any]], set[asyncio.Task[Any]]]: | ||
"""\ | ||
Await all tasks to complete, or until the timeout is reached. | ||
Returns a tuple of done and pending tasks. | ||
""" | ||
if not ACTIVE_TASKS: | ||
return set(), set() | ||
|
||
done, pending = await asyncio.wait( | ||
ACTIVE_TASKS, | ||
timeout=timeout, | ||
return_when=asyncio.ALL_COMPLETED, | ||
) | ||
return done, pending |
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
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