Started doc work for async
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 7265e6f · Browse files · 1 file·+40 added−0 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+40 added−0 removed
- other:+40 added−0 removed
Provenance
- Commit
d8326d9- Tree
699f1a5- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
7265e6f
1 file changed+40 added−0 removed
modified:docs/api.rst+40 added
227 unmodified lines228 .. automethod:: generate([context])229 230 .. automethod:: stream([context])231 232+ .. automethod:: render_async([context])233 234+ .. automethod:: generate_async([context])235+236+237 .. autoclass:: jinja2.environment.TemplateStream()238 :members: disable_buffering, enable_buffering, dump239 240 259 unmodified lines500 501 .. autoclass:: jinja2.MemcachedBytecodeCache502 503 504+Async Support505+-------------506+507+Starting with version 2.9, Jinja2 also supports the Python `async` and508+`await` constructs. As far as template designers go this feature is509+entirely opaque to them however as a developer you should be aware of how510+it's implemented as it influences what type of APIs you can safely expose511+to the template environment.512+513+First you need to be aware that by default async support is disabled as514+enabling it will generate different template code behind the scenes which515+passes everything through the asyncio event loop. This is important to516+understand because it has some impact to what you are doing:517+518+* template rendering will require an event loop to be set for the519+ current thread (``asyncio.get_event_loop`` needs to return one)520+* all template generation code internally runs async generators which521+ means that you will pay a performance penalty even if the non sync522+ methods are used!523+* The sync methods are based on async methods if the async mode is524+ enabled which means that `render` for instance will internally invoke525+ `render_async` and run it as part of the current event loop until the526+ execution finished.527+528+Awaitable objects can be returned from functions in templates and any529+function call in a template will automatically await the result. This530+means that you can let provide a method that asynchronously loads data531+from a database if you so desire and from the template designer's point of532+view this is just another function they can call. This means that the533+``await`` you would normally issue in Python is implied. However this534+only applies to function calls. If an attribute for instance would be an535+avaitable object then this would not result in the expected behavior.536+537+Likewise iterations with a `for` loop support async iterators.538+539+540 Utilities541 ---------542 543 These helper functions and classes are useful if you add custom filters or305 unmodified lines