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.

1

1 Answer 1

-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

Sign up to request clarification or add additional context in comments.

2 Comments

In my case methods are inside class. @shared_task(bind=True) on instance method is not working.
I see - try this: stackoverflow.com/questions/9250317/…, it makes your class extend celery.Task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.