python - 如何在内存中存储设置?

python - how to store settings in memory?

我是 Python 的新手,我正在尝试找出一种存储设置的方法。 我的应用程序 (python + pyqt5) 将设置存储在 SqLite 数据库中。一个函数在启动时加载这些设置并相应地设置所有文本框和复选框。

根据设置,我的程序确实必须做出不同的反应。我的程序在多个线程上工作,所有这些线程都需要知道相关设置。我不想每次需要设置时都访问 UI,我希望将值存储在变量中,并让整个应用程序都可以访问该变量。 (只读就够了...)

现在,这就是我在脑海中看到的,但现在我想知道这是否有意义,如果有不同的方法来做到这一点,那就更好了,如果有类似“可以从任何地方读取的全局环境变量?

我在网上搜索过,但没有找到这方面的任何信息。

到目前为止我探索了哪些选项:

您可以在主线程中定义一个“环境”对象,并将其传递给所有 threads/tasks。例如:

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