从前端使用 AJAX 查询 Celery 以了解创建的任务是否完成的最佳方法?
我使用 Django 和 Celery + RabbitMQ 来创建用户上传的视频的视频转换任务。 现在我知道如何查询 celery 来获取状态。 我的问题是在哪里保存与每个任务关联的task_id任务,我应该将其保存在模型中还是 django 的缓存中?
我知道过去也有人提出过类似的问题,例如 这个 stackoverflow问题,但目前还没有明确的答案。我知道这个问题的答案因开发人员的喜好而异,但如果有人能够了解不同方法的优缺点,那就太好了。
重申一下,我将使用 task_id 经常从前端触发 AJAX 查询,以了解视频转换是否完成。
I am using Django with Celery + RabbitMQ to create video conversion tasks of videos being uploaded by the users. Now I know how to query celery to get the status. My question is where to save the task_id associated with each task, should I save it in models or in django's cache?
I know that similar questions have been asked in the past e.g. this stackoverflow question but there has been no definitive answer yet. I know the answer to this varies per developer's preferences but if someone can educate on pros/cons of different approaches, that will be great.
Just to re-iterate I will use the task_id to fire AJAX queries every so often from the front-end to know if the video conversion is completed or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您安装了 django_celery,您可以查询诸如
task_status
或 < code>is_task_successful 位于djcelery.views
包中。它们返回一个 JSON 字典,以便您可以使用 JavaScript 处理它们。If you have django_celery installed, you can query views like
task_status
oris_task_successful
that come in thedjcelery.views
package. They return a JSON dictionary so you can handle them with JavaScript.请参阅我对链接问题的回答: 测试如果 celery 任务仍在处理中
因此,在设置 celery_task 字段后,您可以轻松定义一个 ajax 视图,该视图将以所需的格式返回任务状态。
Refer to my answer on your linked question: Test if a celery task is still being processed
So after setting the celery_task field, you could easily define a ajax view which would return the task status in a required format.