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.
Does mine and MauroRocco's answer help you?
see celery task and customize decorator
There I succeeded to pass arguments to an extended Task
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With