Skip to main content

Please read the README

Project description

ThreadSafeVariable

The problem

Imagine you have class like this

class A(object):
    def __init__(self, x):
        self.x = x

a = A(3)
print(a.x)

a.x is a member variable of object a. It is not thread safe. This means that if one thread changes `a.x``, all other threads see the new value. You can avoid this using a thread local object:

import threading

class A(object):    
    def __init__(self, x):
        self.local = threading.local()
        self.local.x

a = A(3)
print(a.local.x)

This works but has two downsides:

  • you have changed the syntax
  • even if a is accessible from other threads, a.local.x is only defined for the one thread that created the object

The ThreadSafeVariable module helps solve this problem and preserves the original syntax.

The solution

We change the code in the first example by declaring which variables should the thrad safe:

from threadsafevariable import ThreadSafeVariable

class A(object):
    x = ThreadSafeVariable() # we add this line
    def __init__(self, x):
        self.x = x

a = A(3)
print(a.x)

We have preserved the syntax for the first example but a.x is now a thread local variable.

In addition we can do:

iceblock = ThreadSafeVariable.freeze()

This will store a snapshot all ThreadSafeVariable(s) of all objects defined in the current thread. Then for any other thread:

ThreadSafeVariable.restore(iceblock)

will restore all variables of all objects in this thread to the value stores in iceblock.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

threadsafevariable-20250716.1.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

threadsafevariable-20250716.1-py3-none-any.whl (2.5 kB view details)

Uploaded Python 3

File details

Details for the file threadsafevariable-20250716.1.tar.gz.

File metadata

  • Download URL: threadsafevariable-20250716.1.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0a3

File hashes

Hashes for threadsafevariable-20250716.1.tar.gz
Algorithm Hash digest
SHA256 b7d6915a72f52dc1881385d61ffa5978bfda55c2711648c95d267255c8963f21
MD5 9bdb017702314ddd24bcb648994a2f7e
BLAKE2b-256 b102ecd9c2b0da62a4f4de74b24e7de996f5aa06eeeaaa55587b7b40f3ecb9a5

See more details on using hashes here.

File details

Details for the file threadsafevariable-20250716.1-py3-none-any.whl.

File metadata

File hashes

Hashes for threadsafevariable-20250716.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f0293e56749a523a3c6e2b117fa6db9704000afd8c5b44d59c862d8fa26bf50c
MD5 b70d2f610e233435616d5ecaaa1f9809
BLAKE2b-256 7cff8845cd7d50a09716fa121bd5283f5a6c5f2c27e60d6b2dec71b62eb015a5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page