tornado 使用 corotine 如何获取 celery 返回值

2018-07-09 20:25:35 +08:00
 ray1888
@tornado.web.asynchronous
    @tornado.gen.coroutine
    @auth_token
    @log_record
    def post(self):
        """
        bussiness_name
        description
        :return:
        """
        resultCode = self.request.headers.get('resultCode', -1)
        resultData = self.request.headers.get('resultData', {})
        user_id = resultData['user_id']
        token = None
        data = {}
        body = json.loads(self.request.body)
        data['business_name'] = body['name']
        data['system_user_id'] = body['user_id']
        data['description'] = body['description']
        remote_call_result = get_business_result.apply_async(args=["POST", user_id, data])
        # response = remote_call_result.wait()
        # self.on_post_result(response)
        response = yield tornado.gen.Task(remote_call_result.wait())
        self.on_post_result(response)


    def on_post_result(self, response):
        di_result = json.dumps(response)
        logger.info("verify, result:%s \n" % di_result)
        self.write(json.dumps(di_result))
        self.finish()

代码如上,使用 yield gen.Task 无法获取获得的 response,celery 任务是一个 rpc call, 有什么方法可以异步来获取结果?

1865 次点击
所在节点    Python
5 条回复
ray1888
2018-07-09 20:25:54 +08:00
这里是一个 handler 上面的一个 post 方法
ray1888
2018-07-09 20:26:56 +08:00
尝试过在 celery 的 apply_async 添加 callback=self.on_post_result,但是也无法运行收尾的 self.on_post_result
pabupa
2018-07-09 21:51:10 +08:00
yield future
ray1888
2018-07-09 22:38:03 +08:00
@pabupa yield remote_call_result 这里应该没有用吧?因为 celery 的 Result 类是需要用 get 或者 wait 来获取返回值的结果
supervipcard
2018-07-13 08:51:06 +08:00
用 future,内部轮询获取结果

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/469377

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX