Skip to content

Add render_async() and save_async() - #200

Merged
cvas22 merged 1 commit into
mainfrom
feat/143-async-render-save
Nov 23, 2026
Merged

cvas22 merged 1 commit into
mainfrom
feat/143-async-render-save

Conversation

@cvas22

@cvas22 cvas22 commented Nov 23, 2026

Copy link
Copy Markdown
Owner

What this changes

Calling render() from an async FastAPI endpoint blocks the event loop, so people end up making the endpoint sync or wrapping it themselves. render_async() and save_async() take the same arguments and run the work in a thread through loop.run_in_executor(). That works on 3.7, unlike asyncio.to_thread. An optional executor lets you cap how many documents are generated at once, which was the worry in the issue about running out of threads.

Rendering is CPU and lxml work, so a real async rewrite wouldn't make it faster. A worker thread is what @waketzheng's anyio example does too, this just makes it a method. One template object still shouldn't be used from two tasks at the same time, the docs say so.

Fixes #143

How I tested it

New unit tests render and save three templates concurrently on a two-thread pool and check that template errors come through from the thread. All unit tests pass locally.

  • Tests added or updated in tests/unit
  • CHANGES.rst updated if users will notice the change (left for the release PR)
  • Docs updated if behavior or the API changed

Run render() and save() in a worker thread so async apps like FastAPI
don't block the event loop. An optional executor caps how many run at
once.

Fixes #143
@github-actions github-actions Bot added documentation Improvements or additions to documentation code Changes to the docform package tests Changes to tests labels Nov 23, 2026
@cvas22
cvas22 merged commit 101a213 into main Nov 23, 2026
11 checks passed
@cvas22
cvas22 deleted the feat/143-async-render-save branch November 23, 2026 11:47
@cvas22 cvas22 mentioned this pull request Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code Changes to the docform package documentation Improvements or additions to documentation tests Changes to tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Asyncio support

1 participant