if __name__==“__main__"的强制使用在 Windows 中使用多处理 [英] Compulsory usage of if __name__=="__main__" in windows while using multiprocessing

查看:22
本文介绍了if __name__==“__main__"的强制使用在 Windows 中使用多处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在windows上使用python中的多处理时,希望能保护程序的入口点.文档说确保新的 Python 解释器可以安全地导入主模块,而不会导致意外的副作用(例如启动新进程)".谁能解释一下这到底是什么意思?

While using multiprocessing in python on windows, it is expected to protect the entry point of the program. The documentation says "Make sure that the main module can be safely imported by a new Python interpreter without causing unintended side effects (such a starting a new process)". Can anyone explain what exactly does this mean ?

推荐答案

对您已经得到的好答案进行一点扩展,如果您了解 Linux-y 系统的功能会有所帮助.他们使用 fork() 生成新进程,这有两个好的后果:

Expanding a bit on the good answer you already got, it helps if you understand what Linux-y systems do. They spawn new processes using fork(), which has two good consequences:

  1. 主程序中存在的所有数据结构对子进程都是可见的.他们实际上处理数据的副本.
  2. 子进程从紧跟在主程序中 fork() 之后的指令开始执行 - 因此任何已在模块中执行的模块级代码将不会再次执行.
  1. All data structures existing in the main program are visible to the child processes. They actually work on copies of the data.
  2. The child processes start executing at the instruction immediately following the fork() in the main program - so any module-level code already executed in the module will not be executed again.

fork() 在 Windows 中是不可能的,因此在 Windows 上,每个模块都由每个子进程重新导入.所以:

fork() isn't possible in Windows, so on Windows each module is imported anew by each child process. So:

  1. 在Windows上,子进程没有可以看到主程序中存在的数据结构;并且,
  2. 所有模块级代码在每个子进程中执行.
  1. On Windows, no data structures existing in the main program are visible to the child processes; and,
  2. All module-level code is executed in each child process.

因此您需要考虑一下您希望只在主程序中执行哪些代码.最明显的例子是,您希望创建子进程的代码仅在主程序中运行 - 因此应该受到 __name__ == '__main__' 的保护.举一个更微妙的例子,考虑构建一个巨大列表的代码,您打算将其传递给工作进程以爬行.您可能也想保护它,因为在这种情况下没有必要让每个工作进程浪费 RAM 和时间来构建他们自己无用的巨大列表副本.

So you need to think a bit about which code you want executed only in the main program. The most obvious example is that you want code that creates child processes to run only in the main program - so that should be protected by __name__ == '__main__'. For a subtler example, consider code that builds a gigantic list, which you intend to pass out to worker processes to crawl over. You probably want to protect that too, because there's no point in this case to make each worker process waste RAM and time building their own useless copies of the gigantic list.

请注意,即使在 Linux-y 系统上适当地使用 __name__ == "__main__" 也是一个好主意,因为它使预期的工作分工更清晰.并行程序可能会令人困惑 - 每一点都有帮助 ;-)

Note that it's a Good Idea to use __name__ == "__main__" appropriately even on Linux-y systems, because it makes the intended division of work clearer. Parallel programs can be confusing - every little bit helps ;-)

这篇关于if __name__==“__main__"的强制使用在 Windows 中使用多处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆