简体   繁体   中英

python - how to store settings in memory?

I'm totally new to Python, and I am trying to figure out a way to store settings. My application (python + pyqt5) stores the settings in a SqLite database. A function loads these settings at startup and sets all the textboxes and checkboxes accordingly.

Based on the settings, my program does have to reacti differently. My program works on multiple threads, and all these threads need to know the pertinent settings. I do not want to access the UI each time I need a setting, i would like to have the value stored in a variable, and have this variable accessible from the whole app. (read only is enough...)

Now, this is what i see in my head, but now I would like to know if this makes even sense, if there is a different way to do it, that is better, if there is something like a "global environment variable" that can be read from everywhere?

I searched online, but was not able to find any information to this.

what options I explored so far:

  • passing the values to the threads so they have their own "copy" of the settings. This did not work as some functions wait for external input and when they use the settings, they might have been changed and be old.

  • same as above, but updating the settings with a dedicated signal each time something changes. Works, but has the feeling of reinventing the wheel. There must be a nicer way.

  • Making a global variable accessibel from everywhere - Would be nice, but couldnt find a way to do this so far.

  • query the database when needed from every single thread. works, but I dont like it. It feels like slow compared to accessing directly the memory.

You may define an "environment" object in your main thread, and pass it to all your threads/tasks. For instance:

from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from random import randint

@dataclass
class MyEnv():
    mynumber : int = 0

def setup_task(env):
    env.mynumber = randint(0,10)
    return(f'setting the magic number to {env.mynumber}')

def normal_task(env):
    return(f'the magic number is currently {env.mynumber}')

myenv = MyEnv()

with ThreadPoolExecutor(10) as executor:
        for future in [executor.submit(task, myenv) for i in range(10) for task in [setup_task, normal_task]]:
            print(future.result())
----
setting the magic number to 1
the magic number is currently 1
setting the magic number to 1
the magic number is currently 1
setting the magic number to 9
the magic number is currently 9
setting the magic number to 3
the magic number is currently 3
setting the magic number to 4
the magic number is currently 4
setting the magic number to 0
the magic number is currently 0
setting the magic number to 8
the magic number is currently 8
setting the magic number to 2
the magic number is currently 2
setting the magic number to 1
the magic number is currently 1
setting the magic number to 10
the magic number is currently 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM