5

I have got a chunk of code like

for i in range(0, len(a))
    b[i] = func(a[i])

where a and b are arrays of the same length, a is given (and big), func is some function that has a lot of local variables but does not use any global variables.

I would like to distribute computations of func across several CPUs. Presumably I need to use multiprocessing module, but I have not found any relevant examples. Could you help? Thanks.

4
  • Which Python interpreter you are using? Note that if using CPython, you need to be aware of GIL: wiki.python.org/moin/GlobalInterpreterLock Commented Jun 20, 2012 at 11:37
  • 1
    @jsalonen: That's why he needs the multiprocessing module. Commented Jun 20, 2012 at 11:39
  • If you are using linux, you can use fork. ikharn.blogspot.in/2012/04/multiprocessing-in-python.html Commented Jun 20, 2012 at 11:48
  • @Nikhil: Using fork is problematic because it doesn't have an easy way of getting results back to the parent process. Commented Jun 21, 2012 at 2:33

2 Answers 2

3

See the very first code example in the multiprocessing docs:

from multiprocessing import Pool

# you could define `func`, `a` here

if __name__=="__main__":
    p = Pool() # use all available CPU cores
    b = p.map(func, a)
Sign up to request clarification or add additional context in comments.

Comments

1

Use process pool. You can see a full sample in my github: https://github.com/mariazverina/codejam/blob/master/src/main.py

from multiprocessing import Pool

p = Pool(4)  # set to number of cores
b = p.map(func, a)

2 Comments

good point - brain -> fingers malfunction - updated explanation above
on Windows if __name__=="__main__" is mandatory when using multiprocessing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.