Skip to content
Myr

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

1 file+27 added−26 removed

modified:docs/api.rst+27 added-26 removed
527 unmodified lines
528529Async Support530-------------531532Starting with version 2.9, Jinja also supports the Python `async` and533`await` constructs. As far as template designers go this feature is534entirely opaque to them however as a developer you should be aware of how535it's implemented as it influences what type of APIs you can safely expose536to the template environment.532.. versionadded:: 2.9533538First you need to be aware that by default async support is disabled as539enabling it will generate different template code behind the scenes which540passes everything through the asyncio event loop. This is important to541understand because it has some impact to what you are doing:534Jinja supports the Python ``async`` and ``await`` syntax. For the535template designer, this support (when enabled) is entirely transparent,536templates continue to look exactly the same. However, developers should537be aware of the implementation as it affects what types of APIs you can538use.539543* 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.540By default, async support is disabled. Enabling it will cause the541environment to compile different code behind the scenes in order to542handle async and sync code in an asyncio event loop. This has the543following implications:544545- 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.556557Awaitable objects can be returned from functions in templates and any554function call in a template will automatically await the result. This555means that you can provide a method that asynchronously loads data556from a database if you so desire and from the template designer's point of557view this is just another function they can call. This means that the558``await`` you would normally issue in Python is implied. However this559only applies to function calls. If an attribute for instance would be an560awaitable object then this would not result in the expected behavior.558function call in a template will automatically await the result. The559``await`` you would normally add in Python is implied. For example, you560can provide a method that asynchronously loads data from a database, and561from the template designer's point of view it can be called like any562other function.563562Likewise iterations with a `for` loop support async iterators.564565.. _policies:566567Policies
367 unmodified lines