I have 3 files and I want to do something like below
[1] conf.py
var = 10 # Intialized with 10 (start)
[2] file_1.py
import conf
print(conf.var) # Prints 10
conf.var = 1000 # Updated to 1000
[3] file_2.py
import conf
print(conf.var) # Prints 1000
conf.var = 9999 # Updated to 9999
I want something like this. Assume that the files, file_1 and file_2 will be running and will stay in memory unless pressed CTRL+C
. How can I change var
in other 2 files and persist the value for it? Final value for var
should be 9999 if we were to use it in file_3 like other 2 files. [It should print 9999] and so on.
Execution order file_1.py -> file_2.py
.
Help me with identifying some way to do it or some module/package that can handle this.
Thanks! :)
I worked on this problem. I will suggest to use shared memory cache providers like Redis
or memcached
. Both start a server instance that we need to connect and use it like a key-value store. Note than values should be of type str
or bytes
.
memcached
sudo apt install memcached
You need to start the server before using it or you can set it as a service in linux
sudo service memcached start
pip install pymemcache
from pymemcache.client import base
client = base.Client(('localhost', 11211))
client.set('some_key', 'stored value')
client.get('some_key') # Returns "stored value"
The process is exact same for redis
redis
sudo apt install redis
You need to start the server before using it or you can set it as a service in linux
sudo service redis start
pip install redis
import redis
client = redis.Redis() # Default settings/credentials
client.set('some_key', 'stored value')
client.get('some_key') # Returns "stored value"