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

How to access db connection stored in app from subapp #2412

Closed
SoumenDass opened this issue Oct 26, 2017 · 4 comments
Closed

How to access db connection stored in app from subapp #2412

SoumenDass opened this issue Oct 26, 2017 · 4 comments

Comments

@SoumenDass
Copy link
Contributor

SoumenDass commented Oct 26, 2017

Long story short

I'm trying to find a way to access the database connection details stored in app (via dict-like interface) from the subapp.

Expected behaviour

There must be some access method like subapp.get_root_app() so that I can then use that to get the database connections stored via subapp.get_root_app()['db']

Actual behaviour

I can't find anything in the documentation that shows a way to do this. So I'm using the following approach:

app = web.Application()
app['db'] = db

# sub application setup
subapp = web.Application()
subapp['parent'] = app

# add subapp to main app
app.add_subapp('/sub/', subapp)

Then in the subapp's request handler, I'm doing request.app['parent']['db'] - it works when I know that the subapp is just one level below main/parent app. But what if there are nested subapps?

Steps to reproduce

As shown above.

Your environment

CentOS / aiohttp 2.3 / Nginx

@asvetlov
Copy link
Member

Answered in #2413

@SoumenDass
Copy link
Contributor Author

thanks

@mattrasband
Copy link

@asvetlov It seems like #2413 doesn't address this in the context of signals - as a lot of our connections are managed via a cleanup_ctx in the root app.

Are there any other recommended ways of sharing given that common usage?

@webknjaz
Copy link
Member

Plz open a new issue, this is going to get lost.

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

No branches or pull requests

5 participants
@asvetlov @SoumenDass @webknjaz @mattrasband and others