【问题标题】:How to call the method after all the celery group tasks are completed in python在python中完成所有celery group任务后如何调用方法
【发布时间】:2021-03-12 07:18:20
【问题描述】:

目前我正在做 celery group 任务,我想在所有任务完成后调用 upload_local_directory(output_file) 方法。我尝试了以下方法,但它没有等待工作完成。

tasks = [make_tmp_files.s(page.object_list, path + str(uuid.uuid4() + '.csv')) for page in paginator]
job = group(tasks)
job.apply_async()
job.get()

output_file = 'final.zip'
upload_local_directory_into_S3(output_file)

make_tmp_files方法是celery job方法。

“后端”也在 celery 对象中定义。

如果需要更多信息,请发表评论。

【问题讨论】:

    标签: python python-3.x multiprocessing celery celery-task


    【解决方案1】:

    您可以chain 您的小组和最终的make_tmp_files 任务,或者您使用Chord 来完成相同的任务。如果您看到 Celery 自动将组+任务链转换为 Chord,请不要惊慌。

    【讨论】:

    • 带回调的和弦将对此有所帮助..谢谢:)
    【解决方案2】:

    在 celery 中,一组任务和一组任务结果是有区别的。如果您要将代码更改为以下内容,它应该可以工作:

    tasks = [make_tmp_files.s(page.object_list, path + str(uuid.uuid4() + '.csv')) for page in paginator]
    job = group(tasks)
    job_results = job.apply_async()
    job_results.get()
    
    output_file = 'final.zip'
    upload_local_directory_into_S3(output_file)
    

    job.apply_async() 返回一组 AsyncResults。作为用户,您需要检查 AsyncResult 的结果,而不是任务本身。

    参考:https://docs.celeryproject.org/en/stable/userguide/canvas.html#groups

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      相关资源
      最近更新 更多