-
Notifications
You must be signed in to change notification settings - Fork 0
β‘ Bolt: Offload blocking I/O calls in FastAPI endpoints to worker threads #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anchapin
wants to merge
1
commit into
main
Choose a base branch
from
bolt-fastapi-async-offload-2772379728622250420
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,3 @@ | ||
| ## 2025-02-18 - Lazy Loading Config | ||
| **Learning:** Instantiating `Config` was consuming ~30-70ms due to `import yaml` and file I/O, even for commands that didn't need it (like `init` or `help`). | ||
| **Action:** Implemented lazy loading in `Config` class. `_config` is initialized to `None` and loaded only on first access to properties. This reduces startup time for simple commands and defers the cost for others. | ||
|
|
||
| ## 2025-02-18 - Caching Jinja2 Environment | ||
| **Learning:** Instantiating Jinja2 Environment is surprisingly expensive (35ms vs 0.7ms) even with FileSystemLoader, likely due to filter registration and internal setup. | ||
| **Action:** Cache Environment instances at class level when template directory is constant or keys are manageable. | ||
|
|
||
| ## 2024-05-22 - Lazy Imports for CLI Performance | ||
| **Learning:** Top-level imports of heavy libraries (like Jinja2) in a CLI entry point slow down all commands, even those that don't use the library (like `--help`). | ||
| **Action:** Move heavy imports inside the specific command functions where they are used. | ||
|
|
||
| ## 2025-02-18 - Regex Pre-compilation in Hot Paths | ||
| **Learning:** Re-compiling regexes inside a frequently called function (like `latex_escape` which runs for every string) creates significant overhead. Pre-compiling them at module level yielded a ~3.2x speedup. | ||
| **Action:** Always look for regex compilations inside loops or frequently called functions and move them to module level constants. | ||
| ## 2024-05-18 - Async IO offloading for FastAPI routes | ||
| **Learning:** In FastAPI applications, synchronous blocking operations (such as file I/O or CPU-bound code) placed directly inside `async def` routes can block the entire event loop, starving other requests. | ||
| **Action:** When working with FastAPI and heavy synchronous calls, remember to use `anyio.to_thread.run_sync()` in combination with `functools.partial()` (as `run_sync` only accepts positional arguments for the callable) to properly offload these blocking steps to a worker thread and keep the event loop non-blocking. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (performance):
yaml.dumpitself may be the heaviest part of this operation and is still running on the event loop thread.The file write is now offloaded, but YAML serialization for larger resumes can still block the event loop. If you expect large payloads or high concurrency, consider running
yaml.dumpinsideto_thread.run_syncas well (e.g., perform both dump and write in the same worker-thread function).