【发布时间】:2019-12-29 13:29:54
【问题描述】:
我有这个任务,旨在批量插入或删除数据库中的对象:
views.py
from .tasks import run_task_with
def index():
# some code to retrieve obj_list
run_task_with(insert_obj, obj_list).delay()
return HttpResponseRedirect('/app_root/')
tasks.py
@shared_task
def run_task_with(func, queryset):
cache.add('current_task_id', run_task_with.request.id)
obj_numb = len(queryset)
r = map(func, queryset)
for i, obj in enumerate(r):
sleep(0.1)
progress_percent = int(round(float(i) / float(obj_numb) * 100))
current_task.update_state(
state='PROGRESS',
meta={'progress_percent': progress_percent}
)
但是 run_task_with.request.id 即使在对象插入运行顺利时也会继续返回 None。谁能给我解释一下为什么?
谢谢
【问题讨论】:
标签: django python-3.x celery