Skip to content
Myr

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

3 files+6 added−19 removed

modified:docs/api.rst-3 removed
513 unmodified lines
514environment to compile different code behind the scenes in order to515handle async and sync code in an asyncio event loop. This has the516following implications:517518- 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 async
398 unmodified lines
modified:src/jinja2/environment.py+1 added-13 removed
1280 unmodified lines
1281 """1282 if self.environment.is_async:1283 import asyncio12841285 close = False1285 return asyncio.run(self.render_async(*args, **kwargs))12861287 try:1288 loop = asyncio.get_running_loop()1289 except RuntimeError:1290 loop = asyncio.new_event_loop()1291 close = True12921293 try:1294 return loop.run_until_complete(self.render_async(*args, **kwargs))1295 finally:1296 if close:1297 loop.close()12981287 ctx = self.new_context(dict(*args, **kwargs))12881289 try:1290 return self.environment.concat(self.root_render_func(ctx)) # type: ignore
373 unmodified lines
modified:CHANGES.rst+5 added-3 removed
3 unmodified lines
4-------------56Unreleased78- Calling sync ``render`` for an async template uses ``asyncio.run``.9 :pr:`1952`101112Version 3.1.413-------------1415Released 2024-05-05
124 unmodified lines
140- 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`146147148Version 3.0.0149-------------
858 unmodified lines