admin管理员组文章数量:1191950
Approach 1: Without transaction.atomic
Source: /
def order_created(order):
order.status = 'created'
order.save()
transaction.on_commit(
partial(send_order_email.delay, order_id=order.id)
)
Approach 2: With transaction.atomic
def order_created(order):
with transaction.atomic():
order.status = 'created'
order.save()
transaction.on_commit(
partial(send_order_email.delay, order_id=order.id)
)
Which approach is correct and why? The save() operation makes database changes, but examples differ on whether transaction.atomic is needed. (ATOMIC_REQUESTS=False in settings)
Approach 1: Without transaction.atomic
Source: https://adamj.eu/tech/2022/08/22/use-partial-with-djangos-transaction-on-commit/
def order_created(order):
order.status = 'created'
order.save()
transaction.on_commit(
partial(send_order_email.delay, order_id=order.id)
)
Approach 2: With transaction.atomic
def order_created(order):
with transaction.atomic():
order.status = 'created'
order.save()
transaction.on_commit(
partial(send_order_email.delay, order_id=order.id)
)
Which approach is correct and why? The save() operation makes database changes, but examples differ on whether transaction.atomic is needed. (ATOMIC_REQUESTS=False in settings)
Share Improve this question asked Jan 24 at 7:09 swaranswaran 1159 bronze badges1 Answer
Reset to default 0In the first example transaction.on_commit
will result in error if there is no transaction open (related docs), plus save()
will be executed right away so there is no need for on_commit
at all.
In second example, Celery task will be queued at the end of successful transaction. As I understand your question, this is expected behaviour
本文标签: pythonWhat39s the correct way to use transactiononcommit with Celery tasks in DjangoStack Overflow
版权声明:本文标题:python - What's the correct way to use transaction.on_commit with Celery tasks in Django? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738441167a2086963.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论