Search code examples
pythonmultithreadingmultiprocessingexecution

Python Multiprocessing: How do I keep it within the specified function?


I've been a long-term observer of Stack Overflow but this time I just can't find a solution to my problem, so here I am asking you directly!

Consider this code:

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    p = Pool(5)
    print(p.map(f, [1, 2, 3]))

print "External"

It's the basic example code for multiprocessing with pools as found here in the first box, plus a print statement at the end.

When executing this in PyCharm Community on Windows 7, Python 2.7, the Pool part works fine, but "External" is printed multiple times, too. As a result, when I try to use Multithreading on a specific function in another program, all processes end up running the entire program. How do I prevent that, so only the given function is multiprocessed?

I tried using Process instead, closing, joining and/or terminating the process or pool, embedding the entire thing into a function, calling said function from a different file (it then starts executing that file). I can't find anything related to my problem and feel like I'm missing something very simple.


Solution

  • Since the print instruction is not indented, it is executed each time the python file is imported. Which is, every time a new process is created.

    On the opposite, all the code placed beneath if __name__ == '__main__ will not be executed each time a process is created, but only from the main process, which is the only place where the instruction evaluates to true.

    Try the following code, you should not see the issue again. You should expect to see External printed to the console only once.

    from multiprocessing import Pool
    
    def f(x):
        return x*x
    
    if __name__ == '__main__':
        p = Pool(5)
        print(p.map(f, [1, 2, 3]))
    
        print "External"
    

    Related: python multiprocessing on windows, if __name__ == "__main__"