Windows 上的 python 多处理,如果 __name__ == "__main__"

python multiprocessing on windows, if __name__ == quot;__main__quot;(Windows 上的 python 多处理,如果 __name__ == quot;__main__quot;)

本文介绍了Windows 上的 python 多处理,如果 __name__ == "__main__"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows 7(64 位)上运行 python 2.7.

Running python 2.7 on windows 7 (64bit).

在阅读库模块 multiprocessing 的文档时,它多次声明了 __main__ 模块的重要性,包括条件(尤其是在 Windows 中):

When reading the docs for library module multiprocessing, it states several times the importance of the __main__ module, including the conditional (especially in Windows):

if __name__ == "__main__":
    # create Process() here

我的理解是,你不想在模块的全局命名空间中创建 Process() 实例(因为当子进程导入模块时,他会不经意地产生另一个).

My understanding, is that you don't want to create Process() instances in the global namespace of the module (because when the child process imports the module, he will spawn yet another inadvertently).

不过,我不必将流程管理器放在我的包执行层次结构的最顶层(在 PARENT 中执行).只要我的 Process() 是在类方法中创建、管理和终止的,甚至在函数闭包中.只是不在顶级模块命名空间中.

I do not have to place Process managers at the very top level of my package execution hierarchy though (execution in the PARENT). As long as my Process()'s are created, managed, and terminated in a class method, or even in a function closure. Just not in the toplevel module namespace.

我是否正确理解了这个警告/要求?

Am I understanding this warning/requirement correctly?

在前两个回复之后,我添加了这句话.这是 2.7 文档中第 16.6 节多处理的介绍.

After the first two responses, I add this quotation. This is in the introduction for Section 16.6 multiprocessing from the 2.7 docs.

注意:此包中的功能要求子组件可以导入 __main__ 模块.这在编程中有所介绍指南但是值得在这里指出.这意味着一些示例,例如 multiprocessing.Pool 示例将无法在交互式解释器...

Note: Functionality within this package requires that the __main__ module be importable by the children. This is covered in Programming guidelines however it is worth pointing out here.This means that some examples, such as the multiprocessing.Pool examples will not work in the interactive interpreter...

推荐答案

您不必从模块的顶层"调用 Process().从类方法调用 Process 完全没问题.

You do not have to call Process() from the "top level" of the module. It is perfectly fine to call Process from a class method.

唯一需要注意的是,当模块被导入时,您不能允许调用Process().

The only caveat is that you can not allow Process() to be called if or when the module is imported.

由于 Windows 没有 fork,多处理模块会启动一个新的 Python 进程并导入调用模块.如果 Process() 在导入时被调用,那么这会引发无限连续的新进程(或直到您的机器耗尽资源).这就是在

Since Windows has no fork, the multiprocessing module starts a new Python process and imports the calling module. If Process() gets called upon import, then this sets off an infinite succession of new processes (or until your machine runs out of resources). This is the reason for hiding calls to Process() inside

if __name__ == "__main__"

因为这个 if-statement 中的语句不会在导入时被调用.

since statements inside this if-statement will not get called upon import.

这篇关于Windows 上的 python 多处理,如果 __name__ == "__main__"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Windows 上的 python 多处理,如果 __name__ == "__main__"

基础教程推荐