【发布时间】:2022-01-20 10:40:31
【问题描述】:
我正在使用 Squish 自动化基于 Qt 的 GUI 应用程序。我在应用程序中递归查找 qt 对象。由于它很耗时,我想缓存一旦找到的对象以供以后重用。我有下面的类来维护字典中的对象缓存-
def __init__(self):
self.object_store = {}
@staticmethod
def instance():
if '_instance' not in ObjectCache.__dict__:
ObjectCache._instance = ObjectCache()
return ObjectCache._instance
def set(self, object_name, obj):
self.object_store[object_name] = obj
def remove(self, object_name):
del self.object_store[object_name]
def exists(self, object_name):
if object_name in self.object_store:
return True
return False
def get(self, object_name):
return self.object_store[object_name]
def get_all(self):
return self.object_store
我的自动化脚本中有以下功能的装饰器,用于从该字典中添加/访问/删除 -
def object_caching_decorator(func):
def wrapper(*args, **kwargs):
object_cache = ObjectCache.instance()
if object_cache.exists(func.__name__):
try:
if waitForObject(object_cache.get(func.__name__)):
return object_cache.get(func.__name__)
except LookupError:
object_cache.remove(func.__name__)
obj = func(*args, **kwargs)
object_cache.set(func.__name__, obj)
return obj
return wrapper
有人可能会问,为什么不能所有脚本都共享这个类对象?因为 Squish 工具会在启动每个测试脚本之前重置全局符号表,因此我需要一种方法来持久化这个对象。
如何让这个类保持运行,以便在另一个进程(Squish runner)上运行的脚本可以无缝访问它?
【问题讨论】:
-
这能回答你的问题吗? How can I share a class between processes?
-
如果你使用线程,全局实例是自动共享的,所以这对初学者来说可能更容易
-
@576i 感谢您的快速回复。我刚刚编辑了我的问题以提供完整的上下文。希望这有助于理解问题陈述。
标签: python-3.x subprocess python-multiprocessing python-multithreading squish