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

H

Helmut Jarausch

Hi,

for tracing purposes I have added some print outs like

print('+++ before calling foo',file=sys.stderr)
x=foo(..)
print('--- after calling foo',

and within 'foo'
print('>>> entering foo ...',file=sys.stderr)

Now, when executing this, I always get

+++ before calling foo
--- after calling 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.

Is this guarantee no more valid in Python 3.2 ?

Many thanks for a comment,
Helmut.

(That's a single-threaded application)
 
G

Grant Edwards

Hi,

for tracing purposes I have added some print outs like

print('+++ before calling foo',file=sys.stderr)
x=foo(..)
print('--- after calling foo',

and within 'foo'
print('>>> entering foo ...',file=sys.stderr)

Now, when executing this, I always get

+++ before calling foo
--- after calling 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.
 
H

Helmut Jarausch

Sorry, this is a cut'n paste error. I did use
print('--- after calling foo',file=sys.stderr)
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).


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

Alain Ketterlin

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

This can't happen with "normal" code. Are you playing with lambdas or
generators here? Or anything else that could introduce lazyness?

-- Alain.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top