栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用django-background-tasks

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用django-background-tasks

您似乎使用了错误的方式。

假设您必须执行某些特定的任务,例如在用户注册后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分钟或您想要的任何间隔调用一次。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/634341.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号