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

Discussion in 'Python' started by Helmut Jarausch, Aug 13, 2012.

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

    Is this guarantee no more valid in Python 3.2 ?

    Many thanks for a comment,
    Helmut.

    (That's a single-threaded application)
     
    Helmut Jarausch, Aug 13, 2012
    #1
    1. Advertising

  2. On 2012-08-13, Helmut Jarausch <> 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',
    >
    > 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.

    --
    Grant Edwards grant.b.edwards Yow! ... I'm IMAGINING a
    at sensuous GIRAFFE, CAVORTING
    gmail.com in the BACK ROOM of a
    KOSHER DELI --
     
    Grant Edwards, Aug 13, 2012
    #2
    1. Advertising

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

    > On 2012-08-13, Helmut Jarausch <> 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.
     
    Helmut Jarausch, Aug 13, 2012
    #3
  4. Helmut Jarausch <> writes:

    > On Mon, 13 Aug 2012 15:43:31 +0000, Grant Edwards wrote:
    >
    >> On 2012-08-13, Helmut Jarausch <> 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 ...


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

    -- Alain.
     
    Alain Ketterlin, Aug 14, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Neuhauser
    Replies:
    0
    Views:
    670
    Roman Neuhauser
    Apr 4, 2005
  2. GinTon

    sys.stderr.write and sys.exit

    GinTon, Nov 23, 2006, in forum: Python
    Replies:
    5
    Views:
    588
    Dennis Lee Bieber
    Nov 24, 2006
  3. Mitchell L Model

    sys.stdout vs. sys.stderr

    Mitchell L Model, Jan 11, 2010, in forum: Python
    Replies:
    2
    Views:
    578
    Nobody
    Jan 11, 2010
  4. Michel Albert
    Replies:
    5
    Views:
    1,447
    Daniel Dehennin
    Oct 24, 2012
  5. Jai
    Replies:
    1
    Views:
    187
    Neil Cerutti
    Nov 19, 2013
Loading...

Share This Page