forked from dabeaz/generators
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenqueue.py
More file actions
32 lines (25 loc) · 710 Bytes
/
genqueue.py
File metadata and controls
32 lines (25 loc) · 710 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# genqueue.py
#
# Generate a sequence of items that put onto a queue
def genfrom_queue(thequeue):
while True:
item = thequeue.get()
if item is StopIteration:
break
yield item
def sendto_queue(items, thequeue):
for item in items:
thequeue.put(item)
thequeue.put(StopIteration)
# Example
if __name__ == '__main__':
import queue, threading
def consumer(q):
for item in genfrom_queue(q):
print("Consumed", item)
print("done")
in_q = queue.Queue()
con_thr = threading.Thread(target=consumer,args=(in_q,))
con_thr.start()
# Now, pipe a bunch of data into the queue
sendto_queue(range(100), in_q)