Skip to content

Instantly share code, notes, and snippets.

@contee213
Created January 29, 2015 11:24
Show Gist options
  • Save contee213/85aa7f07696585c11f9d to your computer and use it in GitHub Desktop.
Save contee213/85aa7f07696585c11f9d to your computer and use it in GitHub Desktop.
asyncio producer consumer sample
# -*- coding: utf-8 -*-
__author__ = 'contee'
"""
asyncio producer-consumer
~~~~~~~~~~~~~~
"""
import asyncio, random
q = asyncio.Queue(maxsize=10)
@asyncio.coroutine
def produce():
while True:
i = random.randint(5,10)
print(q.qsize())
yield from q.put(i)
yield from asyncio.sleep(2)
@asyncio.coroutine
def consume():
while True:
value = yield from q.get()
yield from asyncio.sleep(value)
print("Consumed", value)
loop = asyncio.get_event_loop()
asyncio.async(produce())
for x in range(3):
asyncio.async(consume())
loop.run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment