threadsafevariable


Namethreadsafevariable JSON
Version 20250716.1 PyPI version JSON
download
home_pageNone
SummaryPlease read the README
upload_time2025-08-24 20:55:37
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "threadsafevariable",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "Massimo Di Pierro <massimo.dipierro@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/b1/02/ecd9c2b0da62a4f4de74b24e7de996f5aa06eeeaaa55587b7b40f3ecb9a5/threadsafevariable-20250716.1.tar.gz",
    "platform": null,
    "description": "# ThreadSafeVariable\n\n## The problem\n\nImagine you have class like this\n\n```\nclass A(object):\n    def __init__(self, x):\n        self.x = x\n\na = A(3)\nprint(a.x)\n```\n\n``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:\n\n```\nimport threading\n\nclass A(object):    \n    def __init__(self, x):\n        self.local = threading.local()\n        self.local.x\n\na = A(3)\nprint(a.local.x)\n``` \n\nThis works but has two downsides:\n\n- you have changed the syntax\n- even if a is accessible from other threads, a.local.x is only defined for the one thread that created the object\n\nThe ThreadSafeVariable module helps solve this problem and preserves the original syntax.\n\n## The solution\n\nWe change the code in the first example by declaring which variables should the thrad safe:\n\n```\nfrom threadsafevariable import ThreadSafeVariable\n\nclass A(object):\n    x = ThreadSafeVariable() # we add this line\n    def __init__(self, x):\n        self.x = x\n\na = A(3)\nprint(a.x)\n```\n\nWe have preserved the syntax for the first example but a.x is now a thread local variable.\n\nIn addition we can do:\n\n```\niceblock = ThreadSafeVariable.freeze()\n```\n\nThis will store a snapshot all ThreadSafeVariable(s) of all objects defined in the current thread.\nThen for any other thread:\n\n```\nThreadSafeVariable.restore(iceblock)\n```\n\nwill restore all variables of all objects in this thread to the value stores in iceblock.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Please read the README",
    "version": "20250716.1",
    "project_urls": {
        "Bug Tracker": "https://github.com/web2py/yatl/issues",
        "Homepage": "https://github.com/web2py/yatl"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "7cff8845cd7d50a09716fa121bd5283f5a6c5f2c27e60d6b2dec71b62eb015a5",
                "md5": "b70d2f610e233435616d5ecaaa1f9809",
                "sha256": "f0293e56749a523a3c6e2b117fa6db9704000afd8c5b44d59c862d8fa26bf50c"
            },
            "downloads": -1,
            "filename": "threadsafevariable-20250716.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b70d2f610e233435616d5ecaaa1f9809",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 2515,
            "upload_time": "2025-08-24T20:55:36",
            "upload_time_iso_8601": "2025-08-24T20:55:36.408118Z",
            "url": "https://files.pythonhosted.org/packages/7c/ff/8845cd7d50a09716fa121bd5283f5a6c5f2c27e60d6b2dec71b62eb015a5/threadsafevariable-20250716.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "b102ecd9c2b0da62a4f4de74b24e7de996f5aa06eeeaaa55587b7b40f3ecb9a5",
                "md5": "9bdb017702314ddd24bcb648994a2f7e",
                "sha256": "b7d6915a72f52dc1881385d61ffa5978bfda55c2711648c95d267255c8963f21"
            },
            "downloads": -1,
            "filename": "threadsafevariable-20250716.1.tar.gz",
            "has_sig": false,
            "md5_digest": "9bdb017702314ddd24bcb648994a2f7e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 2491,
            "upload_time": "2025-08-24T20:55:37",
            "upload_time_iso_8601": "2025-08-24T20:55:37.502674Z",
            "url": "https://files.pythonhosted.org/packages/b1/02/ecd9c2b0da62a4f4de74b24e7de996f5aa06eeeaaa55587b7b40f3ecb9a5/threadsafevariable-20250716.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-24 20:55:37",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "web2py",
    "github_project": "yatl",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "threadsafevariable"
}
        
Elapsed time: 1.04680s