List Info

Thread: stream page content




stream page content
country flaguser name
United States
2007-09-10 15:24:12
Shouldn't it be possible to stream stuff incrementally to
the client
by using _cp_config = {'response.stream': True} and yield as
described
in http://www
.cherrypy.org/wiki/ReturnVsYield

My use case is 'tracking log output' - but the yielded
content is only
shown after the generator is completly consumed.

If remember correctly I could do this in ASPX by writing
delayed to
the OutputStream.

That is the code for the function:

def streamLog(**args):
    cherrypy.response.headers['Content-Type'] =
'text/plain'
    f = open(config.PathLog.joinpath('%s.log' %
args['name']),'r')

    def content(logFile):
        for l in logFile:
            yield l
    return content(f)
    streamLog._cp_config = {'response.stream': True}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: stream page content
country flaguser name
United States
2007-09-10 15:39:18

d.rothe wrote:
> Shouldn't it be possible to stream stuff incrementally to the client
> by using _cp_config = {'response.stream': True} and yield as described
> in http://www.cherrypy.org/wiki/ReturnVsYield
>
> My use case is 'tracking log output' - but the yielded content is only
>; shown after the generator is completly consumed.
>
> If remember correctly I could do this in ASPX by writing delayed to
> the OutputStream.
>
> That is the code for the function:
>
> def streamLog(**args):
>     cherrypy.response.headers['Content-Type'] = 'text/plain'
>     f = open(config.PathLog.joinpath('%s.log' % args['name']),'r')
>
>  ;   def content(logFile):
>       ;  for l in logFile:
  ;          yield l
>     return content(f)
>     streamLog._cp_config = {'response.stream': True}

Did you mean to indent that last line? It should be at the same
indent level as the function declaration. You also shouldn't need
to wrap the file in a generator; just return the file:


def streamLog(**args):
  ;  cherrypy.response.headers['Content-Type'] = 'text/plain'
   ; return open(config.PathLog.joinpath('%s.log' % args['name']),'r')
streamLog._cp_config = {'response.stream': True}


Also, although the above is generally true, there are some cases
where the body will be collapsed (buffered) even if response.stream
is True. You can find them in your version of CherryPy by grepping
for "collapse_body". Here are the ones in the trunk version of CP:

  _cperror._be_ie_unfriendly: Some error responses are collapsed
  in order to make sure IE doesn't replace the response body, as it
 ; is wont to do.

  ETag tool: if you allow it to autogenerate ETags, it will do so
 ; by collapsing the body so it can use an md5 hash.


Robert Brewer
fumanchuaminus.org


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Re: stream page content
user name
2007-09-10 16:00:21
On 9/10/07, d.rothe <theceregmail.com> wrote:
>
> Shouldn't it be possible to stream stuff incrementally
to the client
> by using _cp_config = {'response.stream': True} and
yield as described
> in http://www
.cherrypy.org/wiki/ReturnVsYield
...
[snip]
...
>     cherrypy.response.headers['Content-Type'] =
'text/plain'

I'm going to guess that you are testing this in Firefox. 
For some
reason, text/plain seems to be buffered by Firefox before
rendering.
I've run into this same thing before.  Change to another
content-type
to see the difference.

Christian
http://www.dowski.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: stream page content
country flaguser name
United States
2007-09-11 11:42:17
Thats the working version now. logFile is a path object
(http://
pypi.python.org/pypi/path.py/2.2) - can be easily translated
to
standard file functions. content() is practically the tail
stuff from
http://aspn.activestate.com/ASPN/Cookbook/Python/R
ecipe/157035

def streamLog(**args):
    cherrypy.response.headers['Content-Type'] = 'text/html'
    logFile = config.PathLog.joinpath('%s.log' %
args['name'])

    def content(logFile):
        yield '<html> <pre>'

        file = open(logFile,'r')
        file.seek(logFile.size)

        while True:
            where = file.tell()
            line = file.readline()
            if not line:
                time.sleep(1)
                file.seek(where)
            else:
                yield line # already has newline
    return content(logFile)
streamLog._cp_config = {'response.stream': True,
                        'tools.gzip.on'             :
False,
                        }


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: stream page content
country flaguser name
United States
2007-09-11 11:49:07
btw.

1) yea the _cp_config indentation was corrupt
2) it was not ETags but gzip which had to collapse the body
3) text/plain was not working in firefox/opera (ie not
tested)
4) text/html is working in firefox/ie but not in opera


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-5]

about | contact  Other archives ( Real Estate discussion Medical topics )