terricain / aioboto3

Wrapper to use boto3 resources with the aiobotocore async backend
Apache License 2.0
719 stars 74 forks source link

Creating session is taking more time than uploading the file #288

Closed abhinavsp0730 closed 1 year ago

abhinavsp0730 commented 1 year ago

Description

I'm having a fastapi server where I upload file using aioboto3. But Surprisingly the time taking to create the session and initialing the session client is more than the the time to upload the file. I have used line profiler to profile the code.

             Hits         Time  Per Hit   % Time  Line Contents
            1     26882.0  26882.0     26.5      session = aioboto3.Session()
            2      71569.0  35784.5     70.6      async with session.client(
            2       2709.0   1354.5      2.7          await s3.upload_fileobj(

@terrycain is there any way I can move session = aioboto3.Session() in the startup of the web application. But I'm not able to move initialition of session.client to the startup of the web application.

Thanks

terricain commented 1 year ago

See here https://github.com/terrycain/aioboto3/issues/279, yeah I definitely wouldn't recommend creating a session repeatedly

abhinavsp0730 commented 1 year ago

@terrycain just wanna confirm, is it safe to do so? Are there any risk of getting the connection closed?

terricain commented 1 year ago

In theory as long as you do async with on .client or .resource, that should suffice

josebsneto commented 1 year ago

I am also trying to do the same thing. My idea is to build a session and client factory and use it. Keeping the session ok, but keeping the client I still don't know if it's a good practice. do you know?

terricain commented 1 year ago

I've kept clients open in the past, especially when dealing with multiregion stuff, it should be fine to do so but I'd do a bit of stress testing with that as the internals of aiobotocore have changed a fair bit since I've been that deep in the code