Bypassing buffering of subprocess output with popen in C or Python

前端 未结 2 534
孤街浪徒
孤街浪徒 2020-12-05 15:49

I have a general question about popen (and all related functions), applicable to all operating systems, when I write a python script or some c code and run the resulting exe

2条回答
  •  执念已碎
    2020-12-05 16:31

    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).

提交回复
热议问题