Skip to content
Myr

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

1 file+40 added−0 removed

modified:docs/api.rst+40 added
227 unmodified lines
228 .. automethod:: generate([context])229230 .. automethod:: stream([context])231232 .. automethod:: render_async([context])233234 .. automethod:: generate_async([context])235236237.. autoclass:: jinja2.environment.TemplateStream()238 :members: disable_buffering, enable_buffering, dump239240
259 unmodified lines
500501.. autoclass:: jinja2.MemcachedBytecodeCache502503504Async Support505-------------506507Starting with version 2.9, Jinja2 also supports the Python `async` and508`await` constructs. As far as template designers go this feature is509entirely opaque to them however as a developer you should be aware of how510it's implemented as it influences what type of APIs you can safely expose511to the template environment.512513First you need to be aware that by default async support is disabled as514enabling it will generate different template code behind the scenes which515passes everything through the asyncio event loop. This is important to516understand because it has some impact to what you are doing:517518* 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.527528Awaitable objects can be returned from functions in templates and any529function call in a template will automatically await the result. This530means that you can let provide a method that asynchronously loads data531from a database if you so desire and from the template designer's point of532view this is just another function they can call. This means that the533``await`` you would normally issue in Python is implied. However this534only applies to function calls. If an attribute for instance would be an535avaitable object then this would not result in the expected behavior.536537Likewise iterations with a `for` loop support async iterators.538539540Utilities541---------542543These helper functions and classes are useful if you add custom filters or
305 unmodified lines