如何制作异步龙卷风处理程序?
How to make an asynchronous tornado handler?
我一直在尝试制作一个异步龙卷风处理程序,但还没有成功。
我阅读并尝试了文档中的 examples。
这是文档示例:
# localhost:8888/test
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")
我替换了这部分:
response = yield http_client.fetch("http://example.com")
有了这个:
response = yield gen.Task(self.test())
其中 self.test()
代码是:
def test(callback):
while(True):
pass
我有另一个 IndexHandler 应该只打印 hello async
,但它不起作用,因为 self.test()
中的无限循环似乎阻止了请求据我所知,这意味着它不是异步的。
有什么帮助吗?
Takes a function (and optional additional arguments) and runs it with
those arguments plus a callback keyword argument.
http://tornado.readthedocs.org/en/latest/gen.html#tornado.gen.Task
我一直在尝试制作一个异步龙卷风处理程序,但还没有成功。
我阅读并尝试了文档中的 examples。
这是文档示例:
# localhost:8888/test
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")
我替换了这部分:
response = yield http_client.fetch("http://example.com")
有了这个:
response = yield gen.Task(self.test())
其中 self.test()
代码是:
def test(callback):
while(True):
pass
我有另一个 IndexHandler 应该只打印 hello async
,但它不起作用,因为 self.test()
中的无限循环似乎阻止了请求据我所知,这意味着它不是异步的。
有什么帮助吗?
Takes a function (and optional additional arguments) and runs it with those arguments plus a callback keyword argument.
http://tornado.readthedocs.org/en/latest/gen.html#tornado.gen.Task