using class methods as celery tasks using class methods as celery tasks python python

using class methods as celery tasks


Celery has experimental support for using methods as tasks since version 3.0.

The documentation for this is in celery.contrib.methods, and also mentions some caveats you should be aware of:

https://docs.celeryproject.org/en/3.1/reference/celery.contrib.methods.html

Be aware: support for contrib.methods removed from Celery since 4.0


Jeremy Satterfield has a clean and straight forward tutorial to write class based tasks if that's what you want to accomplish. You can check it here.

The magic is basically extending celery.Task class including a run() method, like something like this:

from celery import Taskclass CustomTask(Task):    ignore_result = True    def __init__(self, arg):        self.arg = arg    def run(self):        do_something_with_arg(self.arg)

and then run the task like this:

your_arg = 3custom_task = CustomTask()custom_task.delay(your_arg)

I am not sure if ignore_result = True part is necessary or not.


When you have:

    a = A()

you can do:

    A.foo.delay(a, param0, .., paramN)

Cheers