芹菜和交易原子 [英] Celery and transaction.atomic

查看:80
本文介绍了芹菜和交易原子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些Django视图中,我使用了这种模式将更改保存到模型中,然后根据新的模型数据进行一些异步更新(例如生成图像,进一步更改模型)。 mytask 是一个芹菜任务:

In some Django views, I used a pattern like this to save changes to a model, and then to do some asynchronous updating (such as generating images, further altering the model) based on the new model data. mytask is a celery task:

with transaction.atomic():
    mymodel.save()
    mytask.delay(mymodel.id).get()

问题在于任务永不返回。查看芹菜的日志,该任务会排队(我在日志中看到已接收任务),但从未完成。如果将 mytask.delay ... get 调用从事务中移出,它将成功完成。

The problem is that the task never returns. Looking at celery's logs, the task gets queued (I see "Received task" in the log), but it never completes. If I move the mytask.delay...get call out of the transaction, it completes successfully.

transaction.atomic 和芹菜之间有什么不兼容?对于我来说,在Django 1.6或1.7中是否可以同时进行常规模型更新和一次任务下来自单独任务过程的更新?

Is there some incompatibility between transaction.atomic and celery? Is it possible in Django 1.6 or 1.7 for me to have both regular model updates and updates from a separate task process under one transaction?

我的数据库是PostgreSQL 9.1。我正在使用celery == 3.1.16 / django-celery 3.1.16,amqp == 1.4.6,Django == 1.6.7,kombu == 3.0.23。代理后端是amqp,并且rabitmq作为队列。

My database is postgresql 9.1. I'm using celery==3.1.16 / django-celery 3.1.16, amqp==1.4.6, Django==1.6.7, kombu==3.0.23. The broker backend is amqp, and rabitmq as the queue.

推荐答案

单独的任务 =由工作人员运行的任务。

"Separate task" = something that is ran by a worker.

芹菜工人 =另一个过程。

"Celery worker" = another process.

我不知道有什么方法可以让您拥有在两个或多个进程之间共享的单个数据库事务。您想要的是在该事务中以同步方式运行任务,然后等待结果……但是,如果这是您想要的,为什么仍然需要任务队列?

I am not aware of any method that would let you have a single database transaction shared between 2 or more processess. What you want is to run the task in a synchronous way, in that transaction, and wait for the result... but, if that's what you want, why do you need a task queue anyway?

这篇关于芹菜和交易原子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆