繁体   English   中英

从Tornado异步调用函数

[英]Calling function from Tornado async

只是在这方面挣扎。 如果我有一个异步请求处理程序,在它执行期间调用其他执行某些操作的函数(例如异步数据库查询),然后他们自己调用“完成”,我是否必须将它们标记为异步? 因为如果应用程序的结构类似于示例,我会收到有关“完成”的多次调用的错误。 我想我会想念一些东西。

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)

是否所有功能都必须标记为异步? 谢谢

调用完成结束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文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM