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

Is Session/Client thread or asyncio safe? #1088

Open
Azmisov opened this issue Feb 10, 2024 · 4 comments
Open

Is Session/Client thread or asyncio safe? #1088

Azmisov opened this issue Feb 10, 2024 · 4 comments

Comments

@Azmisov
Copy link

Azmisov commented Feb 10, 2024

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
Copy link
Collaborator

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

@Azmisov
Copy link
Author

Azmisov commented Feb 12, 2024

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
Copy link
Collaborator

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
Copy link

@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?

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

3 participants