Create separate instances of non-thread-safe resources for each thread or process in concurrent applications rather than sharing a single instance. Many objects that maintain state or shared metadata (like sessions, connections, or context-dependent resources) are not thread-safe and can cause race conditions or unpredictable behavior when accessed...
Create separate instances of non-thread-safe resources for each thread or process in concurrent applications rather than sharing a single instance. Many objects that maintain state or shared metadata (like sessions, connections, or context-dependent resources) are not thread-safe and can cause race conditions or unpredictable behavior when accessed concurrently.
For example, when using a library with both thread-safe and non-thread-safe components:
import threading
import boto3.session
class MyTask(threading.Thread):
def run(self):
# Create a new session per thread
session = boto3.session.Session()
# Create resource using thread's session
s3 = session.resource('s3')
# Put your thread-safe code here
# ...
# Create and start multiple threads
threads = [MyTask() for _ in range(5)]
for thread in threads:
thread.start()
For thread-safe components (like certain clients), you may create them once and share across threads, but be aware of limitations:
Enter the URL of a public GitHub repository