Skip to content

Instantly share code, notes, and snippets.

@Winand
Created June 1, 2016 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Winand/73c0ae3c3c710fec9ebd36b57776c13b to your computer and use it in GitHub Desktop.
Save Winand/73c0ae3c3c710fec9ebd36b57776c13b to your computer and use it in GitHub Desktop.
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