您似乎使用了错误的方式。
假设您必须执行某些特定的任务,例如在用户注册后5分钟发送一封邮件。因此,您要做的是:
使用django-background-task创建任务。
@background(schedule=60*5)def send_html_mail_post(id, template): u = User.objects.get(id=id) user_email = u.email subject = "anything" html_content = template.format(arguments) from_email, to = from_email, user_email text_content = '' msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.send()
顶部的装饰器定义在调用函数多少时间后才会发生实际事件。
需要时调用它。
def create_user_profile(sender, instance, created, **kwargs): if created: up = UserProfile.objects.create(user=instance) up.save() tasks.send_welcome_email(up.user.id, template=template)
这将创建任务并将其保存在数据库中,并将执行该任务的时间存储在db中。
您想要做的事情,定期执行某件事,可以通过创建cron作业更轻松地完成。
您要做的是,按照问题所示创建一个函数。然后定义一个cron作业,每5分钟或您想要的任何间隔调用一次。



