在Django视图中执行阻止请求 [英] Performing a blocking request in django view

查看:56
本文介绍了在Django视图中执行阻止请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的django应用程序中的一种视图中,我需要执行一个相对冗长的网络IO操作.问题是其他请求即使与该请求无关也必须等待该请求完成.我做了一些研究,偶然发现了Celery,但据我了解,它用于执行独立于请求的后台任务.(因此我无法将任务的结果用于对请求的响应)

In one of the views in my django application, I need to perform a relatively lengthy network IO operation. The problem is other requests must wait for this request to be completed even though they have nothing to do with it. I did some research and stumbled upon Celery but as I understand, it is used to perform background tasks independent of the request. (so I can not use the result of the task for the response to the request)

有没有一种方法可以在Django中异步处理视图,以便在网络请求待处理时可以处理其他请求?

Is there a way to process views asynchronously in django so while the network request is pending other requests can be processed?

我忘了提到的是,我的应用程序是使用django rest框架的Web服务.因此,视图的结果是json响应,而不是我以后可以使用AJAX修改的页面.

What I forgot to mention is that my application is a web service using django rest framework. So the result of a view is a json response not a page that I can later modify using AJAX.

推荐答案

通常的解决方案是将任务转移到芹菜上,并在您的视图中返回请稍候"响应.如果需要,则可以使用Ajax调用来定期命中一个视图,该视图将报告响应是否准备就绪,并在响应就绪时进行重定向.

The usual solution here is to offload the task to celery, and return a "please wait" response in your view. If you want, you can then use an Ajax call to periodically hit a view that will report whether the response is ready, and redirect when it is.

这篇关于在Django视图中执行阻止请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆