Merge pull request #1149 from davidbrochart/fix_doc
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by GitHub · parents dd5a050, 2f61f69 · Browse files · 1 file·+27 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+27 added−26 removed
- other:+27 added−26 removed
Provenance
- Commit
2a70ef2- Tree
15e621e- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
dd5a050- Parent 2
2f61f69
1 file changed+27 added−26 removed
modified:docs/api.rst+27 added-26 removed
527 unmodified lines528 529 Async Support530 -------------531 532-Starting with version 2.9, Jinja also supports the Python `async` and533-`await` constructs. As far as template designers go this feature is534-entirely opaque to them however as a developer you should be aware of how535-it's implemented as it influences what type of APIs you can safely expose536-to the template environment.532+.. versionadded:: 2.9533 538-First you need to be aware that by default async support is disabled as539-enabling it will generate different template code behind the scenes which540-passes everything through the asyncio event loop. This is important to541-understand because it has some impact to what you are doing:534+Jinja supports the Python ``async`` and ``await`` syntax. For the535+template designer, this support (when enabled) is entirely transparent,536+templates continue to look exactly the same. However, developers should537+be aware of the implementation as it affects what types of APIs you can538+use.539 543-* template rendering will require an event loop to be set for the544- current thread (``asyncio.get_event_loop`` needs to return one)545-* all template generation code internally runs async generators which546- means that you will pay a performance penalty even if the non sync547- methods are used!548-* The sync methods are based on async methods if the async mode is549- enabled which means that `render` for instance will internally invoke550- `render_async` and run it as part of the current event loop until the551- execution finished.540+By default, async support is disabled. Enabling it will cause the541+environment to compile different code behind the scenes in order to542+handle async and sync code in an asyncio event loop. This has the543+following implications:544 545+- Template rendering requires an event loop to be available to the546+ current thread. :func:`asyncio.get_event_loop` must return an event547+ loop.548+- The compiled code uses ``await`` for functions and attributes, and549+ uses ``async for`` loops. In order to support using both async and550+ sync functions in this context, a small wrapper is placed around551+ all calls and access, which add overhead compared to purely async552+ code.553+- Sync methods and filters become wrappers around their corresponding554+ async implementations where needed. For example, ``render`` invokes555+ ``async_render``, and ``|map`` supports async iterables.556+557 Awaitable objects can be returned from functions in templates and any554-function call in a template will automatically await the result. This555-means that you can provide a method that asynchronously loads data556-from a database if you so desire and from the template designer's point of557-view this is just another function they can call. This means that the558-``await`` you would normally issue in Python is implied. However this559-only applies to function calls. If an attribute for instance would be an560-awaitable object then this would not result in the expected behavior.558+function call in a template will automatically await the result. The559+``await`` you would normally add in Python is implied. For example, you560+can provide a method that asynchronously loads data from a database, and561+from the template designer's point of view it can be called like any562+other function.563 562-Likewise iterations with a `for` loop support async iterators.564 565 .. _policies:566 567 Policies367 unmodified lines