Name | EasyGlobals JSON |
Version |
0.1.1
JSON |
| download |
home_page | |
Summary | An easy to use variable sharing library for Python using Memcached to can quickly share complex objects between processes as if they had shared memory. |
upload_time | 2023-08-18 14:55:51 |
maintainer | |
docs_url | None |
author | |
requires_python | >=3.7 |
license | |
keywords |
globals
python
shared memory
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Introduction
EasyGlobals is an easy to use variable sharing library for Python that can quickly share almost all types of variables and full Python objects between processes as if they had shared memory.
It uses Memcached in the background to share full objects and other complex variables between Python processes. Inspired by many closed source languages which have an easy way to share "global" variables between processes while retaining pythonic syntax. No need for Multiprocessing Manager dicts, queues and other complex data pipelines in your program anymore.
# Usage
After installation following the instructions down below, try running the following example
```
import EasyGlobals
globals = EasyGlobals.Globals()
globals.test1 = 4
globals.test2 = 'hello world'
globals.test3 = {'dictkey1': globals.test1, 'dictkey2': globals.test2} # Dict
print(globals.test1)
print(globals.test2)
print(globals.test3)
```
- Keep in mind that locks are not implemented here, meaning that race conditions can happen if two processes are writing to the same variable at the same time. For example when they're both incrementing the same value in a loop this can cause unwanted behavior.
- To prevent this, an easy solution is to write to a variable in only one process. Reading can be done in as many processes as desired. Writing on multiple processes is not restricted but you will need to deal with the race conditions so prepare for coding headaches if you do.
# Installation
Install Memcached which acts as the server. installation with Apt is availible on Ubuntu. Different operating systems haven't been tested yet but installation guides with Windows can be found online
```
sudo apt install memcached
sudo systemctl start memcached
```
Now you can install the easyglobals library:
```
pip install easyglobals
```
# Limitations:
- Any variable type that can be pickled should work. E.g. Numpy arrays, OpenCV images.
- Directly getting and setting values from nested objects such as dicts or classes in the Globals can be problematic as the values are in binary format (pickled) while they are in memcached.
- It's currently best to retrieve the an object from globals, modify it locally, and then store the entire object in globals again
- If you're already using Memcached and your variable keys have the same name they will be overwritten
- All variables will stay stored in Memcached even when your program stops. Restart memached to clear them, for example by running "service memcached restart" in the terminal.
# Speed 🚀
Tested on a Ryzen 5900X
- 100.000 writes/reads per second on for small variables
- 1.000 writes/reads per second for big variables such as 4K OpenCV images.
- This is around 200-400 times slower than using normal Python local variables. For most programs this won't matter but keep in mind that any variable in direct process memory will always be faster than having to pickle it and send it to Memcached.
- Redis and SQLite have been tested as potential back-ends but were too slow.
- Memcached was the fastest as it is a dedicated key-value DB.
- Redis is more designed for distributed computing and storing a huge amount of variables.
- SQLite cannot share variables between processes while running in-memory.
# Multiprocessing example:
```
from EasyGlobals import EasyGlobals
import multiprocessing
def write_to_globals():
g = EasyGlobals.Globals()
for i in range(1_000):
g.testvar = i
def retrieve_from_globals(process_id):
g = EasyGlobals.Globals()
for i in range(10):
result = g.testvar
print(f'Process {process_id}, read: {result}')
print('Start writing process')
write_process = multiprocessing.Process(target=write_to_globals)
write_process.start()
print('Start reading with 3 simultaneous processes')
processlist = []
for i in range(5):
processlist.append(multiprocessing.Process(target=retrieve_from_globals, args=(i,)))
processlist[i].start()
for process in processlist:
process.join()
print('Done reading')
write_process.join()
print('Done writing')
```
Raw data
{
"_id": null,
"home_page": "",
"name": "EasyGlobals",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "Globals,Python,Shared memory",
"author": "",
"author_email": "Y <asnaeb2@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/83/a1/a1b7f42dda5bef3fce20f8dddada6c57c212478cb454636474fa8fea4729/easyglobals-0.1.1.tar.gz",
"platform": null,
"description": "# Introduction\n EasyGlobals is an easy to use variable sharing library for Python that can quickly share almost all types of variables and full Python objects between processes as if they had shared memory.\n\nIt uses Memcached in the background to share full objects and other complex variables between Python processes. Inspired by many closed source languages which have an easy way to share \"global\" variables between processes while retaining pythonic syntax. No need for Multiprocessing Manager dicts, queues and other complex data pipelines in your program anymore.\n\n# Usage\nAfter installation following the instructions down below, try running the following example\n```\nimport EasyGlobals\nglobals = EasyGlobals.Globals()\n\nglobals.test1 = 4\nglobals.test2 = 'hello world'\nglobals.test3 = {'dictkey1': globals.test1, 'dictkey2': globals.test2} # Dict\n\nprint(globals.test1)\nprint(globals.test2)\nprint(globals.test3)\n```\n\n- Keep in mind that locks are not implemented here, meaning that race conditions can happen if two processes are writing to the same variable at the same time. For example when they're both incrementing the same value in a loop this can cause unwanted behavior.\n\n- To prevent this, an easy solution is to write to a variable in only one process. Reading can be done in as many processes as desired. Writing on multiple processes is not restricted but you will need to deal with the race conditions so prepare for coding headaches if you do.\n\n# Installation\nInstall Memcached which acts as the server. installation with Apt is availible on Ubuntu. Different operating systems haven't been tested yet but installation guides with Windows can be found online\n```\nsudo apt install memcached\nsudo systemctl start memcached\n```\n\nNow you can install the easyglobals library:\n```\npip install easyglobals\n```\n\n\n# Limitations:\n- Any variable type that can be pickled should work. E.g. Numpy arrays, OpenCV images.\n- Directly getting and setting values from nested objects such as dicts or classes in the Globals can be problematic as the values are in binary format (pickled) while they are in memcached.\n- It's currently best to retrieve the an object from globals, modify it locally, and then store the entire object in globals again\n- If you're already using Memcached and your variable keys have the same name they will be overwritten\n- All variables will stay stored in Memcached even when your program stops. Restart memached to clear them, for example by running \"service memcached restart\" in the terminal.\n\n# Speed \ud83d\ude80\nTested on a Ryzen 5900X\n- 100.000 writes/reads per second on for small variables\n- 1.000 writes/reads per second for big variables such as 4K OpenCV images. \n\n\n- This is around 200-400 times slower than using normal Python local variables. For most programs this won't matter but keep in mind that any variable in direct process memory will always be faster than having to pickle it and send it to Memcached.\n\n- Redis and SQLite have been tested as potential back-ends but were too slow.\n- Memcached was the fastest as it is a dedicated key-value DB.\n- Redis is more designed for distributed computing and storing a huge amount of variables.\n- SQLite cannot share variables between processes while running in-memory.\n\n# Multiprocessing example:\n```\nfrom EasyGlobals import EasyGlobals\nimport multiprocessing\n\ndef write_to_globals():\n g = EasyGlobals.Globals()\n for i in range(1_000):\n g.testvar = i\n\ndef retrieve_from_globals(process_id):\n g = EasyGlobals.Globals()\n for i in range(10):\n result = g.testvar\n print(f'Process {process_id}, read: {result}')\n\n\nprint('Start writing process')\nwrite_process = multiprocessing.Process(target=write_to_globals)\nwrite_process.start()\n\nprint('Start reading with 3 simultaneous processes')\nprocesslist = []\nfor i in range(5):\n processlist.append(multiprocessing.Process(target=retrieve_from_globals, args=(i,)))\n processlist[i].start()\n\nfor process in processlist:\n process.join()\nprint('Done reading')\nwrite_process.join()\nprint('Done writing')\n\n```\n\n",
"bugtrack_url": null,
"license": "",
"summary": "An easy to use variable sharing library for Python using Memcached to can quickly share complex objects between processes as if they had shared memory.",
"version": "0.1.1",
"project_urls": {
"Bug Tracker": "https://github.com/YacobBY/Easy_Globals/issues",
"Homepage": "https://github.com/YacobBY/Easy_Globals"
},
"split_keywords": [
"globals",
"python",
"shared memory"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2224557746966b3edd4bfcb3f6506bb9bbbad0ba05948403d39ba55a61e30da7",
"md5": "f4e21260f9e082610ab3b9eac1824e54",
"sha256": "4cdef9bfeb196144312d4a64853a0e7c06cf2ee8fb9db4d4ea0e65e514800101"
},
"downloads": -1,
"filename": "easyglobals-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f4e21260f9e082610ab3b9eac1824e54",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 9542,
"upload_time": "2023-08-18T14:55:50",
"upload_time_iso_8601": "2023-08-18T14:55:50.366954Z",
"url": "https://files.pythonhosted.org/packages/22/24/557746966b3edd4bfcb3f6506bb9bbbad0ba05948403d39ba55a61e30da7/easyglobals-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "83a1a1b7f42dda5bef3fce20f8dddada6c57c212478cb454636474fa8fea4729",
"md5": "983b04762d5a80d5b41143d9376cd706",
"sha256": "4f42054627d6007d57f5f72f32a64721bcebcbd3b23113907c1911298526e1c0"
},
"downloads": -1,
"filename": "easyglobals-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "983b04762d5a80d5b41143d9376cd706",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 16801,
"upload_time": "2023-08-18T14:55:51",
"upload_time_iso_8601": "2023-08-18T14:55:51.951850Z",
"url": "https://files.pythonhosted.org/packages/83/a1/a1b7f42dda5bef3fce20f8dddada6c57c212478cb454636474fa8fea4729/easyglobals-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-18 14:55:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "YacobBY",
"github_project": "Easy_Globals",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "easyglobals"
}