-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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 can one create a persistent ClientSession #3658
Comments
GitMate.io thinks the contributor most likely able to help you is @asvetlov. Possibly related issues are #2036 (ClientSession created in aiohttp.request() remains unclosed), #329 (Unittests for ClientSession), #2451 (Documentation for ClientSession), #3072 (Why is creating a ClientSession outside of an event loop "dangerous"?), and #857 (Can one ClientSession be used for multiple simultaneous websocket connections?). |
@cpython-rocks Do this via app.cleanup_ctx.append(persisten_session)
async def persistent_session(app):
app['MY_PERSISTENT_SESSION'] = session = aiohttp.ClientSession()
yield
await session.close()
async def my_request_handler(request) ->
async with request.app['MY_PERSISTEN_SESSION'].get() as resp:
.... alternatively you can use Everything works nice so far and it's been 3 months since we've deployed that. |
Thanks @kornicameister for your suggestion, but the documentation is ambiguous and needs an example on how should one create a persistent session. Just saying X is recommended and Y is a bad idea is not enough unless the doc also tells how to correctly implement X. There are many users that are struggling with this persistent |
I suppose you can drop a PR with this example or I can. Would actually wait for @asvetlov or someone else to confirm if that's the way or just a way. |
the provided example is correct |
@kornicameister @asvetlov I’m a little confused now. I usually use |
P.S. |
The official aiohttp doc is ambiguous, it says that creating a session per request is a very bad idea, but apparently doesn't tell how one can create a persistent session. There are plenty of examples all across the internet on how one can make concurrent requests, but there is unfortunately no example reusing the same session across all requests.
One would argue that the following code reuses the session which is partially true. Partially because every time I call main(), a new session will be created.
The following example in doc is quite confusing:
Here's what I call a persistent session in its true sense because no matter where you call fetch from or how many times you call it, the same session will be reused, but this apparently doesn't work
This code doesn't work and
RuntimeError: Timeout context manager should be used inside a task
exception is caught. If you passtimeout=None
toself.session.get
it fixes theRuntimeError
, but you get other warnings aboutUnclosed Session
. I've learnt that you shouldn't create asession
outside acoroutine
and was suggested something likeasync def create_session(): return aiohttp.ClientSession
which brings me back to square on how would I persist it.I believe the author should provide examples of creating persisting
session
.The text was updated successfully, but these errors were encountered: