1

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 ?

Xingdong
  • 1,305
  • 2
  • 13
  • 18
  • 2
    Which documentation are you referring to (could you link to it)? In any case, `if __name__ == '__main__'` is used so that the code wont execute when importing your library. – Ecir Hana Jun 10 '18 at 06:51
  • Xingdong, make sure you're familiar with everything explained here: https://stackoverflow.com/questions/419163/what-does-if-name-main-do – David Z Jun 10 '18 at 06:55
  • @EcirHana In this [official documentation](https://docs.python.org/3.7/library/multiprocessing.html), it shows all examples with `if __name__ == '__main__'`. Do you mean it is okay to put `if __name__ == '__main__'` within a class method and even though we run a python script (e.g. `python file1.py --num_processes 5`) which internally create the class and call this method ? – Xingdong Jun 10 '18 at 06:56

2 Answers2

3

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.

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26
1

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.

Ecir Hana
  • 10,864
  • 13
  • 67
  • 117