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

persistent client #806

Closed
potatochip opened this issue May 1, 2020 · 9 comments
Closed

persistent client #806

potatochip opened this issue May 1, 2020 · 9 comments

Comments

@potatochip
Copy link

I understand you changed the api to make a client accessible only from a context manager, but is it still safe to persist the client anyway?

I am using the following to persist it.

_SESSION = aiobotocore.get_session()
client_context = _SESSION.create_client('s3')
self._client = await client_context.__aenter__()
@potatochip potatochip changed the title persistant client persistent client May 1, 2020
@thehesiod
Copy link
Collaborator

If you don't close the client you'll get warnings about unclosed connections

it's better to use AsyncExitStack

and then aexit that when you're done

@thehesiod
Copy link
Collaborator

I'm going to leave this open to remember to do some examples about this topic

@thehesiod
Copy link
Collaborator

ex:

exit_stack = AsyncExitStack()
s3_client = await exit_stack.enter_async_context(_SESSION.create_client('s3'))

then later during shutdown:

await exit_stack.__aexit__(None, None, None)

the nice thing of exitstack is you can stick a bunch of things in it

@potatochip
Copy link
Author

potatochip commented May 2, 2020

awesome. thanks for the heads up. ive been using await client.close() when finished.

you could probably add a __call__ method to ClientCreatorContext that returns the client. Then a user could use it as context when they need creds refreshed every time or call it as a function when they need a persistent client.

@thehesiod
Copy link
Collaborator

but that's on a create_client so it would still need to be a context because only after you have a client can you make a method call, and then free the context. It would only make sense if we had a static client. aiohttp had something similar to this, static methods on the ClientSession but deprecated as it's not efficient and really not the way the library wanted to go in terms of resource management.

@terricain
Copy link
Collaborator

https://aioboto3.readthedocs.io/en/latest/usage.html#aiohttp-server-example

I have an example, you should add one too 😉

@thehesiod
Copy link
Collaborator

ok added a couple examples to the front page, please re-open if that doesn't fit the bill :)

@fabiopedrosa
Copy link

fabiopedrosa commented Oct 20, 2020

@thehesiod @terrycain
The documentation doesn't make any reference to this, and it still seems unclear to me what the answer is.

Is it safe to persist the client, and is this the easiest way?

_SESSION = aiobotocore.get_session()
client_context = _SESSION.create_client('s3')
self._client = await client_context.__aenter__()

If that's so, why not expose the _get_client(...) to allow the manual creation/clean-up?

@thehesiod
Copy link
Collaborator

persisting the client is ok, however its better to put into a context to be cleaned up when you process/function exits, just better style to let you know the client should be cleaned up

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

No branches or pull requests

4 participants