From 75e29238d82c330c05dbc904ce6ad1c9b59dda92 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Wed, 14 Aug 2024 00:26:47 +0100 Subject: [PATCH 1/2] Add cleanup_ctx example for sharing resources across subapps --- docs/faq.rst | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/docs/faq.rst b/docs/faq.rst index c2c081a33be..ce206892f39 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -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 contexts`:: + + 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? From 2ba298542daa3bc7e8ac2ce21f49d06d17c8215e Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Wed, 14 Aug 2024 12:52:08 +0100 Subject: [PATCH 2/2] Update faq.rst --- docs/faq.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/faq.rst b/docs/faq.rst index ce206892f39..f5e8b9afb15 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -281,7 +281,7 @@ database object, do it explicitly:: subapp[db_key] = mainapp[db_key] mainapp.add_subapp("/prefix", subapp) -This can also be done from a :ref:`cleanup contexts`:: +This can also be done from a :ref:`cleanup context`:: async def db_context(app: web.Application) -> AsyncIterator[None]: async with create_db() as db: