tornado 框架 简单的任务异步化方式

来源:互联网 发布:软件登记在哪里办理 编辑:程序博客网 时间:2024/06/05 23:30

tornado 框架 简单的任务异步化方式

由于项目中存在耗时的查询操作,对应api接口常常被阻塞,影响这一块的并发。所以才有了将其转化为异步缓解并发的想法。

@route(r'/ajax/api_interface_demo', name='ajax_GetAreaTree')  # 获取所有地区class ApiInterfaceDemo(BaseHandler):    executor = ThreadPoolExecutor(20)    @asynchronous    @coroutine    def get(self):        type = self.get_argument('type',None)        bi_id = self.get_argument('bi_id', 0)        bi_id = int(bi_id) if bi_id else 0        a = yield self.get_all_area(type, bi_id)    @run_on_executor    def get_all_area(self,type,bi_id):        items = Area.select(Area.id.alias('id'), Area.pid.alias('pid'), Area.name.alias('name'), Area.code.alias('code')).dicts()        bitems = [item.area_code for item in BlockItemArea.select().where(BlockItemArea.block_item == bi_id)]        nodes = [{                'id': item['id'],                'pId': item['pid'] if item['pid'] else 0,                'name': item['name'],                'data': item['code'],                'target': '_top',                'click': '',                'open': 'false',                'checked': 'true' if item['code'] in bitems else 'false'        } for item in items]        url = '/admin/store_area_product?sid=1'        nodes.insert(0, {            'id': 0,            'pId': -1,            'name': '全部',            'data': '',            'target': '_top',            'click': "pop('全部地域-产品信息', '" + url + "');",            'open': 'true'        })        self.write(simplejson.dumps(nodes))
原创粉丝点击