Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I iterate a (multiprocessing) manager list?

I have an array consisting of manager lists like:

[<ListProxy object, typeid 'list' at 0x177d350>, <ListProxy object, typeid 'list' at 0x177d390>, ...]

and try to convert it to a pure list. My consideration looks like:

y=[]
for i in my_manager_list_content:
    for j in i:
        y.append(float(j))
print y #This works

print [next(iter(i[:]))  for i in my_manager_list_content] #Why doesn't this work?

How could I do this in a oneliner?

like image 763
Caniko Avatar asked Oct 30 '22 15:10

Caniko


1 Answers

Explanation on why your codes don't work. iter() takes an iterable object and returns an interator. next() takes the iterator and returns the next element. In your code, when you take an element out of your my_manager_list_content, you only get the first element out of it with next and then you move on the the second element of the my_manager_list_content. This can be illustrated by the codes below: (an expansion on @Kevin Guan 's solution)

from multiprocessing import Process, Manager

def f(ll, l):
    for ii in range(3):
        ll.append(l)

if __name__ == '__main__':
    with Manager() as manager:
        ll = manager.list()
        l = manager.list(range(3))
        p = Process(target=f, args=(ll, l))
        p.start()
        p.join()

        print([float(ii) for jj in ll for ii in jj])
        print([next(iter(ii)) for ii in ll])

And I get

[0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0]
[0, 0, 0]
like image 62
user3667217 Avatar answered Nov 08 '22 08:11

user3667217