title: Calling instance method of class using celery in Python
body: <p>In older version of <a href="http://www.celeryproject.org/" rel="nofollow noreferrer">Celery</a> there was facility of converting instance method to celery task like example below according to <a href="http://docs.celeryproject.org/en/3.1/reference/celery.contrib.methods.html" rel="nofollow noreferrer">http://docs.celeryproject.org/en/3.1/reference/celery.contrib.methods.html</a></p>
  
  <pre><code>from celery.contrib.methods import task
  class X(object):
      @task()
      def add(self, x, y):
          return x + y
  </code></pre>
  
  <p>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?  </p>
  
  <p>Let me represent my requirement by example.  </p>
  
  <pre><code>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
  </code></pre>
  
  <p>I want to run <code>preProcess</code>, <code>process</code> and <code>postProcess</code> of <code>Client1Service</code> class using celery inside <a href="https://www.djangoproject.com/" rel="nofollow noreferrer">django</a> project. </p>
  
  <p>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.<br>
  Suggest me some design for this requirement.</p>