在 django 中在特定时间调用函数的最佳方法 [英] Best way to call a function in specific time in django

查看:38
本文介绍了在 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

这篇关于在 django 中在特定时间调用函数的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆