aio-libs / aiobotocore

asyncio support for botocore library using aiohttp
https://aiobotocore.rtfd.io
Apache License 2.0
1.14k stars 179 forks source link

Is Session/Client thread or asyncio safe? #1088

Closed Azmisov closed 1 month ago

Azmisov commented 6 months ago

The in the boto3 docs it says Session objects are not thread safe. See here, which mentions clients are threadsafe once created, but not session or resource. I was wondering what kind of safety we get with a aiobotocore:

  1. can I reuse a session across multiple threads?
  2. can I reuse a client across multiple threads?
  3. can I reuse a session for multiple event loops?
  4. can I reuse a client for multiple event loops?
  5. can I reuse a session for simultaneous running tasks in the same event loop?
  6. can I reuse a client for simultaneous running tasks in the same event loop?

The thread safety guards in the underlying botocore I'm guessing can't guarantee 4, e.g. threading and asyncio synchronization primitives are different.

thehesiod commented 6 months ago

asyncio loops are not thread safe, each thread has a separate loop

Azmisov commented 6 months ago

So no for 1/2/3/4, due to @thehesiod's point which implies session/client are attached to a single asyncio loop (which is what you would expect).

Just tested 5/6 and the answer appears to be yes for those: you can reuse both session and client. I wrote, then read 3 files simultaneously from S3 and all tasks gave the correct results.

thehesiod commented 6 months ago

ya 5 and 6 are yes. A client has N tcp connectors you can use at a time in a loop (it's configurable via the Config object)

mohitk1995 commented 2 months ago

@thehesiod I am creating a single "sagemaker-runtime" client and just want to keep using it. Could we make use of this client for calling into four sagemaker endpoints in a async manner? Or we need to create one client for each endpoint?

thehesiod commented 2 months ago

@mohitk1995 this is the same answer as for botocore. I believe the only way to override the endpoint is via a client constructor (https://github.com/boto/botocore/blob/develop/botocore/session.py#L839)

thehesiod commented 1 month ago

going to close as I think we answered the main question. botocore is not cross thread safe, neither is aiobotocore. Given aiobotocore is async, it's tied to a specific run loop, which itself is tied to a specific thread. So 1.no 2.no 3.no 4.no 5.yes 6.yes