即使使用 .close() 和 .join() 池也会产生 'local variable referenced before assignment' 错误

pool yields 'local variable referenced before assignment' error even with .close() and .join()

我编写的这个函数在它自己的 jupyter notebook 中调用时运行正常,但是当我将 notebook 保存为 .py 文件并从另一个 notebook 调用它时,出现以下错误:

UnboundLocalError: 赋值前引用了局部变量'results'

    def get_results(inputs):
        if __name__ == '__main__':
            with multiprocessing.Pool() as pool:
                results = pool.starmap(aaa.dostuff, inputs)
                pool.close()
                pool.join()
        return results

我认为 pool.close() 后跟 pool.join() 负责命令执行的顺序,不知道该怎么做。

您需要从函数中删除 if __name__ == '__main__': 测试。在使用 multiprocessing 模块时,您可能误解了它的用途。

相反,将此 放在模块级别 ,并将对 get_results() 的调用放在受其保护的块中:

def get_results(inputs):
    with multiprocessing.Pool() as pool:
        results = pool.starmap(aaa.dostuff, inputs)
        pool.close()
        pool.join()
    return results

if __name__ == '__main__':
    # ... other code you have to produce 'inputs', that needs to run *before*
    # starting the pool of child processes, goes here ...

    results = get_results(inputs)

    # do something with the results.

if __name__ 测试除了在模块的顶层之外很少在其他地方使用。全局变量 __name__ 在导入模块时由 Python 设置。只有当您 运行 您的文件作为脚本(使用 python path/to/file.py)时,Python 才会将该变量设置为字符串 "__main__",这是一个方便的值来测试何时您想 运行 仅当当前模块是起点时才编码。

根据您的 OS 和配置,当使用 multiprocessing Python 时,会创建新的、独立的子进程,这些子进程以 Python 解释器状态的副本开始,或使用专门为 运行 多处理工作代码设计的专用多处理脚本。您自己的脚本不再是主要起点,__name__ 的值将不同。

你的情况出了什么问题,你几乎可以肯定在工作进程中调用了 get_results() ,其中 __name__ 设置为其他内容。那时你的函数执行两行:

  • if __name__ == '__main__': - 结果False,因此该块被跳过
  • return result - 错误!没有为此本地名称设置值,因此它仍未绑定。