⚡ Bolt: Offload blocking API calls to worker threads#359
Conversation
Moved blocking template generation, PDF compilation, and file I/O operations inside FastAPI endpoints to worker threads using `anyio.to_thread.run_sync`. 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 several blocking PDF generation, AI cover letter generation, and JSON→YAML/PDF rendering operations in FastAPI endpoints onto worker threads using anyio.to_thread.run_sync, so the async event loop remains responsive while preserving existing behavior and error handling semantics. Sequence diagram for offloading blocking PDF generation to worker threadssequenceDiagram
actor Client
participant FastAPI as render_pdf
participant AnyIO as anyio.to_thread.run_sync
participant Worker as WorkerThread
participant Generator as TemplateGenerator
participant FS as FileSystem
Client->>FastAPI: HTTP POST /render_pdf
FastAPI->>AnyIO: to_thread.run_sync(_generate_pdf)
AnyIO->>Worker: execute _generate_pdf
Worker->>Generator: generate(variant, output_format, output_path)
Generator->>FS: write output.pdf
Worker->>FS: output_pdf.exists()
Worker->>FS: output_pdf.read_bytes()
Worker-->>AnyIO: content
AnyIO-->>FastAPI: content
FastAPI-->>Client: HTTP 200 PDF bytes
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:
- Consider extracting the repeated
anyio.to_thread.run_syncpatterns into shared helper utilities (e.g., a genericrun_blocking_pdf_generation/run_blocking_iofunction) instead of defining small nested functions in each route, to reduce duplication and make the threading behavior easier to reason about and maintain. - The
base64import inside_compile_and_readwill execute on every call; moving it to module scope would avoid the repeated import and make the helper function purely about compilation and I/O.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider extracting the repeated `anyio.to_thread.run_sync` patterns into shared helper utilities (e.g., a generic `run_blocking_pdf_generation`/`run_blocking_io` function) instead of defining small nested functions in each route, to reduce duplication and make the threading behavior easier to reason about and maintain.
- The `base64` import inside `_compile_and_read` will execute on every call; moving it to module scope would avoid the repeated import and make the helper function purely about compilation and I/O.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
💡 What: Offloaded blocking operations (like PDF compilation, template generation, and AI generation) in FastAPI endpoints (
render_pdf,generate_cover_letter,render_resume_pdf) to worker threads usinganyio.to_thread.run_sync.🎯 Why: In FastAPI (which is async), performing heavy CPU-bound or blocking I/O tasks directly inside an
async defroute handler blocks the single thread running the ASGI event loop. This leads to severe concurrent request starvation, where other lightweight requests (like/health) are blocked until the heavy operation finishes.📊 Impact: Significantly improves the API's concurrency. The main event loop is no longer blocked by slow operations (like LaTeX compilation), allowing the application to seamlessly handle background or interleaved requests while generating PDFs.
🔬 Measurement: I ran a time test against the API. Before the change, sending requests to the API while a heavy blocking operation was running would timeout or stall. After this optimization, endpoints remain highly responsive to concurrent requests even under heavy load.
PR created automatically by Jules for task 8138516599051557082 started by @anchapin
Summary by Sourcery
Offload blocking resume and cover letter PDF and content generation work from FastAPI route handlers to worker threads to avoid event loop blocking and improve concurrency.
Bug Fixes:
Enhancements: