diff options
author | Devaev Maxim <[email protected]> | 2019-06-05 20:56:46 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2019-06-05 20:56:46 +0300 |
commit | 6d7996924f98bc33e6492e0fa9e36c321d8fb969 (patch) | |
tree | 9d62b268eac36a41d543e955841d71d302836fef /kvmd/aiotools.py | |
parent | 8aa333ba895e7b498e867f7b430335852a4a9f84 (diff) |
wait short tasks
Diffstat (limited to 'kvmd/aiotools.py')
-rw-r--r-- | kvmd/aiotools.py | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/kvmd/aiotools.py b/kvmd/aiotools.py index d0c12537..b164e110 100644 --- a/kvmd/aiotools.py +++ b/kvmd/aiotools.py @@ -26,6 +26,7 @@ import functools import typing from typing import Callable +from typing import Coroutine from typing import TypeVar from typing import Any @@ -41,8 +42,25 @@ def atomic(method: _AtomicF) -> _AtomicF: return typing.cast(_AtomicF, wrapper) -def task(method: Callable[..., Any]) -> Callable[..., asyncio.Task]: +def tasked(method: Callable[..., Any]) -> Callable[..., asyncio.Task]: @functools.wraps(method) async def wrapper(*args: object, **kwargs: object) -> asyncio.Task: - return asyncio.create_task(method(*args, **kwargs)) + return create_short_task(method(*args, **kwargs)) return typing.cast(Callable[..., asyncio.Task], wrapper) + + +_ATTR_SHORT_TASK = "_aiotools_short_task" + + +def create_short_task(coro: Coroutine) -> asyncio.Task: + task = asyncio.create_task(coro) + setattr(task, _ATTR_SHORT_TASK, True) + return task + + +async def gather_short_tasks() -> None: + await asyncio.gather(*[ + task + for task in asyncio.Task.all_tasks() + if getattr(task, _ATTR_SHORT_TASK, False) + ]) |