Reporting yielded results of long-running Celery task

前端 未结 5 1044
你的背包
你的背包 2020-11-27 06:07

Problem

I\'ve segmented a long-running task into logical subtasks, so I can report the results of each subtask as it completes. However, I\'m trying to report the

5条回答
  •  日久生厌
    2020-11-27 06:38

    In order for Celery to know what the current state of the task is, it sets some metadata in whatever result backend you have. You can piggy-back on that to store other kinds of metadata.

    def yielder():
        for i in range(2**100):
            yield i
    
    @task
    def report_progress():
        for progress in yielder():
            # set current progress on the task
            report_progress.backend.mark_as_started(
                report_progress.request.id,
                progress=progress)
    
    def view_function(request):
        task_id = request.session['task_id']
        task = AsyncResult(task_id)
        progress = task.info['progress']
        # do something with your current progress
    

    I wouldn't throw a ton of data in there, but it works well for tracking the progress of a long-running task.

提交回复
热议问题