use asyncio.run
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Thomas Grainger · committed by David Lord · parent 2fcabb5 · Browse files · 3 files·+6 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+6 added−19 removed
- other:+6 added−19 removed
Provenance
- Commit
5bc613e- Tree
39381a3- Author
Thomas Grainger <tagrain@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
2fcabb5
3 files changed+6 added−19 removed
modified:docs/api.rst-3 removed
513 unmodified lines514 environment to compile different code behind the scenes in order to515 handle async and sync code in an asyncio event loop. This has the516 following implications:517 518-- Template rendering requires an event loop to be available to the519- current thread. :func:`asyncio.get_running_loop` must return an520- event loop.518 - The compiled code uses ``await`` for functions and attributes, and519 uses ``async for`` loops. In order to support using both async and520 sync functions in this context, a small wrapper is placed around521 all calls and access, which adds overhead compared to purely async398 unmodified linesmodified:src/jinja2/environment.py+1 added-13 removed
1280 unmodified lines1281 """1282 if self.environment.is_async:1283 import asyncio1284 1285- close = False1285+ return asyncio.run(self.render_async(*args, **kwargs))1286 1287- try:1288- loop = asyncio.get_running_loop()1289- except RuntimeError:1290- loop = asyncio.new_event_loop()1291- close = True1292-1293- try:1294- return loop.run_until_complete(self.render_async(*args, **kwargs))1295- finally:1296- if close:1297- loop.close()1298-1287 ctx = self.new_context(dict(*args, **kwargs))1288 1289 try:1290 return self.environment.concat(self.root_render_func(ctx)) # type: ignore373 unmodified linesmodified:CHANGES.rst+5 added-3 removed
3 unmodified lines4 -------------5 6 Unreleased7 8+- Calling sync ``render`` for an async template uses ``asyncio.run``.9+ :pr:`1952`10 11+12 Version 3.1.413 -------------14 15 Released 2024-05-05124 unmodified lines140 - The deprecation warning for unneeded ``autoescape`` and ``with_``141 extensions shows more relevant context. :issue:`1429`142 - Fixed calling deprecated ``jinja2.Markup`` without an argument.143 Use ``markupsafe.Markup`` instead. :issue:`1438`141-- Calling sync ``render`` for an async template uses ``asyncio.run``142- on Python >= 3.7. This fixes a deprecation that Python 3.10143- introduces. :issue:`1443`144+- Calling sync ``render`` for an async template uses ``asyncio.new_event_loop``145+ This fixes a deprecation that Python 3.10 introduces. :issue:`1443`146 147 148 Version 3.0.0149 -------------858 unmodified lines