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

question #2992

Closed
yjqiang opened this issue May 11, 2018 · 5 comments
Closed

question #2992

yjqiang opened this issue May 11, 2018 · 5 comments
Labels

Comments

@yjqiang
Copy link

yjqiang commented May 11, 2018

I have several different requests on different urls, like google.com, facebook.com, twitter.com. Should I just use one aiohttp.ClientSession for 3 requests or I use 'async with' to create 3 new different ClientSession and then close it at once? Because I wonder if the aiohttp.ClientSession is like the requests.Session, which should be used for the same host according to the doc.
"The Session object allows you to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance, and will use urllib3’s connection pooling. So if you’re making several requests to the same host, the underlying TCP connection will be reused, which can result in a significant performance increase (see HTTP persistent connection)."

@yjqiang
Copy link
Author

yjqiang commented May 11, 2018

And if I use those three request in a loop and will be executed for many times, would it be better to create 3 ClientSession first, and use them for 3 different hosts or just create one ClientSession and use it for those 3 hosts.

@asvetlov
Copy link
Member

Client session shared cookies and uses a connection pool under the hood.
The session can be used for calling different hosts as well.

Consider it as a browser window. How many open windows do you have?
Do you create a new window for every request or reuse the same one for browsing different sites?
The rule of thumb is the same.

@yjqiang
Copy link
Author

yjqiang commented May 11, 2018

@asvetlov "So if you’re making several requests to the same host, the underlying TCP connection will be reused, which can result in a significant performance increase (see HTTP persistent connection)."
So I think if I used one host frequently, I should new one ClienSession for it (keep open without using 'async with'). And for others, I can new another ClientSession for all of them (also keep open without using 'async with'). Am I right? Because the script may be kept running for 24h, so I think that would be a good solution.

@asvetlov
Copy link
Member

Yes, this is a working plan

@lock
Copy link

lock bot commented Oct 28, 2019

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.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants