-
-
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
Replace aiohttp.ClientSession with aiohttp.create_session() #2473
Comments
In our projects we currently use it as in your example:
This way we reuse the session in all the calls done and we can do it in the
IMO as a user, unless I'm missing something I prefer initializing the class directly without having to worry if there is loop available when I create the session or that I'm inside an |
@argaen |
In your case you should monkey-patch the session in test suites: every test is run in own test loop. BTW |
@kxepal its not a global shared object. This object is scoped inside the class that is going to use it which makes that class standalone and able to work without needing external setup. Passing the session is an overhead, we may have 10-20 outbound services we need to contact from our stack. Having to create 10-20 sessions outside and passing them to their respective IMO is worse than just initializing the sessions inside its own class. @asvetlov in this case I'm talking more about usage in real code rather in tests. About aioredis yes I'm aware, I'm creating the pools lazily inside the class for example https://github.com/argaen/aiocache/blob/master/aiocache/backends/redis.py#L19 https://github.com/argaen/aiocache/blob/master/aiocache/backends/redis.py#L190 which forces me to end up adding more logic than I would like to initialize the pool compared to if I had a way of doing it in my |
@argaen class variable is a global object, just like a module variable. |
@argaen Passing the session as argument is not an overhead, but a good design, where you can control the session instance and decide what to pass: some special session with custom configuration for specific service or some common one which fits everyone. Testing also turns into fun. |
Ah, sorry for the confusion but in our case we use it per class instance, not as class variable. We initialize the sessions in @kxepal in our case the configuration for sessions comes from config files which is pulled by the class initializing it. |
In this case lazy evaluation is an option. |
My point here is that its True global shared variables are bad practices but here is where developer responsibility comes. IMO the public API shouldn't be restrictive and let the user decide what to do. Ofc if we keep
Still extra work compared to just doing it in the |
I want deprecate |
I'd be very careful with such change. technically that means that configuration of application can not be made outside of async context. this will complicate configuration stage and increase learning curve. |
@fafhrd91 I have a gut feeling that we need the change. |
I feel opposite, specifically to configuration stage. you need to find better example than I still don't see any evidence that ClientSession is the problem. |
@fafhrd91 do you suggest to close the issue? I can live with it: https://github.com/aio-libs/aiohttp/blob/master/aiohttp/client.py#L82-L91 raises a warning if implicit loop is not running. |
I just want this change be part of design effort to build configuration story for aiohttp based projects, rather than ad-hoc change without clear pros. |
The good point about the @asvetlov proposal is that makes the API more explicit and avoid the prone error pattern that we have right now, even though is protected with some loop checks. Therefore IMHO whatever that means make the API more consistency makes sense for me. Regarding the comment from @fafhrd91 can you evolve a bit more the meaning of::
|
main problem is, right now we do not have any consistent configuration boundaries. everything is ad-hoc. and I believe this big WTF at the moment. as opposite to runtime, in runtime everything works very well. this change moves session configuration to async context, so we would need two different type of context for configuration, which is sucks. I am fine with async context for configuration, but then aiohttp has to provide framework for it and properly define boundaries. I believe, configuration stage should happen in sync context |
@fafhrd91 |
If you pulling confit from consul, then you probably very far in system design. I am more about simple projects. Creating simple scripts with aiohttp is wtf even for me. |
@fafhrd91 something like this ? from aiohttp import config
config.client_session('microservice', limit=10, ..)
async def foo():
session = await aiohttp.session_from_config('microservice') The good think of having the config load not in the runtime but in the starting time is allowing the aiohttp sever to fail fast in case of the config is broken that does not allow to start the process. |
Aren't we deriving the conversation a bit? I agree a config object would be nice to have but I see it a bigger change and maybe we need another issue to discuss it? For now, being able to do One thing I like to apply when coding with async. Don't make functions or interactions asynchronous unless there is a reason to, and by reason I mean another |
@argaen remember that the proposed change would be backward compatible, meaning that legacy code could still use the next aiohttp release without changing the code 😉 |
@asvetlov said ClientSession() would be deprecated (#2473 (comment)) so this means at some point there would only be an asynchronous mode of creating a new session. That's what I want to avoid |
Deprecate does not mean get rid of something IMHO. In fact, the issue has a clear mention of that
But obviously get rid of this interface would mean break many and many systems. Therefore I support your concern if this is the case at last. |
What synchronous configuration are you talking about? |
we just need to replace I think this is bad decision. but you do not need to relay on my opinion, if other commuters agree this is good idea, go ahead and implement it. |
@fafhrd91 You disagree with this design decision. P.S. |
I do not use much of aiohttp and python anymore, so do not strongly rely on me. |
yes, rust. I am working on async web framework, it already has ws and http2 support. and it is fast. |
@asvetlov let me reuse this thread to ask you something about the following message
I have two questions:
|
The quick fix for "annoying message" is to explicitly pass |
In your docs :
if you want "a session per application", so its not convenience to use context manager, and if you pass loop param, it will get some circle import problem. |
Why context manager?
|
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs. |
create_session()
should accept the same params asClientSession
but return a oject with__aenter__
/__aexit__
and__await__
methods.Thus everything is supported except
client = aiohttp.ClientSession()
syntax:client = await aiohttp.create_session()
should be used instead.It prevents hard to debug things like making a session in global namespace:
The change is backward compatible: we don't get rid of
ClientSession
object and even expose it byaiohttp.__all__
.But it's a huge change from user perspective: we will rewrite all documentation to encourage a new way.
The text was updated successfully, but these errors were encountered: