Test if a celery task is still being processed

后端 未结 2 1719
余生分开走
余生分开走 2021-02-08 02:20

How can I test if a task (task_id) is still processed in celery? I have the following scenario:

  1. Start a task in a Django view
  2. Store the BaseAsyncResult in
2条回答
  •  别跟我提以往
    2021-02-08 02:41

    I think there is a better way than to store a task object in the model. For example, in case you wanted to check if a group of task (parallel) have completed:

    # in the script you launch the task
    from celery import group
    
    job = group(
        task1.s(param1, param2),
        task2.s(param3, param4)
    )
    result = job.apply_async()
    result.save()
    
    # save the result ID in your model
    your_obj_model = YourModel.objects.get(id='1234')
    your_obj_model.task_id = result.id
    your_obj_model.save()
    

    Then in your view

    from celery.result import GroupResult
    # ...
    task_result = GroupResult.restore(your_obj_model.task_id)
    task_finished = task_result.ready()
    # will be True or False
    

提交回复
热议问题