Search code examples
pythonpython-3.xmemoryglobal-variables

How to use and update same variable across multiple modules in python independently?


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! :)


Solution

  • 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

    Install memcached
    sudo apt install memcached
    
    Start Server with default settings

    You need to start the server before using it or you can set it as a service in linux

    sudo service memcached start
    
    Install python memcached client
    pip install pymemcache
    
    Sample Code
    from pymemcache.client import base
    
    client = base.Client(('localhost', 11211))
    client.set('some_key', 'stored value')
    
    client.get('some_key')  # Returns "stored value"
    

    Redis

    The process is exact same for redis

    Install redis
    sudo apt install redis
    
    Start Server with default settings

    You need to start the server before using it or you can set it as a service in linux

    sudo service redis start
    
    Install python redis client
    pip install redis
    
    Sample Code
    import redis
    
    client = redis.Redis()  # Default settings/credentials
    client.set('some_key', 'stored value')
    
    client.get('some_key')  # Returns "stored value"
    

    Usage

    • Redis gives more safety and fault tolerance.
    • Both has some memory limits but is configurable. If your data is more in size, use files or db suitable to your problem.
    • Both are easy to configure