print(....,file=sys.stderr) buffered?

Helmut Jarausch jarausch at skynet.be
Mon Aug 13 12:29:45 EDT 2012


On Mon, 13 Aug 2012 15:43:31 +0000, Grant Edwards wrote:

> On 2012-08-13, Helmut Jarausch <jarausch at skynet.be> wrote:
>> Hi,
>>
>> for tracing purposes I have added some print outs like
>>
>> print('+++ before calling foo',file=sys.stderr)
>> x=foo(..)
>> print('--- after  calling foo',

Sorry, this is a cut'n paste error. I did use
print('--- after  calling foo',file=sys.stderr)

>>
>> and within 'foo'
>> print('>>> entering foo ...',file=sys.stderr)
>>
>> Now, when executing this, I always get
>>
>> +++ before calling foo --- after  calling foo
>>>>> entering foo ...
>>
>> When outputting to stderr from C/C++ it's guaranteed that the different
>> outputs appear in the same order as they have been generated.
> 
> You're not printing to stderr in the second print() call -- you're
> printing to stdout.  The two file objects have separate buffers and may
> even be using two different buffering modes (e.g. line vs. block).
> You can't interleave writes to stderr and stdout and assume order is
> preserved unless you take specific steps (such as forcing them both to
> be unbuffered or flushing them at certain points).
> 
>> Is this guarantee no more valid in Python 3.2 ?
> 
> If you write to stderr all three times, it should work the way you want
> it to.

It seems it doesn't do so in my case.

Thanks,
Helmut.





More information about the Python-list mailing list