Asynchronous Tornado调用回调函数,无需等待结果
我有一个使用Tornado的服务器代码:Asynchronous Tornado调用回调函数,无需等待结果,asynchronous,tornado,asynccallback,Asynchronous,Tornado,Asynccallback,我有一个使用Tornado的服务器代码: class mHandle(tornado.web.RequestHandler): @gen.coroutine def process(self, data): yield gen.Task(tornado.ioloop.IOLoop.instance().add_timeout, time.time() + 3) @tornado.web.asynchronous @gen.corou
class mHandle(tornado.web.RequestHandler):
@gen.coroutine
def process(self, data):
yield gen.Task(tornado.ioloop.IOLoop.instance().add_timeout, time.time() + 3)
@tornado.web.asynchronous
@gen.coroutine
def get(self):
_data = self.get_argument('data', default='')
yield gen.Task(self.process, _data)
self.write("OK")
现在,我使用浏览器进入localhost,它将等待3秒钟,然后打印结果“OK”。
我不在乎结果,如何编码让浏览器立即打印“OK”,而不必等待3秒钟
谢谢 (这里的内存即将耗尽)
self.process返回未来,因此您可以执行以下简单操作:
@tornado.web.asynchronous
@gen.coroutine
def get(self):
_data = self.get_argument('data', default='')
ioloop.add_future(self.process(_data), self.process_complete)
self.write("OK")
def process_complete(self, future):
"""Handle the error/success from the future"""
您可能应该执行一个
self.finish(“OK”)
,因为这将关闭异步。谢谢您,我已经使用过了,而且看起来效果很好。我将进一步了解这个问题。谢谢