Skip to main content

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.

Metadata

Release files for threadsafevariable 20250716.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for threadsafevariable 20250716.1
File Size Uploaded
threadsafevariable-20250716.1.tar.gz 2.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for threadsafevariable 20250716.1
File Interpreter ABI Platform
threadsafevariable-20250716.1-py3-none-any.whl Python 3 none any Details

Total release size: 5.0 kB

Release files / threadsafevariable-20250716.1.tar.gz

Download URL threadsafevariable-20250716.1.tar.gz
Size 2.5 kB
Tags Source
SHA-256 checksum
How to use checksums
b7d6915a72f52dc1881385d61ffa5978bfda55c2711648c95d267255c8963f21
BLAKE2b-256 checksum
How to use checksums
b102ecd9c2b0da62a4f4de74b24e7de996f5aa06eeeaaa55587b7b40f3ecb9a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.0a3

Release files / threadsafevariable-20250716.1-py3-none-any.whl

Download URL threadsafevariable-20250716.1-py3-none-any.whl
Size 2.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f0293e56749a523a3c6e2b117fa6db9704000afd8c5b44d59c862d8fa26bf50c
BLAKE2b-256 checksum
How to use checksums
7cff8845cd7d50a09716fa121bd5283f5a6c5f2c27e60d6b2dec71b62eb015a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.0a3

Release history Release notifications | RSS feed

This release

20250716.1 This release

2 release files

1.2

1 release file

1.1

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page