Feedback requested: Add Task.enqueue_on_commit() to Django’s Tasks API

Hi everyone,

I opened a new-features issue proposing a small convenience method for Django’s Tasks API:

The proposal is to add Task.enqueue_on_commit() as a first-class shortcut for the pattern currently documented in the Tasks framework docs:

transaction.on_commit(partial(my_task.enqueue, thing_num=1))

The goal is to make the transaction-safe enqueueing pattern easier to discover and harder to forget when a task depends on database state created or updated in the current transaction.

This would not change Task.enqueue() semantics. It would only add an explicit opt-in method:

with transaction.atomic():
    Thing.objects.create(num=1)
    my_task.enqueue_on_commit(thing_num=1)

The proposed method would return None, because the real TaskResult is not available until the transaction commits.

Prior art:

  • Celery provides delay_on_commit() and apply_async_on_commit() on DjangoTask.
  • The django-tasks backport/reference implementation also has transaction-aware enqueueing behavior.

I’d appreciate feedback on the API shape, especially:

  • Is enqueue_on_commit() the right name?
  • Should this remain intentionally small and omit using= / robust=?
  • Is returning None the least surprising behavior?

For simple support or opposition, please use reactions on the GitHub issue so the new-features process can track them cleanly.

3 Likes