tornado是有异步处理和长轮询的优势的。
在gen.coroutine出现之前tornado是用web.asynchronous来实现异步的。web.asynchronous是与web服务器建立长连接,直到self.finish()关闭连接。而gen.coroutine使用协程实现类似异步的处理效果,
tornado.gen is a generator-based interface to make it easier to work in an asynchronous environment. Code using the gen module is technically asynchronous, but it is written as a single generator instead of a collection of separate functions.
文档中的例子中说明有gen.coroutine装饰器存在异步变得简单,不再需要回调函数。文档例子:
For example, the following asynchronous handler:
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")
could be written with gen as:
class GenAsyncHandler(RequestHandler):
@gen.coroutine
def get(self):
http_client = AsyncHTTPClient()
response = yield \ http_client.fetch("http://example.com")
do_something_with_response(response)
self.render("template.html")
那web.asynchronous应该可以用来建立长轮询。