python 多处理:共享位数组(位数组 0.8.1)

python multiprocessing: sharing bitarray (bitarray 0.8.1)

我想分享一个 3 GB 的位数组 (https://pypi.python.org/pypi/bitarray/0.8.1) between threads created by multiprocessing (https://docs.python.org/2/library/multiprocessing.html)。

我只想读取位数组而不修改它。下面的python2.7代码真的可以吗?不知何故,它似​​乎在不使用 ctypes (docs.python.org/2/library/ctypes.html).

的情况下工作
import multiprocessing as mp
import bitarray
import time
def f(x):
    n = 0
    #print shared_arr[n:(10+n)] & shared_arr[n:(10+n)]
    print "worker %d started at time %s" % (x, str(time.time()-start_time))
    print "running %d. bit %d of shared array is: " % (x, x) +str(shared_arr[n:(10+n)])
    time.sleep(2)
    print "ending %d at time %s" %(x, str(time.time()-start_time))
    return x*x

def main():
    print "The number of cpu is %d" % (mp.cpu_count())
    num_cpu_core = mp.cpu_count()
    n = 0
    global shared_arr
    global start_time
    start_time = time.time()
    shared_arr = bitarray.bitarray(18)
    shared_arr[:] = 0
    shared_arr[(n+5):(n+7)] = 1
    a = 10
    pool = mp.Pool(processes = num_cpu_core) # not saving one for the master process
    pool.map_async(f, range(10))
    pool.close()
    pool.join()

main()

您的脚本不可导入。

替换最后一行:

main()

以下两行:

if __name__ == "__main__":
    main()

这将在使用 fork 语义的 POSIX 系统上工作 multiprocessing,但不适用于使用 spawn 语义的 Windows 系统。 fork 语义将相同的内存映射到 parent 和 child(copy-on-write,因此如果其中一个发生更改,另一个中的数据将保持不变); spawn 语义启动新的 Python 进程。

此外,side-note,关于 Windows,我想你想要一个导入保护来避免 "fork bomb" 之类的场景,不要在模块级别调用 main无条件地,但保护它:

if __name__ == '__main__':
    main()

因此,当生成的 child 将主模块导入为 "not main" 时,它不会尝试重新调用您的 main 函数。