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?
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]
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