Python multiprocessing example does nothing

KiRtAp2 Source

I'm trying to learn python multiprocessing, but when I try to run the following example code from the documentation, nothing happens. Nothing is printed, no exceptions, nothing.

from multiprocessing import Process

def f(name):
    print('hello', name)

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

However, the other example works just as intended:

from multiprocessing import Pool

def f(x):
    return x*x

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

I've checked to see if it's an enviroment-specific problem and now I'm even more confused. If I run the first example I showed in IDLE, it doesn't work. If I run it in the interactive interpreter in the command prompt, it also doesn't work. But if I run a .py file with the same code with the command prompt, it works. Does anybody know what's actually happening and why running the same code yields different results?

(I'm currently on a Windows 10 machine if that's important)

pythonpython-3.xmultiprocessing

Answers

answered 6 months ago ShadowRanger #1

IDLE has exceptionally weird behaviors due to replacing sys.stdout and company; interactive or not, you can end up with some strange behaviors.

multiprocessing in general doesn't work very well in the interactive interpreter, particularly on Windows where the "fork simulation" can't really simulate it properly.

Stick to testing multiprocessing related stuff in actual scripts.

comments powered by Disqus