print without newline "halts" program execution

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Karlo Lozovina

    #1

    print without newline "halts" program execution

    Consider this short script:

    ---
    from time import time, sleep

    st = time()
    print 'Start: %f, ' % st,
    sleep(10)
    sp = time()
    print 'Stop: %f, Duration: %f' % (sp, (st - sp))
    ---

    On my environment (Linux, py24), when run, Python first waits 10s, and
    then produces the entire output. How, can I make it print first part
    ('Start: %f, '), then wait 10s, and then append (WITHOUT NEWLINE) that
    second print statement?

    I'm writting a script with lot of output which has to go on the same line,
    but I don't want to wait for it to end to see output, I just want it to
    print parts as it's finished with them.

    Using sys.stdout.writ e() produces the same behavior, so what can I do?

    Thanks a lot in advance.

    --
    _______ Karlo Lozovina - Mosor
    | | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
    | || _ | _ | Parce mihi domine quia Dalmata sum.
    |__|_|__||_____ |_____|
  • Jay Parlar

    #2
    Re: print without newline "halts&quo t; program execution


    On Apr 13, 2006, at 5:57 PM, Karlo Lozovina wrote:
    [color=blue]
    > Consider this short script:
    >
    > ---
    > from time import time, sleep
    >
    > st = time()
    > print 'Start: %f, ' % st,
    > sleep(10)
    > sp = time()
    > print 'Stop: %f, Duration: %f' % (sp, (st - sp))
    > ---
    >
    > On my environment (Linux, py24), when run, Python first waits 10s, and
    > then produces the entire output. How, can I make it print first part
    > ('Start: %f, '), then wait 10s, and then append (WITHOUT NEWLINE) that
    > second print statement?
    >
    > I'm writting a script with lot of output which has to go on the same
    > line,
    > but I don't want to wait for it to end to see output, I just want it to
    > print parts as it's finished with them.
    >
    > Using sys.stdout.writ e() produces the same behavior, so what can I do?
    > from time import time, sleep
    >
    >[/color]

    Your problem is that the 'print' statement is sending the text to
    sys.stdout, and sys.stdout is buffered.

    There are other ways to do this, but try this:

    from time import time, sleep
    import sys

    st = time()
    print 'Start: %f,'% st,
    sys.stdout.flus h()
    sleep(10)
    sp = time()
    print 'Stop: %f, Duration: %f' % (sp, (st-sp))


    Jay P.

    Comment

    • Karlo Lozovina

      #3
      Re: print without newline "halts&quo t; program execution

      Jay Parlar <jparlar@cogeco .ca> wrote in
      news:mailman.45 37.1144976780.2 7775.python-list@python.org :
      [color=blue]
      > Your problem is that the 'print' statement is sending the text to
      > sys.stdout, and sys.stdout is buffered.[/color]

      I thought it was something like this, but I couldn't find it in the docs :
      (.
      [color=blue]
      > print 'Start: %f,'% st,
      > sys.stdout.flus h()
      > sleep(10)
      > sp = time()
      > print 'Stop: %f, Duration: %f' % (sp, (st-sp))[/color]

      This works excellent, *thank* you.
      [color=blue]
      > There are other ways to do this, but try this:[/color]

      And for purely academical purposes, what are those other ways to do it?
      I'm curious :).

      --
      _______ Karlo Lozovina - Mosor
      | | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
      | || _ | _ | Parce mihi domine quia Dalmata sum.
      |__|_|__||_____ |_____|

      Comment

      Working...