Skip to content

Instantly share code, notes, and snippets.

@gwillem
Created August 14, 2015 09:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gwillem/bbbfb0b372fac2dd4012 to your computer and use it in GitHub Desktop.
Save gwillem/bbbfb0b372fac2dd4012 to your computer and use it in GitHub Desktop.
import tornado
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.httpclient import AsyncHTTPClient
from tornado.queues import Queue
q = Queue(maxsize=0)
@gen.coroutine
def producer():
for x in range(10):
print("Populating the queue with %s" % x)
yield q.put("item %s" % x)
@gen.coroutine
def consumer():
while True:
x = yield q.get()
print("Got %s from queue" % x)
# wait ~3 secs for response
url = 'http://shop.byte.nl/sleep.php'
client = tornado.httpclient.AsyncHTTPClient()
resp = yield client.fetch(url)
q.task_done()
@gen.coroutine
def main():
# Start consumer without waiting (since it never finishes).
IOLoop.current().spawn_callback(consumer)
yield producer() # Wait for producer to put all tasks.
yield q.join() # Wait for consumer to finish all tasks.
print('Done')
if __name__ == '__main__':
IOLoop.current().run_sync(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment