docs/examples/websockets/stream_di_hog_fix.py
raw| 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 | @websocket_stream("/") |
| 12 | async def ping() -> AsyncGenerator[float, None]: |
| 13 | while True: |
| 14 | async with RESOURCE_LOCK: |
| 15 | alive = await ping_external_resource() |
| 16 | yield alive |
| 17 | await asyncio.sleep(1) |
| 18 | |
| 19 | |
| 20 | app = Litestar([ping]) |