【问题标题】:Celery task calling another task芹菜任务调用另一个任务
【发布时间】:2014-08-01 20:22:25
【问题描述】:

我有两个文件。一个是我的程序的主文件,它包含所有必须完成的芹菜任务:

chord(
    tasks.task_01.subtask(task_id='task_01'),
    tasks.task_02.subtask(task_id='task_02')
).delay()

然后我有一个task.py文件:

@task(bind=True)
def task_01(self, result=None):

    headers = models.Header.objects.all()
    group(extract_emails.subtask((header,)) for header in headers).delay()

最后是 extract_emails:

@task(bind=True)
def extract_emails(header, result=None):

    print header.id  #to check in celery log if the header item is recieved
    url_parser.find_emails(header)

所以我的目标是执行 task_01,以便它运行一组 'extract_emails' 任务与作为参数的 'header' 并行。 我希望 'extract_emails' 任务能够收到此标头并使用它运行一些简单的代码。

当我尝试这样做时,我得到: AttributeError("'extract_emails' 对象没有属性 'id'",)

它是从哪里来的??我什至没有将任务名称作为参数传递!我的代码有什么问题?

【问题讨论】:

    标签: python celery django-celery


    【解决方案1】:

    在我的特定示例中,“extract_emails”任务应该这样写:

    @task(bind=True)
    def extract_emails(self, result=None):
    
        header = self.request.args[1] 
        url_parser.find_emails(header)
    

    只需使用 self.request.PARAM 来提取您需要的内容。 您可以在official docs 找到更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-14
      • 1970-01-01
      • 2016-07-21
      • 2014-12-04
      • 2016-08-19
      • 2018-07-16
      • 1970-01-01
      • 2012-12-01
      相关资源
      最近更新 更多