0

I'm currently writing a program that utilizes the multiprocessing tool in Python.

When I try to return the result from the function that utilizes multiprocessing, and then print it, it will first attempt to print it and prints None. However, once it runs through the multiprocessing function and process, it will print again with the valid answers. This is odd, and I have figured out a way around it, but I'm curious as to if someone has a solid answer to why this happens.

Below is the function I use for multiprocessing.

def ParallelProc:

    if __name__ == '__main__':

        core = 7

        p = mp.Pool(core)
        solutions = p.map(calc, NEW_IC)

        return solutions

Then I try to run it and set it to a variable, and print it like below

sol = ParallelProc()
print sol

My only guess is that it runs the multiprocessing in the background and moves the code forward, trying to print with nothing returned yet. Then once ParallelProc converges, it starts again from that point.

This is an easy work around seen below

sol = ParallelProc()
if sol != None:
    print sol

Any ideas as to what is going on?

FYI I'm running this code through the windows command prompt.

Mofi
  • 46,139
  • 17
  • 80
  • 143
Greg Castaldi
  • 355
  • 1
  • 4
  • 11
  • The code you posted won't run. Can you post your actual code? – Blender Aug 26 '16 at 02:09
  • if __name__ == '__main__': Should not be put inside a function. Its purpose is to avoid running code when importing a module. See http://stackoverflow.com/questions/419163/what-does-if-name-main-do . As Blender said, can you post a working minimal example so we can reproduce the error? – CoMartel Aug 26 '16 at 09:27
  • Please post your actual code and the exception you are seeing. Seeing what you have, I would expect a `SyntaxError`. – Mad Physicist Aug 29 '16 at 16:54

0 Answers0