Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get chunks of elements from a queue?

I have a queue from which I need to get chunks of 10 entries and put them in a list, which is then processed further. The code below works (the "processed further" is, in the example, just print the list out).

import multiprocessing

# this is an example of the actual queue
q = multiprocessing.Queue()
for i in range(22):
    q.put(i)
q.put("END")

counter = 0
mylist = list()
while True:
    v = q.get()
    if v == "END":
        # outputs the incomplete (< 10 elements) list
        print(mylist)
        break
    else:
        mylist.append(v)
        counter += 1
        if counter % 10 == 0:
            print(mylist)
            # empty the list
            mylist = list()

# this outputs
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
# [20, 21]

This code is ugly. I do not see how to improve it - I read some time ago how to use iter with a sentinel but fail to see how my problem could make use of it.

Is there a better (= more elegant/pythonic) way to solve the problem?

like image 339
WoJ Avatar asked Nov 09 '22 09:11

WoJ


1 Answers

You could use iter twice: iter(q.get, 'END') returns an iterator which can iterate over the values in the queue until 'END' is returned by q.get().

Then you could use the grouper recipe

iter(lambda: list(IT.islice(iterator, 10)), [])

to group the iterator into chunks of 10 items.

import itertools as IT
import multiprocessing as mp

q = mp.Queue()
for i in range(22):
    q.put(i)
q.put("END")

iterator = iter(q.get, 'END')
for chunk in iter(lambda: list(IT.islice(iterator, 10)), []):
    print(chunk)

yields

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[20, 21]
like image 102
unutbu Avatar answered Nov 14 '22 21:11

unutbu