Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_wsgi yield output buffer instead of return

Right now I've got a mod_wsgi script that's structured like this..

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

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

    return [output]

I was wondering if anyone knows of a way to change this to operate on a yield basis instead of return, that way I can send the page as it's being generated and not only once it's complete, so the page loading can go faster for the user.

However, whenever I swap the output for a list and yield it in the application(), it throws an error:

TypeError: sequence of string values expected, value of type list found
like image 365
Ian Avatar asked Dec 10 '22 21:12

Ian


1 Answers

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

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

    yield output

"However, whenever I swap the output for a list and yield it in the application(), it throws an error:"

Well, don't yield the list. Yield each element instead:

for part in mylist:
    yield part

or if the list is the entire content, just:

return mylist

Because the list is already an iterator and can yield by itself.

like image 124
nosklo Avatar answered Dec 29 '22 04:12

nosklo