質問

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 :)

他のヒント

Just for reference

del mp_list[:]

and

mp_list *= 0

also works but the accepted answer is probably the cleanest solution.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top