3

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.

Alok
  • 7,734
  • 8
  • 55
  • 100
  • Does this answer your question? [using class methods as celery tasks](https://stackoverflow.com/questions/9250317/using-class-methods-as-celery-tasks) – Huseyin Yagli Apr 09 '20 at 15:35

1 Answers1

-1

Try using:

from celery import shared_task
@shared_task(bind=True)
def yourfunction():
    dosth()

Here a good tutorial: http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html

Kai Aeberli
  • 1,189
  • 8
  • 21
  • 1
    In my case methods are inside class. `@shared_task(bind=True)` on instance method is not working. – Alok Aug 10 '18 at 08:00
  • I see - try this: https://stackoverflow.com/questions/9250317/using-class-methods-as-celery-tasks, it makes your class extend celery.Task. – Kai Aeberli Aug 10 '18 at 08:04