mardi 20 août 2019

Python: How to fix random seed inside process?

Here is example code:

import random
from multiprocessing import Process

random.seed(2019)

def f2():
    print('from f2:')
    v = random.randint(0, 10)
    a = random.randint(0, 10)
    print('v:', v)
    return v, a


def python_process_test():
    n_workers = 2
    workers = []
    for i in range(n_workers):
        p = Process(target=f2, args=())
        p.start()
        workers.append(p)

    for p in workers:
        p.join()


if __name__ == '__main__':
    python_process_test()

Output:

run 1:

from f2:
v: 6
from f2:
v: 4

run 2:

from f2:
v: 0
from f2:
v: 5




Aucun commentaire:

Enregistrer un commentaire