我正在使用 django_notification 模块。 https://github.com/pinax/django-notification/blob/master/docs/usage.txt
这就是我在代码中所做的,以便在发生某些事情时向用户发送电子邮件:
notification.send([to_user], "comment_received", noti_dict)
但是,这似乎阻止了请求。而且需要很长时间才能发出来。我阅读了文档,它说可以将它添加到队列中(异步)。如何将其添加到异步队列?
我不明白文档想说什么。什么是“发出通知”?我什么时候调用它?我是否有每 5 秒调用一次的脚本?太傻了。异步执行此操作的正确方法是什么?我该怎么办?
Lets first break down what each does.
``send_now``
~~~~~~~~~~~~
This is a blocking call that will check each user for elgibility of the
notice and actually peform the send.
``queue``
~~~~~~~~~
This is a non-blocking call that will queue the call to ``send_now`` to
be executed at a later time. To later execute the call you need to use
the ``emit_notices`` management command.
``send``
~~~~~~~~
A proxy around ``send_now`` and ``queue``. It gets its behavior from a global
setting named ``NOTIFICATION_QUEUE_ALL``. By default it is ``False``. This
setting is meant to help control whether you want to queue any call to
``send``.
``send`` also accepts ``now`` and ``queue`` keyword arguments. By default
each option is set to ``False`` to honor the global setting which is ``False``.
This enables you to override on a per call basis whether it should call
``send_now`` or ``queue``.
请您参考如下方法:
看起来你需要在你的设置文件中设置
NOTIFICATION_QUEUE_ALL=True
然后你需要设置一个 cronjob(可能每隔 10-30 秒或类似的时间)来运行类似的东西,
django_admin.py emit_notices
这将定期运行并执行阻塞调用,发送所有电子邮件和通知应用程序需要的任何跑腿工作。我敢肯定,如果无事可做,工作量不会那么大。
在你进一步评论这很愚蠢之前,你应该考虑一下。这一点也不傻。您不希望将 阻塞 调用绑定(bind)到 Web 请求,否则用户将永远不会从服务器获得响应。从这个意义上说,发送电子邮件是阻塞的。
现在,如果您只是想让此人在登录时收到此通知,那么您可能不需要这样做,因为您必须对 sendmail 进行外部调用或者你用来发送电子邮件的任何东西。但在您的情况下,发送电子邮件时,您应该这样做。
