Thread Local Storage in Python

The thread local storage in Python is provided through the class local of the threading module. Once instantiated attributes can be added to the local object.

Local Updated 26 Jul 2024 1 min read

Overview

  • When a variable is created in thread local storage every thread will have its own copy which it can update.
  • Thread local storage is not automatic variables.
  • They are valid for the lifetime of the thread.
  • They need not be locked for synchronization unless shared among threads.
  • In Python, the class threading.local incorporates the concept of Thread local storage.
  • A thread local variable can be added by instantiating a threading.local object and adding attributes to it. Each attribute added to the threading.local object is a thread local variable.

Example

python
# Example Python program that uses thread local storage

            # to calculate rolling sums in different threads

            # and adding them to a global sum

            import threading

            import time

# Final sum

            g_sum = 0

# Calculates rolling sum and update the global sum

            def counterThread(numRange, tLock, tIndex):

                # Create thread local storage

                tls = threading.local()

                # Add attribute to the thread local storage

                # Per thread Sum

                tls.tSum = 0

                for i in range(0, numRange):

                    # No locking needed

                    tls.tSum += i

                # Posting final sum to the global

                tLock.acquire()

                global g_sum

                g_sum += tls.tSum

                tLock.release()

# Create locks

            tLock = threading.Lock()

# Create threads that compute individual rolling sums

            t1 = threading.Thread(target = counterThread(5, tLock, 1))

            t2 = threading.Thread(target = counterThread(2, tLock, 2))

            t3 = threading.Thread(target = counterThread(7, tLock, 3))

t1.start()

            t2.start()

            t3.start()

t1.join()

            t2.join()

            t3.join()

            print("Final sum after adding up numbers from each thread:%d"%g_sum)

Output

Output
Final sum after adding up numbers from each thread:32