简体   繁体   中英

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

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 ?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM