[英]Calling function from Tornado async
just struggling on this. 只是在这方面挣扎。 If i have an async request handler that during it's execution calls other functions that do something (for example async db queries) and then they call "finish" on their own, do i have to mark them as async?
如果我有一个异步请求处理程序,在它执行期间调用其他执行某些操作的函数(例如异步数据库查询),然后他们自己调用“完成”,我是否必须将它们标记为异步? because if the application is structured like the example, i get errors about multiple calls to "finish".
因为如果应用程序的结构类似于示例,我会收到有关“完成”的多次调用的错误。 I guess i miss something.
我想我会想念一些东西。
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)
Do all functions have to be marked with async? 是否所有功能都必须标记为异步? thanks
谢谢
Calling finish ends the HTTP request see docs . 调用完成结束HTTP请求,请参阅docs 。 Other functions should not call 'finish'
其他功能不应称为'完成'
I think you want to do something like this. 我想你想做这样的事情。 Note that there is a extra param 'callback' which is added into async functions:
请注意,有一个额外的参数'回调',它被添加到异步函数中:
@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)
See tornado.gen docs for more info 有关详细信息,请参阅tornado.gen文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.