Created
August 14, 2015 09:10
-
-
Save gwillem/bbbfb0b372fac2dd4012 to your computer and use it in GitHub Desktop.
This file contains 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
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