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
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With