Created
May 24, 2013 16:20
-
-
Save FZambia/5644639 to your computer and use it in GitHub Desktop.
fetch several resources simultaniously in Tornado
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
from __future__ import print_function | |
import tornado.httpclient | |
import tornado.ioloop | |
import tornado.web | |
import tornado.gen | |
@tornado.gen.coroutine | |
def get(): | |
urls = [ | |
"http://google.com/", | |
"http://BROKEN_URL.google.com", | |
"http://google.com/" | |
] | |
http_client = tornado.httpclient.AsyncHTTPClient() | |
keys = [] | |
for i, url in enumerate(urls): | |
key = "key-%s" % str(i) | |
keys.append(key) | |
http_client.fetch( | |
tornado.httpclient.HTTPRequest(url, method='GET'), | |
callback=(yield tornado.gen.Callback(key)) | |
) | |
responses = yield tornado.gen.WaitAll(keys) | |
print(responses) | |
print(len(responses)) | |
tornado.ioloop.IOLoop.instance().stop() | |
tornado.ioloop.IOLoop.instance().add_callback(get) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment