tags:

views:

91

answers:

1

I am trying python 2.6 multiprocessing module with this simple code snippet.

from multiprocessing import Pool
p = Pool(5)
def f(x):
    return x*x

print p.map(f, [1,2,3])

But this code cause my OS stopped responding. It looks like the CPU is too busy. What's wrong with my code?

BTW : it seems that multiprocessing module is a bit dangerous. I had to restart my computer.

+6  A: 

You aren't protecting the entry point at all, so each subprocess is trying to start the same map call and so on (into infinity!). Try the following:

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

See this section of the module's documentation.

jkp
@Trent: thats not good :)
jkp
see this: http://stackoverflow.com/questions/1923706/multiprocessing-launching-too-many-instances-of-python-vm
Corey Goldberg