Created
January 29, 2015 11:24
-
-
Save contee213/85aa7f07696585c11f9d to your computer and use it in GitHub Desktop.
asyncio producer consumer sample
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
# -*- 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