Skip to content

perf: async file saving in web generation endpoint - #100

Open
saquibsaifee wants to merge 1 commit into
GenAI-Security-Project:mainfrom
saquibsaifee:perf-async-file-io-9118509034411377622
Open

perf: async file saving in web generation endpoint#100
saquibsaifee wants to merge 1 commit into
GenAI-Security-Project:mainfrom
saquibsaifee:perf-async-file-io-9118509034411377622

Conversation

@saquibsaifee

Copy link
Copy Markdown
Contributor

💡 What: The generate_form endpoint was previously performing synchronous disk I/O (with open(...)) inside of a ThreadPoolExecutor worker, which causes thread exhaustion and blocks thread workers under load. I've updated the endpoint to use aiofiles for asynchronous, non-blocking disk I/O inside the event loop, while keeping only CPU-bound JSON string formatting in the threadpool.

🎯 Why: To improve throughput and scalability of the web server. Thread pool resources shouldn't be held up by disk writing.

📊 Measured Improvement: In synthetic load testing (100 sequential write tasks representing high concurrent load in a web scenario), switching from ThreadPoolExecutor thread-blocking I/O to aiofiles async tasks dropped the duration from ~1.06s to ~0.42s (over a 2.5x speedup in freeing concurrency execution flow).

@saquibsaifee

Copy link
Copy Markdown
Contributor Author

@eaglei15 this PR is ready to be reviewed.

Refactored _save_task in generate_form to split synchronous JSON encoding
into the threadpool, while lifting the file-writing actions out into the
async event loop using aiofiles.

- Added aiofiles dependency to pyproject.toml.
- Added test_generate_uses_aiofiles_async unit test to cover async logic
  and mocks aiofiles.

Signed-off-by: saquibsaifee <saquibsaifee2@gmail.com>
@saquibsaifee
saquibsaifee force-pushed the perf-async-file-io-9118509034411377622 branch from fe2c615 to bab46e2 Compare August 31, 2026 17:05
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