Problem with multiprocessing

Roy Hyunjin Han patrol at invisibleroads.com
Wed Sep 2 11:12:26 CEST 2009


On 09/02/2009 04:51 AM, Peter Otten wrote:
> tleeuwenburg at gmail.com wrote:
>
>> I have a problem using multiprocessing in a simple way. I created a
>> file, testmp.py, with the following contents:
>>
>> ---------------------------------------------------
>> import multiprocessing as mp
>>
>> p = mp.Pool(5)
>>
>> def f(x):
>>    return x * x
>>
>> print map(f, [1,2,3,4,5])
>> print p.map(f, [1,2,3,4,5])
>
> I'm too lazy to read the docs, so I just tinkered:
>
> import multiprocessing as mp
> import testmp
>
> p = mp.Pool(5)
>
> def f(x):
>    return x * x
>
> if __name__ == "__main__":
>      print map(f, [1,2,3,4,5])
>      print p.map(testmp.f, [1,2,3,4,5])
>
>
Yes, to use the multiprocessing module, you must make your script 
importable, so runtime statements should go into a __main__ 
conditional.  This way, when multiprocessing imports the module for each 
of its threads, the actual runtime code only gets executed once in the 
parent thread, which has the lock.  At least, that is what I think is 
happening.


import multiprocessing as mp

def f(x):
     return x * x

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


http://docs.python.org/library/multiprocessing.html

-- 
http://invisibleroads.com
Connecting Python developers with local businesses




More information about the Python-list mailing list