An async version of :meth:`generate`. Works very similarly but returns an async iterator instead.
(
self, *args: t.Any, **kwargs: t.Any
)
| 1348 | yield self.environment.handle_exception() |
| 1349 | |
| 1350 | async def generate_async( |
| 1351 | self, *args: t.Any, **kwargs: t.Any |
| 1352 | ) -> t.AsyncGenerator[str, object]: |
| 1353 | """An async version of :meth:`generate`. Works very similarly but |
| 1354 | returns an async iterator instead. |
| 1355 | """ |
| 1356 | if not self.environment.is_async: |
| 1357 | raise RuntimeError( |
| 1358 | "The environment was not created with async mode enabled." |
| 1359 | ) |
| 1360 | |
| 1361 | ctx = self.new_context(dict(*args, **kwargs)) |
| 1362 | |
| 1363 | try: |
| 1364 | agen = self.root_render_func(ctx) |
| 1365 | try: |
| 1366 | async for event in agen: # type: ignore |
| 1367 | yield event |
| 1368 | finally: |
| 1369 | # we can't use async with aclosing(...) because that's only |
| 1370 | # in 3.10+ |
| 1371 | await agen.aclose() # type: ignore |
| 1372 | except Exception: |
| 1373 | yield self.environment.handle_exception() |
| 1374 | |
| 1375 | def new_context( |
| 1376 | self, |