简体   繁体   中英

How to run @classmethod as a Celery task in Django?

How to run @classmethod as a Celery task in Django? Here is example code:

class Notification(TimeStampedModel):
    ....
    @classmethod
    def send_sms(cls, message, phone, user_obj=None, test=False):    
        send_message(frm=settings.NEXMO_FROM_NUMBER, to=phone, text=message)

        return cls.objects.create(
            user=user_obj,
            email="",
            phone=phone,
            text_plain=message,
            notification_type=constants.NOTIFICATION_TYPE_SMS,
            sent=True,
        )

n = Notification()
n.send_sms(message='test', phone='1512351235')

You can wrap it in a celery task like this.

from celery import shared_task

@shared_task
def sample_task(message, phone):
  Notification.send_sms(message, phone)

and call it this way:

sample_task.delay(message='test', phone='1512351235')

Don't forget to perform first steps of configuration of Celery in your Django project which is described here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM