| 1 |
import asyncio |
| 2 |
from typing import AsyncGenerator |
| 3 |
|
| 4 |
from app.lib import ping_external_resource |
| 5 |
|
| 6 |
from litestar import Litestar, websocket_stream |
| 7 |
|
| 8 |
RESOURCE_LOCK = asyncio.Lock() |
| 9 |
|
| 10 |
|
| 11 |
async def acquire_lock() -> AsyncGenerator[None, None]: |
| 12 |
async with RESOURCE_LOCK: |
| 13 |
yield |
| 14 |
|
| 15 |
|
| 16 |
@websocket_stream("/") |
| 17 |
async def ping(lock: asyncio.Lock) -> AsyncGenerator[float, None]: |
| 18 |
while True: |
| 19 |
alive = await ping_external_resource() |
| 20 |
yield alive |
| 21 |
await asyncio.sleep(1) |
| 22 |
|
| 23 |
|
| 24 |
app = Litestar([ping], dependencies={"lock": acquire_lock}) |