Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the content from a listproxy

How do I clear a shared multiprocess manager.list? In the example below, I want to clear it before the loop continues so that the new spawned processes find an empty list.

num_consumers = multiprocessing.cpu_count() 
p = multiprocessing.Pool(num_consumers)
manager = multiprocessing.Manager()
mp_list = manager.list()

def put_some_data(data):
    #Processing occurs and then we append the result
    mp_list.append(data)

def do_some_processing():
    While True:
        #Multiprocessing runs here
        result = p.map(put_some_data, data)
        mp_list.clear()
        #When done, break

The above throws the error AttributeError: 'ListProxy' object has no attribute 'clear'

The documentation is not very clear() on how one can clear the proxylist object

like image 327
lukik Avatar asked May 06 '14 15:05

lukik


1 Answers

Here is the way I do it. List comprehension to the rescue!

>>> import multiprocessing
>>> m = multiprocessing.Manager()
>>> lst = m.list()
>>> lst.append(0)
>>> lst.append(1)
>>> lst.append(2)
>>> lst
<ListProxy object, typeid 'list' at 0x3397970>
>>> lst[:]
[0, 1, 2]
>>> lst[:] = [] 
>>> lst
<ListProxy object, typeid 'list' at 0x3397970>
>>> lst[:]
[]
>>>

so

>>> lst[:] = []

does it :)

like image 87
csm10495 Avatar answered Nov 15 '22 18:11

csm10495