Django - Celery: @transaction and @task don't stack

I want to run a Django - Celery task with manual transaction management, but it seems that annotations are not stacking.

eg.

def ping():
    print 'ping'
    pong.delay('arg')

@task(ignore_result=True)
@transaction.commit_manually()
def pong(arg):
    print 'pong: %s' % arg
    transaction.rollback()

leads to

TypeError: pong() got an unexpected keyword argument 'task_name'

while the reverse order of annotation leads to

---> 22     pong.delay('arg')

AttributeError: 'function' object has no attribute 'delay'

It makes sense, but it's hard for me to find a good workaround. Django docs don't mention alternate annotations, and I don't want to create a class for every Celery task when I don't need it.

Any ideas?

+5
source share
2 answers

Celery previously had some kind of magic where the default set of keyword arguments were passed to the task if it accepted them.

2.2 , task celery.task celery.decorators:

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, .

+8

class x(Task) run . .

, :

class pong(Task):
  ignore_result = True

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

All Articles