So I wish to create a process using the python multiprocessing module, I want it be part of a larger script. (I also want a lot of other things from it but right now I will settle for this)
I copied the most basic code from the multiprocessing docs and modified it slightly
However, everything outside of the if __name__ == '__main__':
statement gets repeated every time p.join() is called.
This is my code:
from multiprocessing import Processdata = 'The Data'
print(data)# worker function definition
def f(p_num):print('Doing Process: {}'.format(p_num))print('start of name == main ')if __name__ == '__main__':print('Creating process')p = Process(target=f, args=(data,))print('Process made')p.start()print('process started')p.join()print('process joined')print('script finished')
This is what I expected:
The Data
start of name == main
Creating process
Process made
process started
Doing Process: The Data
process joined
script finishedProcess finished with exit code 0
This is the reality:
The Data
start of name == main
Creating process
Process made
process started
The Data <- wrongly repeated line
start of name == main <- wrongly repeated line
script finished <- wrongly executed early line
Doing Process: The Data
process joined
script finishedProcess finished with exit code 0
I am not sure whether this is caused by the if
statement or p.join()
or something else and by extension why this is happening. Can some one please explain what caused this and why?
For clarity because some people cannot replicate my problem but I have; I am using Windows Server 2012 R2 Datacenter and I am using python 3.5.3.