V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ray1888
V2EX  ›  Python

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

  •  
  •   ray1888 ·
    ray1888 · 2018-07-09 20:25:35 +08:00 · 1841 次点击
    这是一个创建于 2089 天前的主题,其中的信息可能已经有所发展或是发生改变。
    @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, 有什么方法可以异步来获取结果?

    5 条回复    2018-07-13 08:51:06 +08:00
    ray1888
        1
    ray1888  
    OP
       2018-07-09 20:25:54 +08:00
    这里是一个 handler 上面的一个 post 方法
    ray1888
        2
    ray1888  
    OP
       2018-07-09 20:26:56 +08:00
    尝试过在 celery 的 apply_async 添加 callback=self.on_post_result,但是也无法运行收尾的 self.on_post_result
    pabupa
        3
    pabupa  
       2018-07-09 21:51:10 +08:00
    yield future
    ray1888
        4
    ray1888  
    OP
       2018-07-09 22:38:03 +08:00
    @pabupa yield remote_call_result 这里应该没有用吧?因为 celery 的 Result 类是需要用 get 或者 wait 来获取返回值的结果
    supervipcard
        5
    supervipcard  
       2018-07-13 08:51:06 +08:00
    用 future,内部轮询获取结果
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1414 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:47 · PVG 07:47 · LAX 16:47 · JFK 19:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.