Python, like many other programs, tries to minimize the number of times it calls the write system call. He does this by collecting the output of several print statements before he actually writes them to his standard output file. This process is called output buffering.
When Python is connected to a terminal, it does not buffer its output. This makes sense because the person at the terminal wants to immediately see the way out.
When Python writes a file (or pipe), it buffers its output. This also makes sense since no one will see the output until the process is complete.
You can defeat this optimization by calling sys.stdout.flush() whenever you want to force Python to write its buffer output to its standard output file.
In your case, try the following:
import sys ... for item in collection: print "what up" sys.stdout.flush()
source share