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

Using client sessions without a context manager #3119

Closed
aliceh75 opened this issue Jul 2, 2018 · 1 comment · Fixed by #3332
Closed

Using client sessions without a context manager #3119

aliceh75 opened this issue Jul 2, 2018 · 1 comment · Fixed by #3332
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers Hacktoberfest We think it's good for https://hacktoberfest.digitalocean.com/ outdated

Comments

@aliceh75
Copy link

aliceh75 commented Jul 2, 2018

It would be useful to be able to use ClientSession without a context manager.

Looking at the code:

So the following currently works:

    session = ClientSession()
    async with session.get('...'):
        # ...
    await session.close()

However the documentation doesn't specify whether we can rely on this behaviour. I can see three options:

  1. Make the current behaviour official by documenting it;
  2. Add a different way to use ClientSession without a context manager (I would guess the main change would be to add an async ClientSession.start() even if it currently does nothing for future-proofing);
  3. Do nothing, and document the fact people shouldn't rely on the current behaviour (people can always call __aenter__ and __aexit__ directly, I understand it's an accepted thing to do).

I'm happy to help and do a PR for whichever solution you prefer.

@asvetlov
Copy link
Member

asvetlov commented Jul 8, 2018

@aliceh75 sorry for delay.
Your snippet is correct, it is the official way for working with ClientSession without async context manager.
I thought it is obvious but looks like we need to document it explicitly.
Please feel free to make a PR with docs update.

The only neat: client = aiohttp.ClientSession() should be called from an async function, the session creation without active (running) event loop raises a user warning.

@asvetlov asvetlov added good first issue Good for newcomers documentation Improvements or additions to documentation labels Jul 24, 2018
@webknjaz webknjaz added Hacktoberfest We think it's good for https://hacktoberfest.digitalocean.com/ and removed Hacktoberfest We think it's good for https://hacktoberfest.digitalocean.com/ labels Oct 3, 2018
@lock lock bot added the outdated label Mar 27, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers Hacktoberfest We think it's good for https://hacktoberfest.digitalocean.com/ outdated
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants