简体   繁体   English

芹菜任务调用另一个任务

[英]Celery task calling another task

I have two files. 我有两个文件。 One is the main file of my programm and it has all celery tasks that have to be done: 一个是我程序的主文件,它具有所有必须完成的芹菜任务:

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

Then I have a task.py file: 然后我有一个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()

And finally the extract_emails taks: 最后是extract_emails taks:

@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)

So my goal is to execute task_01 so that it runs a set of 'extract_emails' tasks in parallel with the 'header' as an argument. 因此,我的目标是执行task_01,使其与“ header”作为参数并行运行一组“ extract_emails”任务。 I expect 'extract_emails' task to recieve this header and run some simple code with it. 我希望'extract_emails'任务能够接收到此标头并使用它运行一些简单的代码。

When Im trying to do it I get: AttributeError("'extract_emails' object has no attribute 'id'",) 当我尝试这样做时,我得到:AttributeError(“'extract_emails'对象没有属性'id'”,)

Where does it come from?? 它从何而来?? Im not even passing the name of the task as an argument! 我什至没有传递任务名称作为参数! What is wrong with my code? 我的代码有什么问题?

In my particular example 'extract_emails' task should be written this way: 在我的特定示例中,“ extract_emails”任务应以这种方式编写:

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

    header = self.request.args[1] 
    url_parser.find_emails(header)

Just use self.request.PARAM to extract what you need. 只需使用self.request.PARAM即可提取您所需的内容。 More information you can find in official docs . 您可以在官方文档中找到更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM