我有以下内容:
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/