立即打印Celery for Python中已编程任务的任务ID

时间:2019-08-27 06:42:28

标签: python django celery

当我从Django的视图中发送已编程的Task时,如果需要第二次撤消,我需要立即获取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})

现在,我的芹菜py像这样:

app = Celery()
@app.task(bind=True)
def tasksend(self, action_id):
    ###do somethig

一切正常,我保存表格并发送任务,但是... 如果我为明天编写了一篇帖子,而我需要在任务运行前撤销任务,该怎么办?

请帮助 谢谢

1 个答案:

答案 0 :(得分:2)

apply_async返回具有celery.result.AsyncResult的{​​{1}}任务实例

id