threadsafevariable


Namethreadsafevariable JSON
Version 20230507.1 PyPI version JSON
download
home_page
SummaryPlease read the README
upload_time2023-05-08 00:17:56
maintainer
docs_urlNone
author
requires_python>=3.7
license
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": "",
    "name": "threadsafevariable",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "",
    "author_email": "Massimo Di Pierro <massimo.dipierro@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/5b/4d/b85162a28c4c6e3e0e4e4b5071b8110be3c4ef330a151e18622a9b13004a/threadsafevariable-20230507.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": "",
    "summary": "Please read the README",
    "version": "20230507.1",
    "project_urls": {
        "Bug Tracker": "https://github.com/web2py/yatl/issues",
        "Homepage": "https://github.com/web2py/yatl"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2070101c3bd85ad659aafdcf3ddf38096f4cd69bf478d57a8aae6787ac7d9b44",
                "md5": "7799d3c22c2e9f15c0f6e7c44590ecd6",
                "sha256": "45d86e8b8bb20f3d540457d5c0e814fe05aeda42758ee763bef476df28248d7e"
            },
            "downloads": -1,
            "filename": "threadsafevariable-20230507.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7799d3c22c2e9f15c0f6e7c44590ecd6",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 2471,
            "upload_time": "2023-05-08T00:17:54",
            "upload_time_iso_8601": "2023-05-08T00:17:54.562158Z",
            "url": "https://files.pythonhosted.org/packages/20/70/101c3bd85ad659aafdcf3ddf38096f4cd69bf478d57a8aae6787ac7d9b44/threadsafevariable-20230507.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5b4db85162a28c4c6e3e0e4e4b5071b8110be3c4ef330a151e18622a9b13004a",
                "md5": "98b51a08bcf8518f1f00b8a31ca96af4",
                "sha256": "0849b5535fd2cc11fb659e7ff7cd3a6e617cffea4891c262a0a1b068c2664551"
            },
            "downloads": -1,
            "filename": "threadsafevariable-20230507.1.tar.gz",
            "has_sig": false,
            "md5_digest": "98b51a08bcf8518f1f00b8a31ca96af4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 2412,
            "upload_time": "2023-05-08T00:17:56",
            "upload_time_iso_8601": "2023-05-08T00:17:56.365497Z",
            "url": "https://files.pythonhosted.org/packages/5b/4d/b85162a28c4c6e3e0e4e4b5071b8110be3c4ef330a151e18622a9b13004a/threadsafevariable-20230507.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-08 00:17:56",
    "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: 0.07972s