简体   繁体   English

python中celery组任务全部完成后如何调用方法

[英]How to call the method after all the celery group tasks are completed in python

Currently I am doing the celery group task and I want to call upload_local_directory(output_file)method after all the tasks completed.目前我正在做 celery 组任务,我想在所有任务完成后调用 upload_local_directory(output_file) 方法。 I tried below approach but its not waiting the job to be completed.我尝试了以下方法,但它没有等待工作完成。

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 method is the celery job method. make_tmp_files方法是 celery 作业方法。

“backend” also defined in the celery object. “后端”也在 celery object 中定义。

please comment If more information needed.如果需要更多信息,请发表评论。

You either chain your group and the final, make_tmp_files task, or you use Chord to accomplish the same.您可以链接您的组和最终的make_tmp_files任务,或者使用Chord来完成相同的任务。 Do not be alarmed if you see that Celery automatically converts group+task chain into a Chord.如果您看到 Celery 自动将组+任务链转换为 Chord,请不要惊慌。

Within celery there is a difference between a group of tasks and a group of task results. celery内部有一组任务和一组任务结果的区别。 If you were to change your code to the following it should work:如果您要将代码更改为以下内容,它应该可以工作:

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() returns a group of AsyncResults. job.apply_async()返回一组 AsyncResults。 As a user, you need to check the result of AsyncResult, not the tasks themselves.作为用户,您需要检查 AsyncResult 的结果,而不是任务本身。

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

Hopefully that helps!希望这会有所帮助!

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

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