我正在创建一个类来设置、保存和访问整个项目中的数据.下面是我的数据类:
class Data:
"""This class stores project-related constants as class attributes for easy access throughout the project."""
ORG_URL: str = "https://www.myurl.com/"
PROJECT_NAME: str = "My Project"
root_path: str = ""
@classmethod
def set_root_path(cls, new_path):
cls.root_path = new_path
下面是我的主类,它在运行时设置根:
from multiprocessing import Process, Manager
class Main:
def __init__(self) -> None:
# determine if application is a script file or frozen exe
if getattr(sys, 'frozen', False):
application_path = os.path.dirname(sys.executable)
elif __file__:
application_path = os.path.dirname(__file__)
data = Data()
data.set_root_path(f"{application_path}")
with Manager() as manager:
q = manager.Queue()
args = manager.Queue()
print(data.root_path) ## prints correct path
gui = Process(target=GUI,args=(q,args,data))
runner = Process(target=Runner,args=(q,args,data))
gui.start()
runner.start()
gui.join()
runner.join()
if __name__ == "__main__":
Main()
下面是我的Runner类,我在其中访问根路径:
class Runner:
def __init__(self,q,args,data) -> None:
print("Initiating Runner")
self.q = q
self.args = args
self.data = data
print(self.data.root_path) # prints ''
我try 了多种方法,但都不能解决问题.请帮帮忙.