Django - Celery:@transaction 和 @task 不叠加

发布于 2024-12-04 03:47:19 字数 608 浏览 4 评论 0原文

我想运行带有手动事务管理的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2024-12-11 03:47:19

以前 Celery 有一些魔力,其中一组默认关键字参数
如果任务接受了它们,则将其传递给该任务。

从 2.2 版开始,您可以禁用此行为,但最简单的方法是
celery.task 而不是 celery.decorators 导入 task 装饰器:

from celery.task import task

@task
@transaction.commit_manually
def t():
    pass

decorators 模块已被弃用,并将被在3.0中完全删除,
和“魔法关键字参数”相同

注意:
对于自定义任务类,您应该将 accept_magic_kwargs 属性设置为 False:

class MyTask(Task):
    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 from celery.task instead of celery.decorators:

from celery.task import task

@task
@transaction.commit_manually
def t():
    pass

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:

class MyTask(Task):
    accept_magic_kwargs = 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.

宛菡 2024-12-11 03:47:19

任务装饰器从您的函数生成一个 class x(Task) ,并将 run 方法作为您的目标。建议您定义类并装饰方法。

未经测试例如:

class pong(Task):
  ignore_result = True

  @transaction.commit_manually()
  def run(self,arg,**kwargs):
    print 'pong: %s' % arg
    transaction.rollback()

The task decorator generates a class x(Task) from your function with the run method as your target. Suggest you define the class and decorate the method.

Untested e.g.:

class pong(Task):
  ignore_result = True

  @transaction.commit_manually()
  def run(self,arg,**kwargs):
    print 'pong: %s' % arg
    transaction.rollback()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文