dev*_*fan 4 python django celery
如何在 Django 中将 @classmethod 作为 Celery 任务运行?这是示例代码:
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')
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以将其包装在像这样的 celery 任务中。
from celery import shared_task
@shared_task
def sample_task(message, phone):
Notification.send_sms(message, phone)
Run Code Online (Sandbox Code Playgroud)
并这样称呼它:
sample_task.delay(message='test', phone='1512351235')
Run Code Online (Sandbox Code Playgroud)
不要忘记在 Django 项目中执行 Celery 配置的第一步,如下所述。
| 归档时间: |
|
| 查看次数: |
1647 次 |
| 最近记录: |