Django - Celery:@transaction 和 @task 不叠加
我想运行带有手动事务管理的 Django - Celery 任务,但注释似乎不堆叠。
例如
def ping():
print 'ping'
pong.delay('arg')
@task(ignore_result=True)
@transaction.commit_manually()
def pong(arg):
print 'pong: %s' % arg
transaction.rollback()
,结果是,
TypeError: pong() got an unexpected keyword argument 'task_name'
而反向注释顺序结果是,
---> 22 pong.delay('arg')
AttributeError: 'function' object has no attribute 'delay'
这是有道理的,但我很难找到一个好的解决方法。 Django 文档没有提到注释的替代方案,并且当我不需要时,我不想为每个 celery 任务创建一个类。
有什么想法吗?
I want to run a Django - Celery task with manual transaction management, but it seems that the annotations do not stack.
e.g.
def ping():
print 'ping'
pong.delay('arg')
@task(ignore_result=True)
@transaction.commit_manually()
def pong(arg):
print 'pong: %s' % arg
transaction.rollback()
results in
TypeError: pong() got an unexpected keyword argument 'task_name'
while the reverse annotation order results in
---> 22 pong.delay('arg')
AttributeError: 'function' object has no attribute 'delay'
It makes sense, but I'm having trouble finding a nice workaround. The Django docs don't mention alternatives to the annotation, and I don't want to make a class for each celery Task when I don't need one.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以前 Celery 有一些魔力,其中一组默认关键字参数
如果任务接受了它们,则将其传递给该任务。
从 2.2 版开始,您可以禁用此行为,但最简单的方法是
从
celery.task
而不是celery.decorators
导入task
装饰器:decorators
模块已被弃用,并将被在3.0中完全删除,和“魔法关键字参数”相同
注意:
对于自定义任务类,您应该将
accept_magic_kwargs
属性设置为 False:注意 2:确保您的自定义装饰器使用
functools.wraps
保留函数的名称,否则任务将结束起错名字了。Previously Celery had some magic where a set of default keyword arguments
were passed to the task if it accepted them.
Since version 2.2 you can disable this behaviour, but the easiest is to
import the
task
decorator fromcelery.task
instead ofcelery.decorators
:The
decorators
module is deprecated and will be completely removed in 3.0,and the same for the "magic keyword arguments"
Note:
For custom Task classes you should set the
accept_magic_kwargs
attribute to False:Note2: Make sure your custom decorators preserves the name of the function using
functools.wraps
, otherwise the task will end up with the wrong name.任务装饰器从您的函数生成一个
class x(Task)
,并将run
方法作为您的目标。建议您定义类并装饰方法。未经测试例如:
The task decorator generates a
class x(Task)
from your function with therun
method as your target. Suggest you define the class and decorate the method.Untested e.g.: