Reuse create_client in aiobotocore for better performences

351 Views Asked by At

I am implementing an async Python code that uses aiobotocore to put objects in S3.

The code is something like that -

class Foo:
    def __init__(self):
        self._aiobotocore_session = get_session()

    def a():
        async with self.s3_client as self._aiobotocore_session.create_client('s3'):
            await s3_client.put_object(...)

        .... some logic ...

        async with self.s3_client as self._aiobotocore_session.create_client('s3'):
            await s3_client.put_object(...)

    def b():
        .... some logic ...

        async with self.s3_client as self._aiobotocore_session.create_client('s3'):
            await s3_client.put_object(...)

In the official docs it looks like this is about the right way to do. Howevber, I do not understand why would I call self._aiobotocore_session.create_client('s3') over and over which by the source code looks like an expensive operation...

Is there something I am missing?

If I am correct, what is the best way to cache the created client? right now it looks like I need to reimplement the ClientCreatorContext so it will persist the client initialization

0

There are 0 best solutions below