Skip to content

Instantly share code, notes, and snippets.

@FZambia
Created May 24, 2013 16:20
Show Gist options
  • Save FZambia/5644639 to your computer and use it in GitHub Desktop.
Save FZambia/5644639 to your computer and use it in GitHub Desktop.
fetch several resources simultaniously in Tornado
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