0

I want to run the same function (with some random numbers generated inside it) many times by parallelizing the runs over multiple CPUs. I tried using multiprocessing but it seems like the function is running only once. I wonder if some caching is going on that prevents multiple runs from happening. Consider the following simple example.

import numpy as np
import multiprocessing as mp

p = mp.Pool()
vals = p.map(np.random.normal, [0]*5)
p.close()
print(vals)

This gives the output:

[-2.5729019061921052, -2.5729019061921052, -2.5729019061921052, -2.5729019061921052, -2.5729019061921052]

I want the function to run for every element in the list passed to map. How to disable this "caching" or whatever is going on? This issue doesn't arise when I use the built-in map instead of multiprocessing.

I am using python 3.8.8 on WSL.

2

0

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.