Я использовал ( iTextSharp) в прошлом с хорошими результатами.
In general, the standard C runtime library (that's running on behalf of just about every program on every system, more or less;-) detects whether stdout is a terminal or not; if not, it buffers the output (which can be a huge efficiency win, compared to unbuffered output).
If you're in control of the program that's doing the writing, you can (as another answer suggested) flush stdout continuously, or (more elegantly if feasible) try to force stdout to be unbuffered, e.g. by running Python with the -u
commandline flag:
-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)
see man page for details on internal buffering relating to '-u'
(what the man page adds is a mention of stdin and issues with binary mode[s]).
If you can't or don't want to touch the program that's writing, -u
or the like on the program that's just reading is unlikely to help (the buffering that matters most is the one happening on the writer's stdout, not the one on the reader's stdin). The alternative is to trick the writer into believing that it's writing to a terminal (even though in fact it's writing to another program!), via the pty
standard library module or the higher-level third party pexpect module (or, for Windows, its port wexpect).
Это правильно и применимо как к Windows, так и к Linux (и, возможно, другим системам), с popen ()
и fopen ()
. Если вы хотите, чтобы выходной буфер отправлялся до 4096 байт, используйте fflush ()
(на C) или sys.stdout.flush ()
(Python).