python - 我不明白这个 Django 文档。我该如何使用这个模块?

标签 python django asynchronous queue notifications

我正在使用 django_notification 模块。 https://github.com/pinax/django-notification/blob/master/docs/usage.txt

这就是我在代码中所做的,当发生某些情况时向用户发送电子邮件:

notification.send([to_user], "comment_received", noti_dict)

但是,这似乎阻止了请求。而且要等很久才能发出来。我阅读了文档,它说可以将其添加到队列(异步)。如何将其添加到异步队列?

我不明白文档想说什么。什么是“emit_notices”?我什么时候这么称呼?我有一个每 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

这将定期运行并执行阻塞调用,发送所有电子邮件以及通知应用程序所需的任何跑腿工作。我确信如果没什么可做的,工作量也不会那么大。

在你进一步评论这件事是愚蠢的之前,你应该考虑一下。这一点也不傻。您不希望阻塞调用与网络请求相关联,否则用户将永远不会从服务器获得响应。从这个意义上说,发送电子邮件是阻塞的。

现在,如果您只是想让用户在登录时收到此通知,那么您可能不需要这样做,因为您必须对 sendmail 进行外部调用或任何您用来发送电子邮件的东西。但就您而言,发送电子邮件,您应该这样做。

关于python - 我不明白这个 Django 文档。我该如何使用这个模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4643564/

相关文章:

Python: "Name is not defined"错误

python - 插入二叉搜索树

jquery - 渲染响应不适用于 jquery ajax 请求

javascript - 场景在哪里异步/等待 promise ?

python - 在 web.py 中异步发送邮件

python - 如何在 python 中将变量从文本 "1m"更改为 "1000000"

python - 如何将 pandas 数据帧的状态导入到第二个 .py 文件

python - 将 Django 数据库导出到 YAML 文件

python - 在 apache 服务器上导入 Pandas 导致超时错误

python - 在python中可以异步写入文件吗?