Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

## 2024-05-20 - Non-blocking asyncio Inference Calls
**Learning:** Calling blocking synchronous code in an asyncio event loop halts all other concurrent tasks. In FastAPI applications, running ML inference or heavy processing functions sequentially on the main thread severely limits throughput and creates bottlenecks.
**Action:** Always wrap heavy synchronous functions (like `inference.generate_solution`) in `await asyncio.to_thread(...)` to dispatch them to background threads, preserving event loop concurrency for incoming requests.
Binary file added web/__pycache__/app.cpython-312.pyc
Binary file not shown.
10 changes: 7 additions & 3 deletions web/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from fastapi.responses import FileResponse
from pydantic import BaseModel
from typing import Optional
import asyncio
import logging

import inference

Expand Down Expand Up @@ -40,14 +42,16 @@ async def solve(req: SolveRequest):
raise HTTPException(status_code=400, detail="`problem` must be a non-empty string")

try:
solution = inference.generate_solution(
solution = await asyncio.to_thread(
inference.generate_solution,
problem=req.problem,
cot=req.cot,
temperature=req.temperature,
top_p=req.top_p,
max_new_tokens=req.max_new_tokens,
)
Comment on lines +45 to 52
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
except Exception:
logging.exception("Error during inference")
raise HTTPException(status_code=500, detail="Internal server error")
Comment on lines +53 to +55

return SolveResponse(solution=solution)
Loading