title: How to stream a video online while it is being generated and a failed CGI approach
body: <p>I have a program that can generate video in real time. Now I would like to stream this video online while it is being generated. Does anybody know an easy way to do it? </p>
  
  <p>I am describing a CGI approach I tried but did not work, but please note that I am open to all options that would achieve my goal. I am just wondering if anybody knows why my approach doesn't work and how I should fix it</p>
  
  <p>I set the content-type to mpeg for example, and print out a chunk of data in the mpeg file periodically. But the video only lasts for very short amount of time and stop streaming. My code is something like this (in Python). </p>
  
  <pre><code>print "Content-type: video/mpeg"
  print
  f = open("test2.mpg")
  while (True):
      st = f.read(1024*1024)
      sys.stdout.write(st)
      time.sleep(0.5)
  </code></pre>
  
  <p>Though this would work fine. I really don't see why the output of these two programs are different. But obviously I can't use this approach since i can't wait until the entire file is generated before reading in. </p>
  
  <pre><code>print "Content-type: video/mpeg"
  print
  f = open("test2.mpg")
  print f.read()
  </code></pre>