Django 模型、信号和电子邮件发送延迟

发布于 2024-08-23 04:44:35 字数 372 浏览 10 评论 0原文

我已向我的模型添加了一个信号,该信号会在保存模型后向某些电子邮件地址发送电子邮件(通过 models.signals.post_save.connect 信号和 send_mail 用于电子邮件发送)。这个想法仍然会给用户带来延迟,当他们在网站上保存模型时,他们必须等到所有这些电子邮件都发送完毕,然后才能收到服务器的响应。

在尝试信号之前,我曾尝试包装模型的 save 方法,在 super(Foo, self).save(*args, **kwargs) 之后我正在发送电子邮件。使用该方法也发生了这种延迟经历。

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

如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

君勿笑 2024-08-30 04:44:35

为了避免响应延迟,您希望在另一个进程中异步执行此操作。

这个问题是关于如何处理这个问题: 有关 Python/Django 和消息队列的建议< /a>

To avoid a delay to the response, you want to do this asynchronously in another process.

This question is about how to handle that: Advice on Python/Django and message queues

浪菊怪哟 2024-08-30 04:44:35

最简单的方法是将电子邮件排队,然后由守护程序发送它们。查看 django-mailer

由于您似乎只关心 send_mail,因此您可以从两个步骤开始。首先,使用它导入 django-mailer 的 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

然后创建一个调用 manage.py send_mail 发送邮件的 cronjob。检查 django-mailer 使用文档 例如 cronjob条目。

如果您没有看到任何电子邮件被发送,请尝试在控制台上运行 manage.py send_mail。这似乎是人们面临的第一个问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文