Latest Version: 0.9.6.2
  Dashboard > Pylons FAQ > Home > Streaming Content to the Browser
  Pylons FAQ Log In | Sign Up   View a printable version of the current page.  
  Streaming Content to the Browser
Added by Ben Bangert, last edited by Ben Bangert on Jul 18, 2007
Labels: 

Question

How do I stream content to the browser?

Answer

First, turn off debug mode in your ini file, or copy your development.ini file and uncomment the line:

1
#set debug = false

When debug mode is on, streaming can't occur because the exception middleware needs to have the entire response finished first. Now that it's disabled, merely returning an iterator object or generator is sufficient to start streaming output.

Here's an example that sends a paragraph every 2 seconds:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import time

class StreamingController(object):
    def output(self):
        def output_pause():
            num = 0
            while 1:
                num += 1
                yield num
                time.sleep(2)
        return output_pause()

On loading /streaming/output, you should now see a number sent out every 2 seconds, continuing on forever.

Is this syntax only for Pylons 0.9.6? If yes, how to do that with old Response object returning?

Posted by Dan Korostelev at Jul 19, 2007 13:22 | Permalink

This will work in 0.9.5 as well.

Posted by Ben Bangert at Jul 20, 2007 21:28 | Permalink
Site running on a free Atlassian Confluence Open Source Project License granted to Pylons. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.3.3 Build:#645 Feb 13, 2007) - Bug/feature request - Contact Administrators
Top