AttributeError:“ ListProxy”对象没有属性“ copy”

时间:2019-03-07 07:18:04

标签: python python-3.x python-multiprocessing

Python 3.5

我正在尝试并行化下面的代码,并为此使用多处理模块中的ListProxy对象,以便访问列表的工作人员以托管方式进行操作。我知道多重处理模块中的Proxy对象不是可迭代的,但是您可以在对象上调用copy()函数以返回该对象的常规版本,即DictProxy上的copy()返回常规Dict,您可以遍历。

def getAllLabels(graph: UndirectedGraph, graphSize: int, probabilityList: list) -> list:
'''Enumerate through ESU algorithm and retrieve all subgraphs of a given size for this graph.'''

process_manager = multiprocessing.Manager()
pool = multiprocessing.Pool(4)
subgraphList = process_manager.list() #looped
exclusionList = list() #looped

for i in range(0, graph.getSize()):
    nodeList = list([i])
    neighborList = list() #looped
    for n in graph.getAdjacencyList(i):
        if n not in exclusionList and n not in nodeList and n not in neighborList:
            neighborList.append(n)

    #pool.apply_async(getSubgraphs, [graph, graphSize, nodeList, neighborList, exclusionList, subgraphList, probabilityList])
    getSubgraphs(graph, graphSize, nodeList, neighborList, exclusionList, subgraphList.copy(), probabilityList)
    exclusionList.append(i)

labels = list()
for g in subgraphList.copy():
    labels.append(nodesToGraph(graph, g))

pool.close()
return labels

但是,每次我在列表对象上调用copy()时,都会出现以下错误:

Traceback (most recent call last):
File "src/network_motifs.py", line 10, in <module>
subgraphs = esu.getAllLabels(graph, 4, [1, 1, 1, 1])
File "/home/erik/Git/nemolib-python-library/src/nemolib/esu/esu.py", line 39, in    getAllLabels
getSubgraphs(graph, graphSize, nodeList, neighborList, exclusionList, subgraphList.copy(), probabilityList)
AttributeError: 'ListProxy' object has no attribute 'copy'

2 个答案:

答案 0 :(得分:1)

如果执行dir(subgraphList),则可以看到LitProxy对象具有的可能方法

['_Client', '__add__', '__builtins__', '__class__', '__contains__', '__deepcopy__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_address_to_local', '_after_fork', '_authkey', '_callmethod', '_close', '_connect', '_decref', '_exposed_', '_getvalue', '_id', '_idset', '_incref', '_manager', '_mutex', '_owned_by_manager', '_serializer', '_tls', '_token', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

如您所见,它具有可使用的Deepcopy方法

subgraphList.__deepcopy__({})

也可以进行深度复制,以确保复制甚至嵌套的项目

答案 1 :(得分:0)

尝试使用

subgraphList[:]

而不是subgraphList.copy(),因为您的subgraphList可能没有以这种方式实现.copy()方法。

否则,选择:

import copy
newsubgraphList = copy.copy(subgraphList)