【发布时间】:2019-03-18 11:17:11
【问题描述】:
我设法找到了 2 个类似的主题来讨论这个问题,但不幸的是我无法从中得到最好的解决方案:
- Update Django Model Field Based On Celery Task Status
- Update Django Model Field Based On Celery Task Status
我使用 Django 和 Celery(+redis 作为消息代理),我想在 celery 任务状态发生变化(从挂起 -> 成功,挂起 -> 失败)等时更新 django 模型。
我的代码:
import time
from celery import shared_task
@shared_task(name="run_simulation")
def run_simulation(simulation_id: str):
t1_start = time.perf_counter()
doSomeWork() # we may change this to sleep for instance
t1_end = time.perf_counter()
return{'process_time': t1_end - t1_start}
以及我从中调用任务的特定视图:
def run_simulation(request):
form = SimulationForm(request.POST)
if form.is_valid():
new_simulation = form.save()
new_simulation.save()
task_id = tasks.run_simulation.delay(new_simulation.id)
问题是,当任务状态发生变化时,更新模拟的 django 模型状态的首选方法是什么?
在文档中,我发现处理程序正在使用 on_failure、on_success 等方法。http://docs.celeryproject.org/en/latest/userguide/tasks.html#handlers
【问题讨论】:
标签: python django redis celery