Flushing stdout

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Graham Ashton

    Flushing stdout

    Hi. I'm having trouble flushing sys.stdout. I've written a small example
    to illustrate my problem (see below). In short, I expect it to ping
    "received hello", sleep for 5 seconds and then print "received world".
    Instead I get nothing for 5 seconds and then both statements pop out at
    once.

    As you'll no doubt gather from the example, the problem I'm really trying
    to solve is sending message back from one process to another via a pipe.
    Changing the buffering option in the os.pipe() call doesn't have any
    effect either.

    Any thoughts? Thanks.

    P.S. I've not found -u and PYTHONUNBUFFERE D to help me.

    P.P.S. I'm running 2.3.3 on Linux, libc 2.2.5-11.5.

    ---cut---
    #!/usr/bin/env python

    import os, sys, time

    if len(sys.argv) > 1:
    sys.stdout.writ e("hello\n")
    sys.stdout.flus h()
    time.sleep(5)
    sys.stdout.writ e("world\n")
    else:
    command = "python %s foo" % os.path.basenam e(sys.argv[0])
    for line in os.popen(comman d, "r", 1):
    print "received", line,

  • Steve Holden

    #2
    Re: Flushing stdout

    Graham Ashton wrote:[color=blue]
    > Hi. I'm having trouble flushing sys.stdout. I've written a small example
    > to illustrate my problem (see below). In short, I expect it to ping
    > "received hello", sleep for 5 seconds and then print "received world".
    > Instead I get nothing for 5 seconds and then both statements pop out at
    > once.
    >[/color]
    You aren't actually having trouble flushing sys.stdout at all ...
    [color=blue]
    > As you'll no doubt gather from the example, the problem I'm really trying
    > to solve is sending message back from one process to another via a pipe.
    > Changing the buffering option in the os.pipe() call doesn't have any
    > effect either.
    >[/color]
    Nope. The holdup is elsewhere.
    [color=blue]
    > Any thoughts? Thanks.
    >
    > P.S. I've not found -u and PYTHONUNBUFFERE D to help me.
    >[/color]
    Nope, they won't ...
    [color=blue]
    > P.P.S. I'm running 2.3.3 on Linux, libc 2.2.5-11.5.
    >
    > ---cut---
    > #!/usr/bin/env python
    >
    > import os, sys, time
    >
    > if len(sys.argv) > 1:
    > sys.stdout.writ e("hello\n")
    > sys.stdout.flus h()
    > time.sleep(5)
    > sys.stdout.writ e("world\n")
    > else:
    > command = "python %s foo" % os.path.basenam e(sys.argv[0])
    > for line in os.popen(comman d, "r", 1):
    > print "received", line,
    >[/color]
    The iteration over the pipe's contents is waiting until it sees the end
    of the output before it yields anything. Try this:

    import os, sys, time

    if len(sys.argv) > 1:
    sys.stdout.writ e("hello\n")
    sys.stdout.flus h()
    time.sleep(5)
    sys.stdout.writ e("world\n")
    else:
    command = "python %s foo" % os.path.basenam e(sys.argv[0])
    f = os.popen(comman d, "r", 1)
    while 1:
    line = f.readline()
    if not line:
    break
    print "received", line,

    You can probably find a tidier way to bundle this now you know what's up.

    regards
    Steve
    --


    Holden Web LLC +1 800 494 3119

    Comment

    • Graham Ashton

      #3
      Re: Flushing stdout

      On Mon, 01 Nov 2004 16:41:04 -0500, Steve Holden wrote:
      [color=blue]
      > Graham Ashton wrote:[color=green]
      >> Hi. I'm having trouble flushing sys.stdout.[/color]
      > You aren't actually having trouble flushing sys.stdout at all ...
      >[color=green]
      >> As you'll no doubt gather from the example, the problem I'm really trying[/color]
      >
      > The iteration over the pipe's contents is waiting until it sees the end
      > of the output before it yields anything.[/color]

      Genius. Thanks Steve, much appreciated. Now I can actually send our users
      some feedback...

      Graham

      Comment

      Working...