1

I'm having a problem getting different return values from a pool.map call. Im running this simplified example on a macbook, in a jupyter notebook with python 3.9:

import multiprocessing as mp
import numpy as np

def gen_random_list(num):
    print(f"Num: {num}\n")
    ret_list = np.random.randint(0, 5, size=3).tolist()
    return ret_list
    
with mp.Pool() as pool:
    results = []
    results = pool.map(gen_random_list, range(4))
    print(results)

I keep getting the same list back for each call to gen_random_list. The complete output is this:

Num: 0
Num: 1
Num: 2
Num: 3




[[0, 3, 2], [0, 3, 2], [0, 3, 2], [0, 3, 2]]

Does anyone know why every call to gen_random_list is returning the same values (in this run: [0, 3, 2])? And how do I fix this so I get a different random list back each time?

Thanks Brett

3
  • The code as written does not run unless I put the multiprocessing part inside of if __name__ == '__main__'. Once I do that, I cannot reproduce your results, I just got [[0, 1, 2], [2, 0, 2], [2, 4, 3], [2, 1, 0]]. Commented Aug 9, 2022 at 15:38
  • It works as written in a jupyter notebook. The check for main, won't work in a notebook, since the notebook isn't the main process. Commented Aug 9, 2022 at 15:54
  • 1
    This is probably because the processes use the same seed to generate the numbers. Relevant stackoverflow.com/questions/73214631/… Commented Aug 9, 2022 at 17:13

1 Answer 1

0

Thanks @Charchit! That was it! Subtle for sure.... Here's a working example now:

import multiprocessing as mp
import numpy as np

def gen_random_list2(num):
    print(f"Num: {num}\n")
    # https://stackoverflow.com/questions/22994423/difference-between-np-random-seed-and-np-random-randomstate
    local_state = np.random.RandomState(num)
    ret_list = local_state.randint(0, 5, size=3).tolist()
    return ret_list
    
with mp.Pool(processes=processes) as pool:
    results = list(map(gen_random_list2, range(100)))
    print(results)
Sign up to request clarification or add additional context in comments.

Comments

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.