How to share numpy random state of a parent process with child processes?

后端 未结 3 2052
旧巷少年郎
旧巷少年郎 2020-11-27 08:20

I set numpy random seed at the beginning of my program. During the program execution I run a function multiple times using multiprocessing.Process. The function

3条回答
  •  没有蜡笔的小新
    2020-11-27 08:22

    You need to update the state of the Manager each time you get a random number:

    import numpy as np
    from multiprocessing import Manager, Pool, Lock
    
    lock = Lock()
    mng = Manager()
    state = mng.list(np.random.get_state())
    
    def get_random(_):
        with lock:
            np.random.set_state(state)
            result = np.random.uniform()
            state[:] = np.random.get_state()
            return result
    
    np.random.seed(1)
    result1 = Pool(10).map(get_random, range(10))
    
    # Compare with non-parallel version
    np.random.seed(1)
    result2 = [np.random.uniform() for _ in range(10)]
    
    # result of Pool.map may be in different order
    assert sorted(result1) == sorted(result2)
    

提交回复
热议问题