Skip to content

Terminate running task #165

@geracobo

Description

@geracobo

Is there any way to terminate a task while it is running?
On this code example, I have a task that I would like to terminate due to user input:

# On my tasks
@task()
def task_that_modifies_database(model_id):
    result = long_running_api_call()
    model = Model.objects.get(model_id)
    model.value = result
    model.save()

# On my views
def user_has_cancelled(request):
    running_task = task_that_modifies_database(task_id)
    running_task.terminate()

    model = Model.object.get(model_id)
    model.value = "" # We clean the value of our model, but if task continues to run it will override.
    model.save()

    return redirect("/")

If the task is not terminated, it will override model.value. I could maybe create a flag that the task checks before saving but that's not ideal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    tasks-apiIssues relating to the tasks API and general API contract from the library

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions