2013-05-07 30 views
1

目前我没有异步代码。我的龙卷风成本看起来像有没有简单的方法从同步到异步方式从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 

有没有简单的方法来使这个异步,来从过滤后的结果异步?

+0

相关问题:http://stackoverflow.com/questions/10214042/can-sqlalchemy-be-configured-to-be-non-blocking – stalk 2013-05-07 13:31:55

回答

0

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

首先,看看python的generatorsyieldkeyword

其次,看看龙卷风的add_callbackmethod,它允许你使用异步方法,甚至利用多线程。