【问题标题】:How to get results from celery tasks from another app in another host如何从另一个主机的另一个应用程序中获取 celery 任务的结果
【发布时间】:2014-12-09 12:37:16
【问题描述】:

我正在使用 django、celery 和 rabbitmq 来处理任务,我们称之为 APP1。在另一台主机上,我有 APP2,它需要从 APP1 中处理的任务中获取结果。

两个APP/主机都可以访问rabbitmq,我的第一个方法是简单地尝试从两个APP共享一个队列,但没有成功。

实现这一目标的最佳方法是什么?

【问题讨论】:

    标签: django rabbitmq celery


    【解决方案1】:

    一种可能的方法是让任务在 APP1 上运行,当它处理完任务后,将另一个任务发布到 celery。将此新任务称为ProcessResults。此任务的数据将是原始任务的结果。此新任务的工作人员将位于 APP2 上。

    【讨论】:

      【解决方案2】:

      只需使用您在APP1 中使用的相同结果后端,例如在APP2 中:

      from celery import Celery
      from celery.result import AsyncResult
      
      # set the backend URL that APP1 is using
      app = Celery(backend='backend_url')
      
      # The task ID that was queued in APP1
      task = AsyncResult('task_id')
      
      # get the task result
      task.result
      

      您需要存储来自 APP1 的任务 ID 以便能够在 APP2 中获得其结果,或者如果可以在不存储它的情况下使用自定义任务 ID,但您需要使用 Task.apply_async() 来设置自定义 ID:

      task.apply_async(args, kwargs, task_id='custom_id')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-16
        • 2018-05-15
        • 2020-07-17
        • 1970-01-01
        • 1970-01-01
        • 2018-01-20
        • 1970-01-01
        相关资源
        最近更新 更多