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

aiohttp server: run_app coroutine? #4419

Closed
JulienPalard opened this issue Dec 4, 2019 · 3 comments · Fixed by #4467
Closed

aiohttp server: run_app coroutine? #4419

JulienPalard opened this issue Dec 4, 2019 · 3 comments · Fixed by #4467
Labels

Comments

@JulienPalard
Copy link
Contributor

Long story short

I think I'd like to do:

async with ClientSession() as session:
    await web.run_app(app)

to reuse an aiohttp client session during the whole life of a server. Or any other asyncornous context manager like a connection to a database and so on.

Expected behaviour

To easily run an aiohttp server inside an asyncronous context manager.

Actual behaviour

I'm actually doing:

    async def on_startup_wrapper(app):
        app["aiohttp_client_session"] = await ClientSession().__aenter__()

    async def on_cleanup_wrapper(app):
        await app["aiohttp_client_session"].__aexit__(None, None, None)

    app.on_startup.append(on_startup_wrapper)
    app.on_cleanup.append(on_cleanup_wrapper)

but I'm not proud of it.

Obviously it can't be done with the actual run_app, and probably easily done with the _run_app, but, _.

Maybe the AppRunner could eat some code from _run_app allowing one to completly bypass _run_app with something like:

runner = AppRunner(app, ...)
await runner.setup(host, path, sock, port)  # Doing most of _run_app current code here
async with Something() as something:
    app["something"] = something
    await runner.run_forever()  # The while True of the current _run_app

Does it looks like a good idea? Should I try a PR? Or am I getting something wrong?

@webknjaz
Copy link
Member

webknjaz commented Dec 5, 2019

Hey @JulienPalard, are you aware of the cleanup context? https://docs.aiohttp.org/en/stable/web_advanced.html#cleanup-context

Would this work for you?

async def init_session_ctx(app):
    async with ClientSession() as session:
        app["aiohttp_client_session"] = session
        try:
            yield
        finally:
            del app["aiohttp_client_session"]

app.cleanup_ctx.append(init_session_ctx)

@webknjaz webknjaz added the question StackOverflow label Dec 5, 2019
@JulienPalard
Copy link
Contributor Author

I missed cleanup_ctx while reading the doc, thanks!

In my current case I'm having two context managers, using cleanup_ctx instead of on_startup and on_cleanup gives me 13 new lines for 18 lines removed, and I'm no longer calling __aenter__ and __aexit__ myself, so it's better, yes.

It probably miss a link somewhere, to this, to make it easier to find from maybe on_startup?

@webknjaz
Copy link
Member

webknjaz commented Dec 5, 2019

Do you imply that the discoverability can be improved? You're welcome to try improving the doc.

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

Successfully merging a pull request may close this issue.

2 participants