是否有简单的方法可以从同步更改为从Tornado中获取数据库结果的异步方式?

时间:2013-05-07 13:21:56

标签: python python-2.7 tornado

目前我没有异步代码。我的龙卷风成本看起来像

class EmployeeHandler(tornado.web.RequestHandler):
    '''Returns all users which satisfy conditions'''

    def post(self):
        data = tornado.escape.json_decode(self.request.body)
        age = data['age']
        education = data['education']
        result = self._filter(education, age)
        self.write(json.dumps(result))
        self.flush()

    def _filter(self, education, age):
        '''Reads from local database a lot using SQLAlchemy, make joins and is slow'''
        pass

是否有简单的方法可以使这种异步,以异步方式从过滤器中获取结果?

1 个答案:

答案 0 :(得分:0)

当我们看不到_filter函数中的代码时很难回答这个问题,但我会尝试向您建议一些研究方向。

首先,看一下python的generatorsyield keyword

其次,看一下龙卷风的add_callback method,它允许你使用异步方法,甚至可以使用多线程。

相关问题