Django-刷新响应? [英] Django - flush response?

查看:86
本文介绍了Django-刷新响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将AJAX请求发送到Django视图,这可能会花费很多时间。但是,它要经过一些明确定义的步骤,因此,我想向用户打印状态指示器,让它知道何时完成某件事并移至下一个。



如果我使用的是PHP,则可能使用 flush 函数如下所示:

  do_something(); 
打印做某事!;
flush();

do_something_else();
打印做其他事情!
flush();

我将如何使用Django进行相同操作?查看文档,我看到HttpResponse对象具有刷新功能方法,但必须说的是此方法使HttpResponse实例成为类似文件的对象。 -我不确定那不是我想要的。我很费劲地想在Django中完成此操作,因为我必须返回响应,并且无法真正控制内容何时到达浏览器。

解决方案

大多数Web服务器(例如FCGI / SCGI)执行自己的缓冲,HTTP客户端执行自己的缓冲,依此类推。



最接近您的实际情况,很难以这种方式实际清除数据并使客户端无法实际接收数据。



尝试做的是将迭代器传递给HttpResponse,并在生成器中完成工作;像这样的东西:

  def index(request):
def do_work():
step_1()
收益第1步完成
step_2()
收益第2步完成
step_3()
收益第3步完成
返回HttpResponse (do_work())

...但这不会不必要冲洗。 (未经测试的代码,但您可以理解;请参见 http: //docs.djangoproject.com/en/dev/ref/request-response/#passing-iterators 。)



大多数基础设施都不是期望得到零星的回应。即使Django没有缓冲,您的前端服务器也可能是,客户端也可能是。这就是大多数事情为此使用拉动更新的原因:一个单独的接口来查询长时间运行的请求的状态。



(我希望能够做到可靠也为此类事情推送更新...)


I am sending an AJAX request to a Django view that can potentially take a lot of time. It goes through some well-defined steps, however, so I would like to print status indicators to the user letting it know when it is finished doing a certain thing and has moved on to the next.

If I was using PHP it might look like this, using the flush function:

do_something();
print 'Done doing something!';
flush();

do_something_else();
print 'Done doing something else!';
flush();

How would I go about doing the same with Django? Looking at the documentation I see that HttpResponse objects have a flush method, but all it has to say is that "This method makes an HttpResponse instance a file-like object." - I'm not sure that's what I want. I'm having a hard time wrapping my head around how this could be done in Django since I have to return the response and don't really have a control of when the content goes to the browser.

解决方案

Most webservers (eg. FCGI/SCGI) do their own buffering, HTTP clients do their own, and so on. It's very difficult to actually get data flushed out in this way and for the client to actually receive it, because it's not a typical operation.

The closest to what you're trying to do would be to pass an iterator to HttpResponse, and to do the work in a generator; something like this:

def index(request):
    def do_work():
        step_1()
        yield "step 1 complete"
        step_2()
        yield "step 2 complete"
        step_3()
        yield "step 3 complete"
    return HttpResponse(do_work())

... but this won't necessarily flush. (Not tested code, but you get the idea; see http://docs.djangoproject.com/en/dev/ref/request-response/#passing-iterators.)

Most of the infrastructure is simply not expecting a piecemeal response. Even if Django isn't buffering, your front-end server might be, and the client probably is, too. That's why most things use pull updates for this: a separate interface to query the status of a long-running request.

(I'd like to be able to do reliable push updates for this sort of thing, too...)

这篇关于Django-刷新响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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