Skip to content

Commit

Permalink
Add cleanup_ctx example for sharing resources across subapps (#8694)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer authored Aug 14, 2024
1 parent 4f41d05 commit d83804d
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 d83804d

Please sign in to comment.