joblib文档包含以下警告:
Under Windows, it is important to protect the main loop of code to
avoid recursive spawning of subprocesses when using joblib.Parallel.
In other words, you should be writing code like this:
06000
No code should run outside of the “if __name__ == ‘__main__’” blocks,
only imports and definitions.
最初,我认为这只是为了防止偶然的奇怪情况,传递给joblib.Parallel的函数递归地调用模块,这意味着它通常是良好的做法但通常是不必要的.但是,对我来说这没有意义,为什么这只会是Windows的风险.另外,this answer似乎表明无法保护主循环导致代码运行速度比非常简单的非递归问题慢几倍.
出于好奇,我从joblib文档中运行了一个非常简单的尴尬并行循环示例,而没有保护Windows框中的主循环.我的终端被垃圾邮件发送以下错误,直到我关闭它:
ImportError: [joblib] Attempting to do parallel computing without protecting your import on a system that does not suppo
rt forking. To use parallel-computing in a script, you must protect you main loop using "if __name__ == '__main__'". Ple
ase see the joblib documentation on Parallel for more information
我的问题是,joblib的windows实现需要在每种情况下保护主循环吗?
如果这是一个超级基本问题,请道歉.我是并行化世界的新手,所以我可能只是缺少一些基本概念,但我无法在任何地方找到这个问题.
最后,我想指出,这纯粹是学术性的;我理解为什么以这种方式编写一个代码是generally good practice,并且无论joblib如何都会继续这样做.