⚡ Bolt: Offload blocking I/O to anyio in FastAPI routes#213
⚡ Bolt: Offload blocking I/O to anyio in FastAPI routes#213
Conversation
Wraps blocking operations (PDF generation, file reads/writes, existence checks) in FastAPI async routes using anyio.to_thread.run_sync to prevent event loop starvation and improve concurrency performance. Co-authored-by: anchapin <6326294+anchapin@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Reviewer's GuideOffloads blocking PDF generation and file I/O in FastAPI routes to worker threads using anyio, and documents this performance pattern in the Bolt playbook. Sequence diagram for offloading blocking PDF rendering in FastAPI routesequenceDiagram
actor Client
participant FastAPIRoute as FastAPIRoute_render_pdf
participant Anyio as AnyioToThread
participant Generator as TemplateGenerator
participant FS as FileSystem
participant PdfEngine as PdfCompilation
Client->>FastAPIRoute: HTTP POST /v1/render/pdf
FastAPIRoute->>FastAPIRoute: Create temp_path and output_pdf
FastAPIRoute->>Anyio: run_sync(generator.generate, variant, pdf, output_pdf)
Anyio->>Generator: generate(variant, output_format, output_path)
Generator->>PdfEngine: invoke pdflatex
PdfEngine-->>Generator: write PDF to output_pdf
Generator-->>Anyio: return
Anyio-->>FastAPIRoute: await completion
FastAPIRoute->>Anyio: run_sync(output_pdf.exists)
Anyio->>FS: exists(output_pdf)
FS-->>Anyio: bool
Anyio-->>FastAPIRoute: exists_result
FastAPIRoute->>FastAPIRoute: Raise HTTP 500 if exists_result is False
FastAPIRoute->>Anyio: run_sync(output_pdf.read_bytes)
Anyio->>FS: read_bytes(output_pdf)
FS-->>Anyio: pdf_bytes
Anyio-->>FastAPIRoute: pdf_bytes
FastAPIRoute-->>Client: HTTP 200 Response(pdf_bytes, application/pdf)
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- You now have several repeated
anyio.to_thread.run_sync(functools.partial(...))call sites; consider extracting a small helper (e.g.run_in_thread(fn, *args, **kwargs)) to reduce duplication and make future changes to threading behavior centralized. - For the quick
Path.existsandPath.read_byteschecks, you might consider grouping related filesystem work into a single threaded function (e.g. generate-and-read) to avoid multiple thread hops per request and keep the I/O pattern easier to reason about.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- You now have several repeated `anyio.to_thread.run_sync(functools.partial(...))` call sites; consider extracting a small helper (e.g. `run_in_thread(fn, *args, **kwargs)`) to reduce duplication and make future changes to threading behavior centralized.
- For the quick `Path.exists` and `Path.read_bytes` checks, you might consider grouping related filesystem work into a single threaded function (e.g. generate-and-read) to avoid multiple thread hops per request and keep the I/O pattern easier to reason about.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
💡 What: Wrapped blocking synchronous operations (
generator.generate,Path.read_bytes,Path.exists,generator._compile_pdf, etc.) in theapi/main.pyFastAPI routes withanyio.to_thread.run_sync(andfunctools.partialwhen arguments were needed).🎯 Why: FastAPI's
async defroutes run on the main event loop. Any blocking synchronous call in these routes (like runningpdflatexor reading files) stalls the entire application, making it unable to process other incoming concurrent requests. This causes significant performance degradation under load.📊 Impact: Considerably improves concurrent performance by offloading CPU-bound or blocking I/O tasks to worker threads, freeing up the FastAPI event loop to handle hundreds of concurrent requests simultaneously.
🔬 Measurement: Run load tests using
locustorwrkagainst the/v1/render/pdfor/v1/cover-letterAPI endpoints to observe improved throughput and decreased P99 latencies under concurrent load.PR created automatically by Jules for task 15037805867948622709 started by @anchapin
Summary by Sourcery
Offload blocking PDF generation and file I/O in FastAPI resume and cover-letter endpoints to background threads using anyio to prevent blocking the event loop and improve concurrency.
Enhancements: