What is a thread-safe way to share a boto3 client or Session?
AdvancedConceptConcurrency: threads, processes & asyncio
Answer
boto3 low-level clients are documented as generally thread-safe, so one client can be shared across threads. Session objects and resources are not thread-safe. The safe pattern is: create the Session per thread (or create all clients up front in the main thread) and never create clients concurrently from a shared default session, which can raise intermittent errors like KeyError: 'endpoint_resolver' or credential_provider races. In our lab, each region worker creates its own boto3.session.Session().
More on Concurrency: threads, processes & asyncio
What is the GIL, and what does it mean for DevOps scripts?IntermediateHow do you use `ThreadPoolExecutor` for parallel API calls?BeginnerThreads vs processes vs asyncio: how do you choose?IntermediateWhat is a race condition, and how do you prevent one in threaded code?IntermediateHow does a producer/consumer pattern with `queue.Queue` work?AdvancedWhat are coroutines, `async def` and `await`?Intermediate