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

Sharing cleanup_ctx data with sub-apps #3876

Closed
mattrasband opened this issue Jun 28, 2019 · 4 comments · Fixed by #8694
Closed

Sharing cleanup_ctx data with sub-apps #3876

mattrasband opened this issue Jun 28, 2019 · 4 comments · Fixed by #8694

Comments

@mattrasband
Copy link

There is a documented approach of sharing resources from parent apps to nested apps in #2413. However that does not work for the common case of using CleanupContext managed resources (such as a database or redis pool).

Is there a recommended way of sharing resources provided by a root application's context signals?

Originally posted in #2412 (comment)

@mattrasband
Copy link
Author

I ask mostly to know if there is a planned design for that, as there are a few options:

  1. Add the resource to a sub-application in the signal
  2. Create the resources at the app creation phase and have signals manage the cleanup or other lifecycle things (not too fond of this)
  3. Pass the parent app to a nested app (definitely don't like this)

Item 1 seems the most likely to be a good approach, and then nested apps could have their signal check for a resource or create their own to make it more pluggable.

@asvetlov
Copy link
Member

Do you have an idea of how the usage code should look like?

@vladarefiev
Copy link

Faced the same issue. I want to initialise db variable in main app in cleanup_ctx and pass it to sub_apps, but can't find out how to do it properly.

@mattrasband
Copy link
Author

mattrasband commented Aug 23, 2019

I ended up deciding that a parent app knowing about subapps is 100% ok, so for one app we are just explicity setting the properties on a subapp. With testing they are easy enough to set by just testing the nested apps on their own merit.

Here's a sample from ours (option #1 above):

async def postgresql_pool(app: web.Application) -> AsyncGenerator[None, None]:
    async with asyncpg.create_pool(dsn=app["POSTGRESQL_DSN"]) as pool:
        app["db"] = app["website_app"]["db"] = pool
        yield

Where app["website_app"] is our nested application.

There may be merit in just having a last-signal register them all (async def register_nested_dependencies(app: web.Application):), but I wanted to keep the usages close to the declaration/lifecycle managed in the cleanup contexts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants