刚刚在看tornado的gen模块http://www.tornadoweb.org/documentati...
class AsyncHandler(RequestHandler):
@asynchronous
def get(self):
http_client = AsyncHTTPClient()
http_client.fetch("http://example.com",
callback=self.on_fetch)
def on_fetch(self, response):
do_something_with_response(response)
self.render("template.html")
可以用gen模块优化成
class GenAsyncHandler(RequestHandler):
@asynchronous
@gen.engine
def get(self):
http_client = AsyncHTTPClient()
response = yield gen.Task(http_client.fetch, "http://example.com")
do_something_with_response(response)
self.render("template.html")
ok, 现在书抄完了, 开始提问
我使用 tornado.httpclient.HTTPClient
http_client = httpclient.HTTPClient()
try:
response = http_client.fetch("http://www.google.com/")
print response.body
except httpclient.HTTPError, e:
print "Error:", e
也可以实现同步的代码风格, 实际上应该也是异步执行, 因为用到了IOLoop
我的问题是
1. tornado.httpclient.HTTPClient 用在生产环境有问题嘛?
2. 两种写法达到的效果是不是一样的, 如果不一样, 有什么区别?