Add async template loading APIs and async loader/bytecode-cache hooks #2129
+711
−6
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
Environment:get_template_async,select_template_async,get_or_select_template_asynclist_templates_async,compile_templates_asyncBaseLoader.*_async) with sync fallbacks by default.BytecodeCache.load_bytecode_async/dump_bytecode_async) and ensures async bucket operations await these hooks.Motivation
Async apps increasingly store templates and caches behind async I/O. Jinja already supports async template execution, but loading and bytecode caching still require sync I/O. This change enables fully async template loading/caching without forcing thread offloading.
Design notes
BaseLoader.get_source_asyncandlist_templates_asyncdefault to calling the sync versions.BytecodeCache.load_bytecode_asyncanddump_bytecode_asyncdefault to calling the sync versions.BytecodeCache.get_bucket_async/set_bucket_asyncawait the async hooks, so async-only caches work as expected.Tests
Added tests that fail without the change:
Environment.get_template_asyncworks with sync loaders.AsyncEnvironmentcalls async loader hooks.compile_templates_asyncproduces loadable module templates.BytecodeCachehooks are awaited (sync hooks must not be called).Docs
docs/api.rstto include the new async APIs and examples.Changelog
CHANGES.rstdescribing async loading/caching APIs.Links
fixes #2128
Reference links