在 django 中在特定时间调用函数的最佳方法
在我的 Django 项目中,我有一个模型 Task
:
in my django project i have a model Task
:
class Task(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
expiration_date = models.DateTimeField(default=max_expiration_date)
####################################
def max_expiration_date():
now = timezone.now()
return now + timezone.timedelta(days=7)
确切的到期日期我需要调用 notification
函数.请注意我将有太多 Task
实例,所以我需要最佳系统消耗的最佳方法.什么是最好的方法是什么?celery
能做到吗?谢谢.
exact in expiration date i need call notification
function.please be notice i will have too many Task
instance so i need the best method for optimal system consumption.what is best way to do that? celery
can do this?thank you.
Celery 函数 apply_async
可以接受一个参数 eta
来指定任务应该在之后的日期时间运行.
The Celery function apply_async
can take an argument eta
to specify a datetime after which the task should be run.
注意:任务保证在指定日期和时间之后的某个时间执行,但不一定在那个确切时间执行.
Note: The task is guaranteed to be executed at some time after the specified date and time, but not necessarily at that exact time.
有了这个,您只需将您的 expiration_date
作为 eta
参数传递给您的 notification
内的 apply_async
初始任务创建.
With this you can just pass your expiration_date
as the eta
parameter calling the apply_async
on your notification
within the initial task creation.
文档:http://docs.celeryproject.org/en/latest/userguide/calling.html#eta-and-countdown