Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

To clarify some of the answers.

  • ruby or python or any sensible scripting language will buffer the output; this is in order to minimize the IO; writing to disk is slow, writing to a console is slow...
  • usually the data gets flush()'ed automatically after you have enough data in the buffer with special handling for newlines. e.g. writing a string without newline then sleep() would not write anything until after the sleep() is complete (I'm only using sleep as an example, feel free to substitute with any other expensive system call).

e.g. this would wait 8 seconds, print one line, wait 5 more seconds, print a second line.

from time import sleep def test(): print "ok", time.sleep(3) print "now", time.sleep(5) print "done" time.sleep(5) print "again" test() 

To clarify some of the answers.

  • ruby or python or any sensible scripting language will buffer the output; this is in order to minimize the IO; writing to disk is slow, writing to a console is slow...
  • usually the data gets flush()'ed automatically after you have enough data in the buffer with special handling for newlines. e.g. writing a string without newline then sleep() would not write anything until after the sleep() is complete (I'm only using sleep as an example, feel free to substitute with any other expensive system call).

e.g. this would wait 8 seconds, print one line, wait 5 more seconds, print a second line.

from time import sleep def test(): print "ok", time.sleep(3) print "now", time.sleep(5) print "done" time.sleep(5) print "again" test() 
  • for ruby, STDOUT.sync = true, turns the autoflush on; all writes to STDOUT are followed by flush(). This would solve your problem but result in more IO.

     STDOUT.sync = true 
  • for python, you can use python -u or the environment variable PYTHONUNBUFFERED to make stdin/stdout/stout not buffered, but there are other solutions that do not change stdin or stderr

     export PYTHONUNBUFFERED=1 
  • for perl, you have autoflush

     autoflush STDOUT 1; 

To clarify some of the answers.

  • ruby or python or any sensible scripting language will buffer the output; this is in order to minimize the IO; writing to disk is slow, writing to a console is slow...
  • usually the data gets flush()'ed automatically after you have enough data in the buffer with special handling for newlines. e.g. writing a string without newline then sleep() would not write anything until after the sleep() is complete (I'm only using sleep as an example, feel free to substitute with any other expensive system call).

e.g. this would wait 8 seconds, print one line, wait 5 more seconds, print a second line.

from time import sleep def test(): print "ok", time.sleep(3) print "now", time.sleep(5) print "done" time.sleep(5) print "again" test() 
  • for ruby, STDOUT.sync = true, turns the autoflush on; all writes to STDOUT are followed by flush(). This would solve your problem but result in more IO.

     STDOUT.sync = true 
  • for python, you can use python -u or the environment variable PYTHONUNBUFFERED to make stdin/stdout/stout not buffered, but there are other solutions that do not change stdin or stderr

     export PYTHONUNBUFFERED=1 
  • for perl, you have autoflush

     autoflush STDOUT 1; 
Source Link
dnozay
  • 24.4k
  • 6
  • 85
  • 105

To clarify some of the answers.

  • ruby or python or any sensible scripting language will buffer the output; this is in order to minimize the IO; writing to disk is slow, writing to a console is slow...
  • usually the data gets flush()'ed automatically after you have enough data in the buffer with special handling for newlines. e.g. writing a string without newline then sleep() would not write anything until after the sleep() is complete (I'm only using sleep as an example, feel free to substitute with any other expensive system call).

e.g. this would wait 8 seconds, print one line, wait 5 more seconds, print a second line.

from time import sleep def test(): print "ok", time.sleep(3) print "now", time.sleep(5) print "done" time.sleep(5) print "again" test() 
  • for ruby, STDOUT.sync = true, turns the autoflush on; all writes to STDOUT are followed by flush(). This would solve your problem but result in more IO.

     STDOUT.sync = true 
  • for python, you can use python -u or the environment variable PYTHONUNBUFFERED to make stdin/stdout/stout not buffered, but there are other solutions that do not change stdin or stderr

     export PYTHONUNBUFFERED=1 
  • for perl, you have autoflush

     autoflush STDOUT 1;