我看过许多关于concurrent.futures.ProcessPoolExecutor
基本教程的YouTube视频。我也在SO这里和这里,GitHub和GitHubMemory中看到过帖子,但没有运气。
问题:我得到以下运行时错误:
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
我承认,我不完全理解这个错误,因为这是我第一次尝试在python代码中进行多处理。
这是我的伪代码:
模块.py
import xyz
from multiprocessing import freeze_support
def abc():
return x
def main():
xyz
qwerty
if __name__ == "__main__":
freeze_support()
obj = Object()
main()
classObject.py
import abcd
class Object(object):
def __init__(self):
asdf
cvbn
with concurrent.futures.ProcessPoolExecutor(max_workers=2) as executor:
executor.map(self.function_for_multiprocess, var1, var2)
# ****The error points at the code above.👆*👆*👆
def function_for_multiprocess(var1, var2):
doSomething1
doSomething2
self.variable = something
我的类文件(classObject.py(没有;主";警卫
我尝试过的东西:
- 尝试在
classObject.py
中添加if __name__ == "__main__":
和freeze_support
,同时重命名__init__() to
main((` - 执行上述操作时,从
module.py
中删除了freeze_support
我没有找到与上面提供的链接不同的解决方案。如有任何见解,我们将不胜感激!
我使用的是MacBook Pro(16英寸,2019(,处理器2.3 GHz 8核英特尔酷睿i9,操作系统:Big Sur。我认为这并不重要,但如果重要的话,就宣布它。
您需要将参数作为可拾取对象传递,如列表或元组。你不需要freeze_support()
只需更改executor.map(self.function_for_multiprocess, var1, var2)
至executor.map(self.function_for_multiprocess, (var1, var2))
from multiprocessing import freeze_support
import concurrent.futures
class Object(object):
def __init__(self, var1=1, var2=2):
with concurrent.futures.ProcessPoolExecutor(max_workers=2) as executor:
executor.map(self.function_for_multiprocess, (var1, var2))
def function_for_multiprocess(var1, var2):
print('var1:', var1)
print('var2:', var2)
def abc(x):
return x
def main():
print('abc:', abc(200))
if __name__ == "__main__":
#freeze_support()
obj = Object()
main()