Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subclassing celery Task

I'm writing alot of tasks that are very similar, and want to know how to better subclass the Task to reduce boilerplate. Since a Task is only instatiated once, I you can't put things in __init__ like I show below, but it should illustrate the point.

what I'm trying to accomplish:

class EmailTaskOne(Task):
    def run(self, object_id):
        email_data = EmailData.objects.get(pk=object_id)
        data = self.do_common_stuff(email_data)
        self.do_unique_stuff(data)

class EmailTaskTwo(Task):
    def run(self, object_id):
        email_data = EmailData.objects.get(pk=object_id)
        data = self.do_common_stuff(email_data)
        self.do_unique_stuff2(data)

# lots more tasks like this

What I would like to have is:

class BaseEmailTask(Task):
     abstract = True
     #...Insert Magic Here...

class EmailTaskOne(BaseEmailTask):
     def run(self, object_id):
         self.do_unique_stuff(self.data)

So, since __init__ is right out, where do I setup the class in the abstract class. I can define a bunch of functions quite easily if all I want to do is factor out some stuff, but some (lots) of the boilerplate depends on the object_id.

like image 858
yarbelk Avatar asked Dec 13 '12 09:12

yarbelk


1 Answers

Does mine and MauroRocco's answer help you?

see celery task and customize decorator

There I succeeded to pass arguments to an extended Task

like image 73
michel.iamit Avatar answered Sep 29 '22 08:09

michel.iamit