python - Django异步-关于模型保存功能

标签 python django asynchronous

我有以下内容:

def save(self):
    for lang in ["es", "ar"]:
        setattr(self, "title_" + lang, translateField(self.title, lang))
    super(Landmarks, self).save()

translateField 函数调用 Microsoft 翻译 API,这需要一些时间才能完成执行。

是否可以异步执行相同的操作?

最佳答案

Django 不是一个异步框架。

您需要一个异步作业/任务队列,例如 celery :

Celery is an asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operation, but supports scheduling as well.

django-celery package使得django和celery的集成变得更加容易。

还有一个Redis Queue项目:

RQ (Redis Queue) is a simple Python library for queueing jobs and processing them in the background with workers. It is backed by Redis and it is designed to have a low barrier to entry. It should be integrated in your web stack easily.

另请参阅:

关于python - Django异步-关于模型保存功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18676838/

相关文章:

python - 尝试用Python实现卡片组排序算法

python - 如何防止从 Python 向 MySQLdb 语句中插入额外的引号?

javascript - 如何在 Django for 循环中使用 'clicked' 选择一个元素而不是所有元素

jquery - 如何使用 jquery 和 django 将变量传递到前面来创建 token ?

python - Django if 语句未按预期工作

javascript - 如何在 useEffect 中管理两个函数

ios - Swift4 中的异步 DispatchQueue 和多线程

python - 为什么优化模型精度会由于没有预测样本而引发错误 : Precision is ill-defined and being set to 0. 0?

python - 当我没有文件标识符时,如何在 python 中关闭文件

android - Kotlin:元素未附加在列表中