0

I ran into a case in which my machine buffered stdout, but a coworker's machine did NOT buffer stdout for the exact same code. We're running nearly identical environments (we're both using the same virtual machine image in VMware Player, but he on a Mac, and I on a PC). The code uses Popen() to execute one python script from another, and the executed script buffers stdout on my machine, but not his.

I know of these ways to control python's buffering of stdout: Disable output buffering

But neither machine had the PYTHONUNBUFFERRED environment variable set, and none of the programmatic means for unbuffering stdout were employed in any of the code.

Incidentally, adding "-u" to the Popen() call caused stdout to be unbuffered on my machine, but did not change the behavior on his.

Taking into account that preamble, what other things might cause stdout to be buffered on one machine and unbuffered on another, for otherwise identical code?

This might be esoteric, but it's important that I be able to give a thorough explanation, and I've run out of ideas for search strings.

EDIT: The initial program can be invoked from either the command-line or a desktop link (which issues a bash command). The VM is running Ubuntu Linux. I will try to post a simple code example, but that may take some time.

Thinking about invocation, I just realized that I usually launch from the command line and he usually launches from the desktop shortcut, which might be the critical difference. But I still don't know why...

Here is some code that demonstrates what I'm doing (mostly; there's much more to it, but it's hard for me to know what's relevant).

process = Popen(['python', '-u', 'some_script.py'], shell=False, stdout=PIPE)
char = ''
while char != '\n' and len(read) > 0: 
    read, write, exception = select([stdout],[],[],0)          
    if read is not None and len(read) > 0:
        char = stdout.readline(1)
        output_buffer += char
#end while

if char == '\n':
    print output_buffer
    output_buffer = ''

The invoking script calls Popen, and then does a non-blocking buffered read, by selecting on stdout to read a character until it gets a \n or there's nothing to read.

The invoked script basically just prints a line of text using "print" (not reproduced here).

4

0 回答 0