Created
June 1, 2016 10:38
-
-
Save Winand/73c0ae3c3c710fec9ebd36b57776c13b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio, itertools | |
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] | |
def grouper(iterable, n): | |
"Collect data into fixed-len chunks: stackoverflow.com/a/31185097/1119602" | |
it = iter(iterable) | |
return iter(lambda: list(itertools.islice(it, n)), []) | |
@asyncio.coroutine | |
def produce(q): | |
for i in grouper(data, 4): | |
#do some work | |
for j in i: | |
yield from q.put(j) | |
print("put", j) | |
# yield from asyncio.sleep(0.1) | |
@asyncio.coroutine | |
def consume(q): | |
while True: | |
print(" get>") | |
value = yield from q.get() | |
print(" <get", value) | |
queue = asyncio.Queue() | |
loop = asyncio.get_event_loop() | |
loop.create_task(consume(queue)) | |
loop.run_until_complete(produce(queue)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment