我正在工作在多线程环境中,并且看起来像这样一类(含过量噪声去除):classmethods线程安全吗?
class B:
@classmethod
def apply(cls, item):
cls.do_thing(item)
@classmethod
def do_thing(cls, item)
'do something to item'
def run(self):
pool = multiprocessing.Pool()
for list_of_items in self.data_groups:
pool.map(list_of_items, self.apply)
我担心的是,两个线程可能会调用apply
或do_thing
在同一时间,或者子类可能会尝试在这些函数之一中使用cls
做一些愚蠢的事情。我可以使用staticmethod
而不是classmethod
,但调用do_thing
会变得复杂得多,特别是如果一个子类重新实现其中一个而不是另一个。所以我的问题是这样的:上面的类是线程安全的,还是存在使用类方法的潜在问题?
感谢您的回答。那么就简单地调用'cls.do_thing'危险没有锁?直觉上我不这么认为,但我没有太多的多线程经验。 – aquavitae
依赖于'thing'' do_thing()'做... – shx2