Python 函数中的多处理:将 freeze_support() 放在哪里?

Multiprocessing in a Python function: where to put freeze_support()?

我正在尝试使用 Python 2.7 中的 GGS 函数之一(https://github.com/cvxgrp/GGS,我正在尝试使用的函数位于 ggs.py 中,它称为 GGSCrossVal ,第 72 行)但是 Python 向我显示了这个错误:

Attempt to start a new process before the current process has finished its bootstrapping phase. This probably means that you are on Windows 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 a Windows executable.

我必须将此命令放在哪里? .

我生成了一个 2x3740 的数据矩阵,数据取自具有特定均值和特定协方差矩阵的双变量分布(实际上我已经在 MATLAB 中生成它们并导入 Python)并调用函数这样:

trainTestResults = GGSCrossVal(data, 25, [10, 1, 0.1, 0.01, 0.001, 0.0001], [], False)

有人可以帮助我吗? 谢谢

在 windows 中,默认情况下在 macOS 中,“生成”新进程的方式基本上相当于“启动一个新的 python 进程,导入所有相同的模块,导入 'main' 文件作为一个库,然后使用 pickle 来交换要调用的函数和参数是什么”。 *nix 系统上的替代方法是“fork”,其中复制进程内存,新进程从同一点开始。

这里的重要含义是,当使用“生成”“主”文件时,您运行宁不能在 imported 时生成更多的 child 线程。如果是这样,第一个 children 会在 import __main__ 时生成 grandchildren,然后在 import __main__ 时生成 great-grandchildren,并且 [=23] =] 创建无限递归 child 进程。这显然是一个问题,因此如果您在此导入阶段尝试在 child 进程中创建新进程,python 会引发错误。

避免此问题的解决方案是防止任何生成 child 进程的进程在主进程之外执行(这对于 运行 在库上进行测试时也很有用) 运行 作为主进程而不是作为库导入)。

if __name__ == "__main__":
    trainTestResults = GGSCrossVal(data, 25, [10, 1, 0.1, 0.01, 0.001, 0.0001], [], False)