Numpy memmap和多个进程

时间:2015-09-29 08:50:32

标签: python multithreading numpy

无法理解python中线程的行为。我对np.memmap()的线程安全感兴趣,并写了一些例子:

from multiprocessing import Process
import numpy as np
import time

mm = np.memmap("foo", dtype=int, mode = "w+", shape = (10,))

def foo(i):
    while True:
        mm[1]=np.random.randint(low=2, high=50)
        sleepfor = np.random.uniform(low=0.0, high=1.1, size=None)
        print "proces = ", i, " value = ", mm[1], " sleep = ", sleepfor

        time.sleep(sleepfor)

if __name__ == "__main__":
    processes = []
    p1 = Process(target=foo, args=(1,))
    p1.start() 

    p2 = Process(target=foo, args=(2,))
    p2.start()

    p3 = Process(target=foo, args=(3,))
    p3.start()

    p4 = Process(target=foo, args=(4,))
    p4.start()

输出:

proces =  1  value =  13  sleep =  0.91652869794
proces =  2  value =  13  sleep =  0.91652869794
proces =  3  value =  13  sleep =  0.91652869794
proces =  4  value =  13  sleep =  0.91652869794
proces =  1  value =  6  sleep =  0.721062383299
proces =  2  value =  6  sleep =  0.721062383299
proces =  3  value =  6  sleep =  0.721062383299
proces =  4  value =  6  sleep =  0.721062383299
proces =  1  value =  24  sleep =  0.161040327399
proces =  2  value =  24  sleep =  0.161040327399
proces =  3  value =  24  sleep =  0.161040327399
proces =  4  value =  24  sleep =  0.161040327399
proces =  1  value =  22  sleep =  0.837121791675
proces =  2  value =  22  sleep =  0.837121791675
proces =  3  value =  22  sleep =  0.837121791675
proces =  4  value =  22  sleep =  0.837121791675
and so on...

为什么不同进程的随机值相同?

为什么他们每4次迭代改变一次?

还有如何编写测试以检查numpy memmap是否是线程安全的(是吗?)

UPD:

好的,random.seed()解决了第一和第二个问题? 然后我评论sleep()导致编写并发性,也没有发生错误。这是否意味着np.memmap()线程安全?

1 个答案:

答案 0 :(得分:1)

我认为这是np.random的问题,虽然我不知道为什么。 如果您使用mm[1] = random.randint(1, 20),则会解决所有流程中值相同的问题。

>>> proces =  1  value =  17  sleep =  0.0967840686219
>>> proces =  2  value =  13  sleep =  0.0967840686219
>>> proces =  3  value =  19  sleep =  0.0967840686219
>>> proces =  4  value =  14  sleep =  0.0967840686219

使用random.random也解决了sleep变量的相同问题。