Skip to content

Commit

Permalink
[PR #8694/d83804db backport][3.10] Add cleanup_ctx example for sharin…
Browse files Browse the repository at this point in the history
…g resources across subapps (#8695)

**This is a backport of PR #8694 as merged into master
(d83804d).**

Co-authored-by: Sam Bull <git@sambull.org>
  • Loading branch information
patchback[bot] and Dreamsorcerer authored Aug 14, 2024
1 parent 91c9c6f commit 7d593ec
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,18 @@ A subapplication is an isolated unit by design. If you need to share a
database object, do it explicitly::

subapp[db_key] = mainapp[db_key]
mainapp.add_subapp('/prefix', subapp)
mainapp.add_subapp("/prefix", subapp)

This can also be done from a :ref:`cleanup context<aiohttp-web-cleanup-ctx>`::

async def db_context(app: web.Application) -> AsyncIterator[None]:
async with create_db() as db:
mainapp[db_key] = mainapp[subapp_key][db_key] = db
yield

mainapp[subapp_key] = subapp
mainapp.add_subapp("/prefix", subapp)
mainapp.cleanup_ctx.append(db_context)


How do I perform operations in a request handler after sending the response?
Expand Down

0 comments on commit 7d593ec

Please sign in to comment.