MattWritesCode MattWritesCode - 6 months ago 40
Python Question

Windows multiprocessing

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

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

import multiprocessing
ITEM = 0
def method1():
print '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.


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


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, 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.