Skip to content

Instantly share code, notes, and snippets.

@mayflaver
Created December 10, 2013 02:42
Show Gist options
  • Save mayflaver/7884973 to your computer and use it in GitHub Desktop.
Save mayflaver/7884973 to your computer and use it in GitHub Desktop.
tornado coroutine return
import tornado.ioloop
import tornado.web
import tornado.gen
import tornado.httpclient
from tornado.platform.caresresolver import CaresResolver
import time
@tornado.gen.coroutine
def test():
client = tornado.httpclient.AsyncHTTPClient()
response = yield client.fetch('http://www.baidu.com')
response = response.body
# do something with the response
raise tornado.gen.Return(response)
class MainHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
response = yield test()
self.write(response)
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment