2

As I have discovered windows is a bit of a pig when it comes to multiprocessing and I have a questions about it.

The pydoc states you should protect the entry point of a windows application when using multiprocessing.

Does this mean only the code which creates the new process?

For example

Script 1

import multiprocessing

def somemethod():
    while True:
        print 'do stuff'

# this will need protecting
p = multiprocessing.Process(target=somemethod).start()

# this wont
if __name__ == '__main__':
    p = multiprocessing.Process(target=somemethod).start()

In this script you need to wrap this in if main because the line in spawning the process. But what about if you had?

Script 2

file1.py

import file2
if __name__ == '__main__':
    p = Aclass().start()

file2.py

import multiprocessing
ITEM = 0
def method1():
    print 'method1'

method1()

class Aclass(multiprocessing.Process):
    def __init__(self):
        print 'Aclass'
        super(Aclass, self).__init__()

    def run(self):
        print 'stuff'

What would need to be protected in this instance? What would happen if there was a if __main__ in File 2, would the code inside of this get executed if a process was being created?

NOTE: I know the code will not compile. It's just an example.

Shravan
  • 2,809
  • 2
  • 18
  • 39
Matt Seymour
  • 8,880
  • 7
  • 60
  • 101
  • Does this answer your question? [Compulsory usage of if \_\_name\_\_=="\_\_main\_\_" in windows while using multiprocessing](https://stackoverflow.com/questions/20360686/compulsory-usage-of-if-name-main-in-windows-while-using-multiprocessi) – imbr Aug 31 '21 at 17:46

1 Answers1

5

The pydoc states you should protect the entry point of a windows application when using multiprocessing.

My interpretation differs: the documentations states

the main module can be safely imported by a new Python interpreter without causing unintended side effects (such a starting a new process).

So importing your module (import mymodule) should not create new processes. That is, you can avoid starting processes by protecting your process-creating code with an

if __name__ == '__main__':
    ...

because the code in the ... will only run when your program is run as main program, that is, when you do

python mymodule.py

or when you run it as an executable, but not when you import the file.

So, to answer your question about the file2: no, you do not need protection because no process is started during the import file2.

Also, if you put an if __name__ == '__main__' in file2.py, it would not run because file2 is imported, not executed as main program.

edit: here is an example of what can happen when you do not protect your process-creating code: it might just loop and create a ton of processes.

Community
  • 1
  • 1
bernard paulus
  • 1,644
  • 1
  • 21
  • 33