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
|
||||
import aiofiles
|
||||
from fastapi import APIRouter
|
||||
from functools import lru_cache
|
||||
import json
|
||||
import asyncio
|
||||
|
||||
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')
|
||||
async def get_tariffs(request: Request):
|
||||
async with aiofiles.open('data/tariffs.json', 'r', encoding='utf-8') as file:
|
||||
return await file.read()
|
||||
async def get_tariffs():
|
||||
return await asyncio.to_thread(get_tariffs_sync)
|
||||
Reference in New Issue
Block a user