Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is boto3 client thread-safe

Is boto3 low level client for S3 thread-safe? Documentation is not explicit about it.

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#client

A similar issue is discussed in Github

https://github.com/boto/botocore/issues/1246

But still there is no answer from maintainers.

like image 649
Andrey Novosad Avatar asked Oct 15 '18 16:10

Andrey Novosad


People also ask

Are Boto3 clients thread safe?

client function is not thread-safe. It can fail when called from multiple threads #2750.

Should I use Boto3 client or resource?

To summarize, resources are higher-level abstractions of AWS services compared to clients. Resources are the recommended pattern to use boto3 as you don't have to worry about a lot of the underlying details when interacting with AWS services.

Do you need to close Boto3 client?

There is little to gain by manually closing the boto connections because they are just HTTP connections and will close automatically after a few minutes of idle time. I wouldn't worry about trying to close them.

What is Boto3 client?

Boto3 documentationYou use the AWS SDK for Python (Boto3) to create, configure, and manage AWS services, such as Amazon Elastic Compute Cloud (Amazon EC2) and Amazon Simple Storage Service (Amazon S3). The SDK provides an object-oriented API as well as low-level access to AWS services.


2 Answers

If you take a look at the Multithreading/Processing documentation for boto3 you can see that they recommend one client per session as there is shared data between instance that can be mutated by individual threads.

It also looks like there's an open GitHub issue for this exact question. https://github.com/boto/botocore/issues/1246

like image 127
Skam Avatar answered Sep 19 '22 12:09

Skam


I recently tried using the single boto client instance using concurrent.futures.ThreadPoolExecutor. I run into exceptions coming from boto. I assume the boto client is not thread safe in this case.

The exception I got

  File "xxx/python3.7/site-packages/boto3/session.py", line 263, in client     aws_session_token=aws_session_token, config=config)   File "xxx/python3.7/site-packages/botocore/session.py", line 827, in create_client     endpoint_resolver = self._get_internal_component('endpoint_resolver')   File "xxx/python3.7/site-packages/botocore/session.py", line 694, in _get_internal_component     return self._internal_components.get_component(name)   File "xxx/python3.7/site-packages/botocore/session.py", line 906, in get_component     del self._deferred[name] 
like image 24
Pawel Avatar answered Sep 18 '22 12:09

Pawel