芹菜:在特定时间间隔后执行任务

时间:2021-06-19 01:21:14

I want to send an email to my users exactly 48 hours after they have registered.How do i achieve this using celery? If I create a periodic task to send an email, I will have to decide a specific time during which i want to execute that task. I don't want to keep run a celery task every second to check if there are any emails needed to be sent.

我想在注册后48小时向我的用户发送电子邮件。如何使用芹菜实现这一目标?如果我创建一个定期任务来发送电子邮件,我将必须决定我想要执行该任务的特定时间。我不想每秒都继续运行一个芹菜任务来检查是否需要发送任何电子邮件。

1 个解决方案

#1


15  

You'll want to make use of ETA. Read that section of the docs as it'll have more information. However, your code will look something like this:

你会想要使用ETA。阅读文档的该部分,因为它将提供更多信息。但是,您的代码看起来像这样:

from datetime import datetime, timedelta
send_date = datetime.utcnow() + timedelta(days=2)
email_user.apply_async([user], eta=send_date)

#1


15  

You'll want to make use of ETA. Read that section of the docs as it'll have more information. However, your code will look something like this:

你会想要使用ETA。阅读文档的该部分,因为它将提供更多信息。但是,您的代码看起来像这样:

from datetime import datetime, timedelta
send_date = datetime.utcnow() + timedelta(days=2)
email_user.apply_async([user], eta=send_date)