Offload blocking file conversion to threadpool (issue #5, Approach 1)#6
Merged
Merged
Conversation
…king, issue #5) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018bkodLeWwRcswfSZNFo6Q3
Owner
Author
|
Full hypocrisy alert, this PR was created by Claude after reviewing (and rightfully discarding) the suggestions made in issue #5 . Garbage as that text was, Approach 1 made sense and was a simple enough change. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The
/process_fileendpoint isasync def, butconvert_to_md()runs the synchronousMarkItDown().convert()call directly, blocking uvicorn's event loop for the entire conversion. While one file converts, the server cannot accept or respond to any other request.This implements Approach 1 from #5: wrap the blocking call with FastAPI's
run_in_threadpool, so the event loop stays free while the conversion runs on a worker thread.Changes
from fastapi.concurrency import run_in_threadpoolimport.convert_to_md(temp_file_path)withawait run_in_threadpool(convert_to_md, temp_file_path)inprocess_file.Two-line diff; no changes to
convert_to_md, error handling, or temp-file cleanup. Exceptions raised inside the threadpool still propagate through theawaitand are caught by the existingexceptblock, and each call constructs its ownMarkItDowninstance, so there is no shared state across worker threads.Verification
python -c "import app").uvicorn app:appon port 8490, POSTed a text file to/process_file, received200 OKwith the expected{"markdown": ...}payload; temp file cleanup logged as before.finally, and thread safety — no blocking findings.Notes
Approach 2 from the issue (AsyncOpenAI/
_ocr_pdfrewrite) was intentionally not implemented — it references code that does not exist in this repository. For raw conversion throughput under heavy parallel load, the complementary lever is running multiple uvicorn workers; this change keeps each worker responsive.Closes #5
🤖 Generated with Claude Code
https://claude.ai/code/session_018bkodLeWwRcswfSZNFo6Q3
Generated by Claude Code