Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no way to explicitly shutdown async generators #1953

Open
graingert opened this issue Apr 7, 2024 · 1 comment
Open

no way to explicitly shutdown async generators #1953

graingert opened this issue Apr 7, 2024 · 1 comment

Comments

@graingert
Copy link

graingert commented Apr 7, 2024

given:

import sys
import trio

from jinja2 import Template, select_autoescape


class MyModel:
    class objects:
        @staticmethod
        async def all():
            while True:
                yield "hello"


template = """
<html>
  <head></head>
  <body>
    {% for m in model.objects.all() %}
        {{ m }}
        {% break %}
    {% endfor %}
  </body>
</html>
"""


async def amain():
    return await Template(
        source=template, enable_async=True, extensions=["jinja2.ext.loopcontrols"]
    ).render_async(model=MyModel)


def main():
    trio.run(amain)


if __name__ == "__main__":
    sys.exit(main())

this results in:

 python -Wall demo_jinja_asyncgens.py
<template>:19: ResourceWarning: Async generator 'jinja2.async_utils.auto_aiter' was garbage collected before it had been exhausted. Surround its use in 'async with aclosing(...):' to ensure that it gets cleaned up as soon as you're done using it.
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/home/graingert/.virtualenvs/demo-jinja-asyncgens/lib/python3.12/site-packages/trio/_core/_asyncgens.py:204: ResourceWarning: Async generator '__main__.MyModel.objects.all' was garbage collected before it had been exhausted. Surround its use in 'async with aclosing(...):' to ensure that it gets cleaned up as soon as you're done using it.
  await agen.aclose()
ResourceWarning: Enable tracemalloc to get the object allocation traceback

but there's no way to wrap generators with async with aclosing(...): in jinja2

Environment:

  • Python version: 3.12.2
  • Jinja version: 3.1.3
@graingert
Copy link
Author

graingert commented Apr 8, 2024

I'd like to see a cmgr/closing block so I can do:

<html>
  <head></head>
  <body>
    {% closing model.objects.all() as agen %}
        {% for m in agen %}
            {{ m }}
            {% break %}
        {% endfor %}
    {% endclosing %}
  </body>
</html>

but with all the changes in #1960 it's possible pass in an aclosing function to context:

import sys
import trio
import contextlib

from jinja2 import Template, select_autoescape


class MyModel:
    class objects:
        @staticmethod
        async def all():
            while True:
                yield "hello"


template = """
<html>
  <head></head>
  <body>
    {% for m in aclosing(model.objects.all()) %}
        {{ m }}
        {% break %}
    {% endfor %}
  </body>
</html>
"""


async def amain():
    async with contextlib.AsyncExitStack() as stack:

        def aclosing(agen):
            stack.push_async_callback(agen.aclose)
            return agen

        return await Template(
            source=template, enable_async=True, extensions=["jinja2.ext.loopcontrols"]
        ).render_async(model=MyModel, aclosing=aclosing)


def main():
    trio.run(amain)


if __name__ == "__main__":
    sys.exit(main())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant