add cache to /api/v1/tariffs and i/o-bound operations (sync read from file) which locked async event loop has moved to asyncio thread
This commit is contained in:
+11
-5
@@ -1,9 +1,15 @@
|
|||||||
from fastapi import APIRouter, Request
|
from fastapi import APIRouter
|
||||||
import aiofiles
|
from functools import lru_cache
|
||||||
|
import json
|
||||||
|
import asyncio
|
||||||
|
|
||||||
router = APIRouter(prefix='/api/v1', tags=['tariffs'])
|
router = APIRouter(prefix='/api/v1', tags=['tariffs'])
|
||||||
|
|
||||||
|
@lru_cache(maxsize=1)
|
||||||
|
def get_tariffs_sync():
|
||||||
|
with open('data/tariffs.json', 'r', encoding='utf-8') as f:
|
||||||
|
return json.load(f)
|
||||||
|
|
||||||
@router.get('/tariffs')
|
@router.get('/tariffs')
|
||||||
async def get_tariffs(request: Request):
|
async def get_tariffs():
|
||||||
async with aiofiles.open('data/tariffs.json', 'r', encoding='utf-8') as file:
|
return await asyncio.to_thread(get_tariffs_sync)
|
||||||
return await file.read()
|
|
||||||
Reference in New Issue
Block a user