Search code examples
pythonpython-3.xpython-multiprocessing

AttributeError: 'ListProxy' object has no attribute 'copy'


Python 3.5

I am trying to parallelize the following code and to do so I am using a ListProxy object from the multiprocessing module, so that workers accessing the list do so in a managed way. I understand that the Proxy objects from the multiprocessing module are not iterable but you are able to call the copy() function on the objects to return the regular version of that object, i.e copy() on a DictProxy returns a regular Dict which you can iterate through.

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

However, every time I call copy() on the list object I get the following error:

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'

Solution

  • If you do dir(subgraphList) you can see the possible methods that a LitProxy object has

    ['_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']
    

    as you can see it has a deepcopy method that you can use

    subgraphList.__deepcopy__({})
    

    also deepcopy ensure it copies even nested items