In older version of Celery there was facility of converting instance method to celery task like example below according to http://docs.celeryproject.org/en/3.1/reference/celery.contrib.methods.html
from celery.contrib.methods import task
class X(object):
@task()
def add(self, x, y):
return x + y
I am using Celery 4.1 which does not have such feature by default. How can I achieve this facility by my own in some simple way?
Let me represent my requirement by example.
from abc import ABC, abstractmethod
AbstractService(ABC):
def __init__(self, client_id, x, y):
self.client_id = client_id
self.x = x
self.y = y
@abstractmethod
def preProcess(self):
'''Some common pre processing will execute here'''
@abstractmethod
def process(self):
'''Some common processing will execute here'''
@abstractmethod
def postProcess(self):
'''Some common post processing will execute here'''
Client1Service(AbstractService):
def __init__(self, x, y):
super(__class__, self).__init__('client1_id', x, y)
# I want to run this using celery
def preProcess(self):
super().preProcess()
# I want to run this using celery
def process(self):
data = super().process()
# apply client1 rules to data
self.apply_rules(data)
print('task done')
# I want to run this using celery
def postProcess(self):
super().postProcess()
def appy_rules(self, data):
'''Client 1 rules to apply'''
# some logic
I want to run preProcess
, process
and postProcess
of Client1Service
class using celery inside django project.
If I will not get any solution then I would have to run logic of preProcess, process and postProcess in some outside celery task that would be little messy.
Suggest me some design for this requirement.