当我从 Django 中的 View 发送一个已编程的任务时,如果我需要在第二时刻撤销,我需要立即获取 task.id。
例如我的观点:
@login_required
def program_task(request, pk):
member = get_object_or_404(Post_Blog, pk=pk)
if request.method == "POST":
form = PostProgrammaForm(request.POST)
if form.is_valid():
action = form.save(commit=False)
action.account_id = member.pk
action.programmed = 1
year= action.dataprogrammato.year
month= action.dataprogrammato.month
day= action.dataprogrammato.day
hourz = action.oraprogrammato.hour
hour= oraz - 2 ##this is for timezone :D
minute= action.oraprogrammato.minute
quando = datetime(year, month, day, hour, minute)
action.save()
if action.programmed == True:
tasksend.apply_async(args=(action.id), eta=quando)
### here it send task and work, but I need to know here the task.id
return redirect('blog_action', pk=member.pk)
else:
form = PostProgrammaForm(request.POST)
return render(request, 'FBIsystem/post_program.html', {'form': form, 'member':member})
现在,我的 celery py 是这样的:
app = Celery()
@app.task(bind=True)
def tasksend(self, action_id):
###do somethig
一切正常,我保存表格并发送任务,但是...... 如果我为明天编写一篇文章,并且需要在运行之前撤销任务,我该怎么办?
请帮忙 谢谢
最佳答案
apply_async
返回具有 id
celery.result.AsyncResult
任务实例
task = tasksend.apply_async(args=(action.id), eta=quando)
print(task.id)
关于python - 立即打印 Celery for Python 中已编程任务的任务 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57668960/