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) listprint(mylist)breakelse:mylist.append(v)counter += 1if counter % 10 == 0:print(mylist)# empty the listmylist = 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?