In Python multiprocessing.Process , do we have to use `__name__ == __main__`?

安稳与你 提交于 2019-12-10 11:39:16

问题


I am writing a class that supports easy-to-use API to add different settings to run a given program (class.add(args)) and to benchmark all settings with multiprocessing (class.benchmark(num_processes=5)).

From the documentation of multiprocessing.Process, it seems all cases using if __name__ == '__main__'. Is it safe to skip using it ?

For example, the class method benchmark(num_processes=5) starts and joins processes, and another python file file.py creates a class and simply call class.benchmark(num_processes=5). Will it work as usual ?


回答1:


As described in the multiprocessing guidelines under the heading "Safe importing of main module", some forms of multiprocessing need to import your main module and thus your program may run amok in a fork bomb if the __name__ == '__main__' check is missing. In particular, this is the case on Windows where CPython cannot fork. So it is not safe to skip it. The test belongs at the top (global) level of your module, not inside some class. Its purpose is to stop the module from automatically running tasks (as opposed to defining classes, functions etc) when it is imported, as opposed to run directly.




回答2:


if __name__ == '__main__': is used to indicate which code to run when the module is loaded. Basically, it is loaded either when you run it as a script or when you import it as a library. In the first case, one usually writes it so that all of the written code executes so it is not necessary to include it. But when you are writing a library, there might by some code which you don't wont to run when other people import it, such as a short example or tests. So in the later case, you definitely want to include it.

To answer you question from the comments above, I don't think it makes sense to include it in a class method, as it is top-level construct and so it loads always.



来源:https://stackoverflow.com/questions/50781216/in-python-multiprocessing-process-do-we-have-to-use-name-main

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!