Skip to content

Instantly share code, notes, and snippets.

@asvetlov
Forked from anonymous/gist:10250831
Last active August 29, 2015 13:58
Show Gist options
  • Save asvetlov/10267944 to your computer and use it in GitHub Desktop.
Save asvetlov/10267944 to your computer and use it in GitHub Desktop.
import asyncio
@asyncio.coroutine
def sender(queue):
print('sender started')
try:
yield from asyncio.sleep(3)
while True:
data = yield from queue.get()
print('get', data)
except Exception as e:
print(e)
@asyncio.coroutine
def process(queue):
print('process started')
while True:
yield from asyncio.sleep(1)
print('put, count', queue.qsize())
queue.put_nowait('hello')
def main():
# loop = asyncio.new_event_loop()
# asyncio.set_event_loop(loop)
loop = asyncio.get_event_loop()
queue = asyncio.Queue()
asyncio.async(process(queue))
asyncio.async(sender(queue))
loop.run_forever()
main()
process started
sender started
put, count 0
get hello
put, count 0
get hello
put, count 0
get hello
put, count 0
get hello
put, count 0
get hello
put, count 0
get hello
put, count 0
get hello
put, count 0
get hello
put, count 0
get hello
put, count 0
get hello
put, count 0
get hello
put, count 0
process started
sender started
put, count 0
put, count 1
get hello
get hello
put, count 0
put, count 0
put, count 1
put, count 2
put, count 3
put, count 4
put, count 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment