Decorator after @task decorator in celery
The task decorator does not return a class, it returns an instance.
It seems that your question should really be "How can I have access to the task inside the decorator" rather than how you can apply the decorator first.
In the upcoming 3.1 (development version) you can use bound tasks to accomplish this:
def send_email(fun):
@wraps(fun)
def outer(self, *args, **kwargs):
print('decorated and task is {0!r}'.format(self))
return fun(self, *args, **kwargs)
return outer
@task(bind=True)
@send_email
def any_function(self):
print('inside the function')
For previous versions you can use current_task
:
from celery import current_task
def send_email(fun):
@wraps(fun)
def outer(*args, **kwargs):
print('decorated and task is: %r' % (current_task, ))
return fun(*args, **kwargs)
return outer
@task
@send_email
def any_function():
print('inside the function')
"before" looks like "after" visually.
Eg, this:
@decorator1
@decorator2
@decorator3
def func():
pass
is equivalent to:
def func():
pass
func = decorator1(decorator2(decorator3(func)))
This means you have to write @send_email
after @task
to get it applied before @task
. Eg:
@task
@send_email
def any_function():
print "inside the function"