【问题标题】:Shared arrays in multiprocessing Python多处理 Python 中的共享数组
【发布时间】:2023-03-10 02:39:01
【问题描述】:

我正在尝试在并行处理 python 脚本中写入相同的共享数组。

当我在课外使用普通脚本时,一切正常。但是当我尝试通过一个类(使用相同的代码)来完成它时,我得到了
Runtime Error: SynchronizedArray objects should only be shared between processes through inheritance

我的脚本如下(没有类):

import numpy
import ctypes

from multiprocessing import Pool, Array, cpu_count

n = 2

total_costs_matrix_base = Array(ctypes.c_double, n*n)
total_costs_matrix = numpy.ctypeslib.as_array(
                     total_costs_matrix_base.get_obj())
total_costs_matrix = total_costs_matrix.reshape(n,n)


def set_total_costs_matrix( i, j, def_param = total_costs_matrix_base):
    total_costs_matrix[i,j] = i * j

if __name__ == "__main__":

    pool = Pool(processes=cpu_count())
    iterable = []

    for i in range(n):
        for j in range(i+1,n):
            iterable.append((i,j))
    pool.starmap(set_total_costs_matrix, iterable)
    total_costs_matrix.dump('some/path/to/file')

该脚本运行良好。没有的是以下(使用类):

import numpy
import ctypes

from multiprocessing import Pool, Array, cpu_count

class CostComputation(object):
    """Computes the cost matrix."""

    def __init__(self):
        self.n = 2

        self.total_costs_matrix_base = Array(ctypes.c_double, self.n*self.n)
        self.total_costs_matrix = numpy.ctypeslib.as_array(
                             self.total_costs_matrix_base.get_obj())
        self.total_costs_matrix = self.total_costs_matrix.reshape(self.n,self.n)


    def set_total_costs_matrix(self, i, j, def_param = None):
        def_param = self.total_costs_matrix_base
        self.total_costs_matrix[i,j] = i * j


    def write_cost_matrix(self):
        pool = Pool(processes=cpu_count())
        iterable = []

        for i in range(self.n):
            for j in range(i+1,self.n):
                iterable.append((i,j))
        pool.starmap(self.set_total_costs_matrix, iterable)
        self.total_costs_matrix.dump('some/path/to/file')

在此之后,在创建CostComputation 的实例之后,我会从另一个文件中调用write_cost_matrix

我阅读了this answer,但仍然无法解决我的问题。

我在 Mac OSX Yosemite 10.10.4 中使用 Python 3.4.2。

编辑
使用 CostComputation 类时,我使用的脚本是:

from cost_computation import CostComputation

cc = CostComputation()
cc.write_costs_matrix()

整个错误是:

Traceback (most recent call last):
  File "app.py", line 65, in <module>
    cc.write_cost_matrix()
  File "/path/to/cost_computation.py", line 75, in write_cost_matrix
    pool.starmap(self.set_total_costs_matrix, iterable)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/pool.py", line 268, in starmap
    return self._map_async(func, iterable, starmapstar, chunksize).get()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/pool.py", line 599, in get
    raise self._value
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/pool.py", line 383, in _handle_tasks
    put(task)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/connection.py", line 206, in send
    self._send_bytes(ForkingPickler.dumps(obj))
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/reduction.py", line 50, in dumps
    cls(buf, protocol).dump(obj)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/sharedctypes.py", line 192, in __reduce__
    assert_spawning(self)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/context.py", line 347, in assert_spawning
    ' through inheritance' % type(obj).__name__
RuntimeError: SynchronizedArray objects should only be shared between processes through inheritance

【问题讨论】:

  • 究竟在哪里引发了 RuntimeException?我的猜测是您正在从另一个类访问 CostComputation.total_costs_matrix,这会触发此异常。
  • @justhecuke 查看编辑。我没有从不同的类访问total_costs_matrix,但实际上我从类的同一个实例多次访问它(因为多处理)。我想解决方案是下面@ATOzTOA 给出的答案。尽快尝试。
  • 这可能是一个序列化问题。检查您的课程是否可以正确腌制。

标签: python arrays numpy multiprocessing ctypes


【解决方案1】:

尝试创建仅包含共享数据的第二个类。然后在你的主类中使用该类对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 2013-06-26
    • 2011-01-06
    • 2019-10-09
    • 2020-06-12
    相关资源
    最近更新 更多