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

[PR #8694/d83804db backport][3.10] Add cleanup_ctx example for sharing resources across subapps #8695

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading