我刚刚看到赏金,再次尝试;)
基本上我认为错误消息意味着它说什么 – 多处理共享内存数组不能作为parameter passing(通过酸洗)。 将数据序列化是没有意义的 – 关键是数据是共享内存。 所以你必须使共享数组成为全局的。 我认为把它作为一个模块的属性比较合适,就像在我的第一个答案中一样,但是在你的例子中把它作为一个全局variables也是适用的。 考虑到你不想在fork之前设置数据,这里是一个修改的例子。 如果你想有多个可能的共享数组(这就是为什么你想传递给Share作为参数),你可以类似地创build一个共享数组的全局列表,只要将索引传递给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"] )
[编辑:上述不工作在Windows上,因为不使用分叉。 但是,下面的工作在Windows上,仍然使用池,所以我认为这是最接近你想要的:
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"] )
不知道为什么地图不会pickle数组,但进程和池将 – 我想也许它已经在窗口上的subprocess初始化点传输。 请注意,数据仍然在叉后设置。