On 30 June 2010 21:35, Aaron Fransen <aaron.fran...@gmail.com> wrote:
>
>
> On Tue, Jun 29, 2010 at 6:17 PM, Graham Dumpleton
> <graham.dumple...@gmail.com> wrote:
>>
>> On 30 June 2010 02:14, Aaron Fransen <aaron.fran...@gmail.com> wrote:
>> > Couple more things I've been able to discern.
>> >
>> > The first happened after I "fixed" the html code. Originally under
>> > mod_python, I guess I was cheating more than a little bit by sending
>> > <html></html> code blocks twice, once for the incremental notices, once
>> > for
>> > the final content. Once I changed the code to send a single properly
>> > parsed
>> > block, the entire document showed up as expected, however it still did
>> > not
>> > send any part of the html incrementally.
>> >
>> > Watching the line with Wireshark, all of the data was transmitted at the
>> > same time, so nothing was sent to the browser incrementally.
>> >
>> > (This is using the write() functionality, I haven't tried watching the
>> > line
>> > with yield yet.)
>>
>> Use a variation of WSGI middleware wrapper in:
>>
>>
>>  http://code.google.com/p/modwsgi/wiki/DebuggingTechniques#Tracking_Request_and_Response
>>
>> using it to 'print' returned data to Apache log and then tail Apache
>> error log to see when that data is output. Alternatively, change the
>> code there to output a time stamp against each chunk of data written
>> to the file recording the response content.
>>
>> This will show what data is returned by WSGI application, before
>> mod_wsgi truncates anything greater than content length specified,
>> plus also show whether it is your WSGI application which is delaying
>> output somehow, or whether Apache output filters are doing it.
>>
>> Graham
>
> I've actually tried a variation on this already using a built-in logging
> facility in the application that writes date/time values to an external log
> file with comments, and in the case of testing wsgi I actually included some
> time.sleep() statements to force a delay in the application.
>
> To give you an idea of the flow, here's essentially what's going on:
>
> def application(environ,start_response):
>     mydict = {}
>     mydict['environ']=environ
>     mydict['startresponse'] = start_response
>     # run program in another .py file that has been imported
>     RunTest(mydict)
>
> Then in the other module you would have something like:
>
> def RunTest(mydict):
>     status = '200 OK'
>     response_headers = [('Content-type','text/html')]
>     writeobj = detail['startresponse'](status,response_headers)
>     writeobj('<html><body>Fetching sales for 2009...')
>     time.sleep(2)
>     writeobj('<br>Fetching sales for 2010...')
>
>     ...then finally...
>
>     writeobj('5000 results returned.</body></html>')
>     return
>
> This is obviously a truncated (and fake) example, but it gives you an idea
> of the flow.

Now go try the following two examples as illustrated instead.

In both cases, do not use a web browser, instead telnet to the port of
the web server and enter HTTP GET directly. If you are not using
VirtualHost, use something like:

  telnet localhost 80
  GET /stream-yield.wsgi HTTP/1.0

If using a VirtualHost, use something like:

  telnet localhost 80
  GET /stream-yield.wsgi HTTP/1.1
  Host: tests.example.com

Ensure additional blank line entered to indicate end of headers.

First example uses yield.

# stream-yield.wsgi

import time

def application(environ, start_response):
    status = '200 OK'

    response_headers = [('Content-type', 'text/plain')]
    start_response(status, response_headers)

    for i in range(10):
      yield '%d\n' % i
      time.sleep(1)

Second example uses write:

# stream-write.wsgi

import time

def application(environ, start_response):
    status = '200 OK'

    response_headers = [('Content-type', 'text/plain')]
    write = start_response(status, response_headers)

    for i in range(10):
      write('%d\n' % i)
      time.sleep(1)

    return []

For me, using stock standard operating system supplied Apache on Mac
OS X, I see a line returned every second.

If I use Safari as a web browser, in both cases the browser only shows
the response after all data has been written and the socket connection
closed. If I use Firefox however, they display as data comes in.

This delay in display is thus possibly just the behaviour of a
specific browser delaying the display until the socket is closed.

The example for multipart/x-mixed-replace which others mention is:

import time

def application(environ, start_response):
    status = '200 OK'

    response_headers = [('Content-Type', 'multipart/x-mixed-replace;
boundary=xstringx')]
    start_response(status, response_headers)

    yield '--xstrinx\n'

    for i in range(10):

      yield 'Content-type: text/plain\n'
      yield '\n'
      yield '%d\n' % i
      yield '--xstringx\n'

      time.sleep(1)

With telnet you will see the various sections, but with Safari again
only shows at end, although you will find that it only shows the data
line, ie., the number and not all the other stuff. So, understands
multipart format but doesn't support x-mixed-replace. It was always
the case that only certain browsers supported that mime type. In the
case of Firefox, it doesn't seem to like it at all and seems to give
up and not display anything, not even replacing the previously
displayed page contents.

What this means is that you cant rely on browsers to handle multipart
mixed replace alone. If you were really going to use that format, you
really want to use JavaScript and AJAX stuff to process it. The same
applies for progressive display of plain text content when streamed
over time.

In summary, you really want to be using some JavaScript/AJAX stuff on
browser side to get uniform behaviour on all the browsers.

Graham
_______________________________________________
Web-SIG mailing list
Web-SIG@python.org
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: 
http://mail.python.org/mailman/options/web-sig/archive%40mail-archive.com

Reply via email to