Skip to content

⚡ Bolt: Offload blocking I/O to threads in FastAPI#367

Open
anchapin wants to merge 1 commit into
mainfrom
bolt-fastapi-async-optimization-2849466650206990477
Open

⚡ Bolt: Offload blocking I/O to threads in FastAPI#367
anchapin wants to merge 1 commit into
mainfrom
bolt-fastapi-async-optimization-2849466650206990477

Conversation

@anchapin

Copy link
Copy Markdown
Owner

💡 What: Replaced direct blocking function calls in api/main.py (like generator.generate, file reads/writes, and anthropic sync client calls) with anyio.to_thread.run_sync(), using functools.partial for kwargs.

🎯 Why: FastAPI runs async def endpoints directly on the main async event loop. Since operations like PDF compilation via LaTeX and reading files are synchronous and heavily blocking, they starve the event loop, effectively reducing the API's concurrency to 1 request at a time.

📊 Impact: Significantly increases throughput under load by freeing up the main event loop to handle concurrent API requests while worker threads process blocking jobs.

🔬 Measurement: Verify by running concurrent requests against the API endpoints; the server should now process incoming requests without timing out during heavy PDF generation operations. Confirm via pytest tests/test_api_endpoints.py.


PR created automatically by Jules for task 2849466650206990477 started by @anchapin

Replaces blocking synchronous operations (PDF generation,
file system writes/reads, synchronous API requests) inside
async endpoints with anyio.to_thread.run_sync() to prevent
event loop starvation and improve API concurrency.

Co-authored-by: anchapin <[email protected]>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @anchapin, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant