I have a program that uses curses, and then returns to the main script for further processing. After it returns, my subsequent output to stdout does not appear until there’s a large amount of it (e.g. thousands of bytes).
I’ve reduced the problem to a very simple program that fails reliably:
import curses
import time
curses.initscr()
curses.endwin()
print "Hello world!"
time.sleep(5)
If I comment out the two curses calls, the ‘Hello world!’ prints before the delay. If I put them in, it prints after the delay (when the script exits).
The
curses.endwin()call “sets standard I/O back to normal”… which unfortunately means “buffered” (you could consider that a bug incursesand file the bug on Python’s tracker).To ensure standard-output is unbuffered,
(You can put the
import osat the start, or course; the reassignment ofsys.stdoutis meant to go right after theendwincall).