class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@gen.engine
def post(self):
#do some stuff even with mongo motor
self.handleRequest(bla)
@gen.engine
def handleRequest(self,bla):
#do things,use motor call other functions
self.finish(result)
是否所有功能都必须标记为异步? 感谢
答案 0 :(得分:0)
调用完成结束HTTP请求,请参阅docs。其他功能不应称为'完成'
我想你想做这样的事情。请注意,有一个额外的参数'回调'被添加到异步函数中:
@tornado.web.asynchronous
@gen.engine
def post(self):
query =''
response = yield tornado.gen.Task(
self.handleRequest,
query=query
)
result = response[0][0]
errors = response[1]['error']
# Do stuff with result
def handleRequest(self, callback, query):
self.motor['my_collection'].find(query, callback=callback)
有关详细信息,请参阅tornado.gen docs