diff --git a/CHANGES.rst b/CHANGES.rst index eb46957df..b3ddbaa87 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,7 +8,7 @@ Unreleased - Use modern packaging metadata with ``pyproject.toml`` instead of ``setup.cfg``. :pr:`1793` - Use ``flit_core`` instead of ``setuptools`` as build backend. - +- Calling sync ``render`` for an async template uses ``asyncio.run``. :pr:`1952` Version 3.1.3 ------------- @@ -130,9 +130,8 @@ Released 2021-05-18 extensions shows more relevant context. :issue:`1429` - Fixed calling deprecated ``jinja2.Markup`` without an argument. Use ``markupsafe.Markup`` instead. :issue:`1438` -- Calling sync ``render`` for an async template uses ``asyncio.run`` - on Python >= 3.7. This fixes a deprecation that Python 3.10 - introduces. :issue:`1443` +- Calling sync ``render`` for an async template uses ``asyncio.new_event_loop`` + This fixes a deprecation that Python 3.10 introduces. :issue:`1443` Version 3.0.0 diff --git a/docs/api.rst b/docs/api.rst index e2c9bd526..cb62f6c32 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -515,9 +515,6 @@ environment to compile different code behind the scenes in order to handle async and sync code in an asyncio event loop. This has the following implications: -- Template rendering requires an event loop to be available to the - current thread. :func:`asyncio.get_running_loop` must return an - event loop. - The compiled code uses ``await`` for functions and attributes, and uses ``async for`` loops. In order to support using both async and sync functions in this context, a small wrapper is placed around diff --git a/src/jinja2/environment.py b/src/jinja2/environment.py index c9afae311..4dc36d603 100644 --- a/src/jinja2/environment.py +++ b/src/jinja2/environment.py @@ -1279,21 +1279,7 @@ def render(self, *args: t.Any, **kwargs: t.Any) -> str: This will return the rendered template as a string. """ if self.environment.is_async: - import asyncio - - close = False - - try: - loop = asyncio.get_running_loop() - except RuntimeError: - loop = asyncio.new_event_loop() - close = True - - try: - return loop.run_until_complete(self.render_async(*args, **kwargs)) - finally: - if close: - loop.close() + return asyncio.run(self.render_async(*args, **kwargs)) ctx = self.new_context(dict(*args, **kwargs))