给出代码:
from multiprocessing import Process import numpy as np class NN(Process): def __init__(self, id): super(NN, self).__init__() self.id = id def run(self): super(NN, self).run() print(np.random.random(5)) print(np.random.random(5)) np.random.seed(1111) print(np.random.random(5)) print(np.random.random(5)) print("="*30) np.random.seed(1111) ps = [NN(i) for i in range(1)] for p in ps: p.start() for p in ps: p.join()
运行结果:
========================================
给出对比代码:
from multiprocessing import Process import numpy as np import time class NN(Process): def __init__(self, id): super(NN, self).__init__() self.id = id def run(self): super(NN, self).run() time.sleep(10) print(np.random.random(5)) np.random.seed(1111) print(np.random.random(5)) ps = [NN(i) for i in range(1)] for p in ps: p.start() np.random.seed(1111) for p in ps: p.join()
运行结果:
-------------------------------------------------------------------
PS:
可以看到,在python生成多进程时会copy父进程中的numpy.random的状态,这其中也包括随机种子的状态; 如果子进程生成完成后,那么父进程中的numpy.random的状态是不会影响子进程的。
========================================================
标签:__,NN,python,self,random,print,seed,np From: https://www.cnblogs.com/devilmaycry812839668/p/17693440.html