Django机型,信号和电子邮件发送延迟 [英] Django models, signals and email sending delay

查看:141
本文介绍了Django机型,信号和电子邮件发送延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经向我的模型添加了一个信号,一旦保存了一个模型,它就会向一些电子邮件地址发送电子邮件(通过 models.signals.post_save.connect signal和 send_mail 发送电子邮件)。这个想法仍然使用户延迟,当他们将模型保存在网站时,他们必须等待所有这些电子邮件发送,并在收到服务器的响应时。



在尝试信号之前,我试图包装我的模型的保存方法,之后 super(Foo,self).save(* args,** kwargs)我正在发送电子邮件



我只是希望我的电子邮件发送操作在后台完成,而不会在网站上向用户显示延迟。



如何解决?

解决方案

最简单的方法是排队邮件消息,然后让他们由守护进程发送。查看 django-mailer



既然你似乎只关心send_mail,你可以开始两步。首先,使用它来导入django-mailer的send_mail版本:

 #favor django-mailer但是回到django.core。邮件
从django.conf导入设置

如果mailer在settings.INSTALLED_APPS:
从邮件导入send_mail
其他:
从django.core .mail import send_mail

然后创建一个cronjob调用 manage.py send_mail 发送邮件。检查 django-mailer usage docs ,例如cronjob



如果您没有看到任何电子邮件发送,请尝试在控制台上运行 manage.py send_mail 。这似乎是人们头等大事。


I have added a signal to my model, which sends email to some email addresses once a model is saved (via models.signals.post_save.connect signal and send_mail for email sending). This idea still makes delay for the users, when they save the model at the site, they have to wait until all those emails are sent and thats when they receive response from the server.

Before trying signals, I had tried to wrap the save method of my model, and after super(Foo, self).save(*args, **kwargs) I was sending emails. This delay experience was happening with that method too.

I simply want my email sending actions to be done in background, without showing delays to users at site.

How can this be solved?

解决方案

The easiest thing is to queue the email messages and then have them sent by a daemon. Check out django-mailer.

Since you only seem to be concerned with send_mail, you can get started with two steps. First, use this to import django-mailer's version of send_mail:

# favour django-mailer but fall back to django.core.mail
from django.conf import settings

if "mailer" in settings.INSTALLED_APPS:
    from mailer import send_mail
else:
    from django.core.mail import send_mail

And then create a cronjob that calls manage.py send_mail to send the mail. Check the django-mailer usage docs for example cronjob entries.

If you are not seeing any email get sent, try running manage.py send_mail on the console. This seems to be the number one problem people have.

这篇关于Django机型,信号和电子邮件发送延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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