gpt4 book ai didi

python - 在 Python 多处理中将 Pool.map 与共享内存数组结合起来

转载 作者:IT老高 更新时间:2023-10-28 20:30:02 27 4
gpt4 key购买 nike

我有一个非常大的(只读)数据数组,希望由多个进程并行处理。

我喜欢 Pool.map 函数,并希望使用它来并行计算该数据上的函数。

我看到可以使用 ValueArray 类在进程之间使用共享内存数据。但是当我尝试使用它时,我得到一个 RuntimeError: 'SynchronizedString objects should only be shared between processes through inheritance when using the Pool.map function:

这是我正在尝试做的一个简化示例:

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"]] )

谁能告诉我我在这里做错了什么?

所以我想做的是在进程池中创建进程后将有关新创建的共享内存分配数组的信息传递给进程。

最佳答案

刚看到赏金就再试一次;)

基本上我认为错误消息的意思是 - 多处理共享内存数组不能作为参数传递(通过酸洗)。序列化数据没有意义——关键是数据是共享内存。所以你必须使共享数组成为全局的。我认为将它作为模块的属性更简洁,就像在我的第一个答案中一样,但是在您的示例中将其作为全局变量也可以很好地工作。考虑到您不想在 fork 之前设置数据的观点,这是一个修改后的示例。如果您想拥有多个可能的共享数组(这就是您想将 toShare 作为参数传递的原因),您可以类似地创建一个共享数组的全局列表,并将索引传递给 count_it (这将成为 for c in toShare[i]:)。

from sys import stdin
from multiprocessing import Pool, Array, Process

def count_it( key ):
count = 0
for c in toShare:
if c == key:
count += 1
return count

if __name__ == '__main__':
# allocate shared array - want lock=False in this case since we
# aren't writing to it and want to allow multiple processes to access
# at the same time - I think with lock=True there would be little or
# no speedup
maxLength = 50
toShare = Array('c', maxLength, lock=False)

# fork
pool = Pool()

# can set data after fork
testData = "abcabcs bsdfsdf gdfg dffdgdfg sdfsdfsd sdfdsfsdf"
if len(testData) > maxLength:
raise ValueError, "Shared array too small to hold data"
toShare[:len(testData)] = testData

print pool.map( count_it, ["a", "b", "s", "d"] )

[编辑:由于不使用 fork,以上内容在 Windows 上不起作用。但是,以下在 Windows 上确实有效,仍然使用 Pool,所以我认为这是最接近您想要的:

from sys import stdin
from multiprocessing import Pool, Array, Process
import mymodule

def count_it( key ):
count = 0
for c in mymodule.toShare:
if c == key:
count += 1
return count

def initProcess(share):
mymodule.toShare = share

if __name__ == '__main__':
# allocate shared array - want lock=False in this case since we
# aren't writing to it and want to allow multiple processes to access
# at the same time - I think with lock=True there would be little or
# no speedup
maxLength = 50
toShare = Array('c', maxLength, lock=False)

# fork
pool = Pool(initializer=initProcess,initargs=(toShare,))

# can set data after fork
testData = "abcabcs bsdfsdf gdfg dffdgdfg sdfsdfsd sdfdsfsdf"
if len(testData) > maxLength:
raise ValueError, "Shared array too small to hold data"
toShare[:len(testData)] = testData

print pool.map( count_it, ["a", "b", "s", "d"] )

不知道为什么 map 不会 Pickle 数组,但 Process 和 Pool 会 - 我想它可能已经在 windows 上的子进程初始化时转移了。请注意,数据仍然是在 fork 之后设置的。

关于python - 在 Python 多处理中将 Pool.map 与共享内存数组结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1675766/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com