我有一个非常大的(只读)数据数组,希望能够并行地由多个进程处理。
我喜欢Pool.map
函数,并希望使用它来并行计算数据上的函数。
我发现可以使用Value
或Array
类在进程之间共享内存数据。但是当我尝试使用这些类时,在使用Pool.map
函数时会出现RuntimeError:'SynchronizedString objects should only be shared between processes through inheritance'
:
以下是我正在尝试做的简化示例:
from sys import stdin
from multiprocessing import Pool, Array
def count_it( arr, key ):
count = 0
for c in arr:
if c == key:
count += 1
return count
if __name__ == '__main__':
testData = "abcabcs bsdfsdf gdfg dffdgdfg sdfsdfsd sdfdsfsdf"
# want to share it using shared memory
toShare = Array('c', testData)
# this works
print count_it( toShare, "a" )
pool = Pool()
# RuntimeError here
print pool.map( count_it, [(toShare,key) for key in ["a", "b", "s", "d"]] )
有人能告诉我这里我做错了什么吗?
我想要做的是,在进程池中创建进程后,将关于新创建的共享内存分配数组的信息传递给这些进程。