When delegating work to concurrent/asynchronous execution (tasks, workers, background processes), ensure: 1) State ordering: the async code cannot observe missing/partial state. For DB-backed workflows, enqueue/start tasks only after the transaction commits. 2) Lifecycle safety: concurrent worker processes must be cleaned up when the parent/master process exits so you don’t leave orphaned workers running.
Example (transaction commit before enqueue):
from django.db import transaction
def create_article_and_process(request):
article = Article(...) # fill fields
with transaction.atomic():
article.save()
def enqueue_task():
process_article.delay(article.pk)
transaction.on_commit(enqueue_task)
return response_ok()
Checklist:
transaction.on_commit) or equivalent “after commit” mechanism.Enter the URL of a public GitHub repository