-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathutils.py
39 lines (31 loc) · 1.12 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import asyncio
from contextlib import asynccontextmanager
from datetime import timedelta
from functools import lru_cache
from typing import AsyncGenerator, Coroutine
from .models import ServiceConfig
@lru_cache
def get_config() -> ServiceConfig: # pragma: no cover
"""
Cached service configuration, loaded from env.
Values are loaded when the model object is created.
The same object is returned from the cache every time.
Because values are loaded from the process env,
this function can be called at any time.
"""
return ServiceConfig()
@asynccontextmanager
async def task_context(coro: Coroutine,
cancel_timeout=timedelta(seconds=5)
) -> AsyncGenerator[asyncio.Task, None]:
"""
Wraps provided coroutine in an async task.
At the end of the context, the task is cancelled and awaited.
This makes it easy to start background tasks in `lifespan()` context managers.
"""
task = asyncio.create_task(coro)
try:
yield task
finally:
task.cancel()
await asyncio.wait([task], timeout=cancel_timeout.total_seconds())