我一直在使用 Python 的多处理模块。我想在一个类中创建多处理工作者,并将这个类的对象用作另一个类中的组合。下面是结构看起来与我的原始代码相同的虚拟代码。
一个.py
import concurrent.futures
class A:
def __init__(self, basic_input):
self.initial = basic_input
def _worker(self, work):
print(f"Doing some {work} using {self.initial}")
def print_my_work(self, set_of_works):
with concurrent.futures.ProcessPoolExecutor(max_workers=5) as executor:
executor.map(self._worker, set_of_works)
b.py
class B:
def __init__(self, basic_input):
# A is a composite object
self.a = A(basic_input)
def get_all_works(self):
return {'A', 'B', 'C'}
def processingB(self):
works = self.get_all_works()
self.a.print_my_work(works)
在这里,我尝试在另一个模块中使用 B 类,如下所示
检查.py
import b
obj = B('Test')
obj.processingB()
出现以下错误 Python multiprocessing RuntimeError: 尝试在当前进程完成其引导阶段之前启动一个新进程......
有人可以帮助我。感谢您阅读这个问题。