print problem

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

    print problem

    I was trying to print a dot on console every second to indicates
    running process, so I wrote, for example:

    for i in xrange(10):
    print ".",
    time.sleep(1)

    Idealy, a dot will be printed out each second. But there is nothing
    print out until after 10 seconds, all 10 dots come out together.

    I've tried lose the comma in the print statement, and it works.

    Is that because of the print statement buffer the characters until
    there is a new line character?

    Thanks
  • Gabriel Genellina

    #2
    Re: print problem

    En Tue, 17 Jun 2008 03:15:11 -0300, pirata <pirata@mars.in validescribió:
    I was trying to print a dot on console every second to indicates
    running process, so I wrote, for example:
    >
    for i in xrange(10):
    print ".",
    time.sleep(1)
    >
    Idealy, a dot will be printed out each second. But there is nothing
    print out until after 10 seconds, all 10 dots come out together.
    >
    I've tried lose the comma in the print statement, and it works.
    >
    Is that because of the print statement buffer the characters until
    there is a new line character?
    Very probably, altough I can't reproduce it on Windows. Try invoking the script with python -u (unbuffered input/output):

    python -u your_script.py

    --
    Gabriel Genellina

    Comment

    • Rich Healey

      #3
      Re: print problem

      -----BEGIN PGP SIGNED MESSAGE-----
      Hash: SHA1

      Gabriel Genellina wrote:
      En Tue, 17 Jun 2008 03:15:11 -0300, pirata <pirata@mars.in validescribió:
      >
      >I was trying to print a dot on console every second to indicates
      >running process, so I wrote, for example:
      >>
      >for i in xrange(10):
      > print ".",
      > time.sleep(1)
      >>
      >Idealy, a dot will be printed out each second. But there is nothing
      >print out until after 10 seconds, all 10 dots come out together.
      >>
      >I've tried lose the comma in the print statement, and it works.
      >>
      >Is that because of the print statement buffer the characters until
      >there is a new line character?
      >
      Very probably, altough I can't reproduce it on Windows. Try invoking the script with python -u (unbuffered input/output):
      >
      python -u your_script.py
      >
      Or just write to sys.stdout without the print wrapper..

      - --
      Rich Healey - healey.rich@gma il.com
      Developer / Systems Admin - OpenPGP: 0x8C8147807
      MSN: bitchohealey@ho tmail.com AIM: richohealey33
      irc.psych0tik.n et -#hbh #admins richohealey
      irc.freenode.or g -#hbh #debian PythonNinja
      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.4.6 (GNU/Linux)
      Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

      iD8DBQFIV2NxLeT fO4yBSAcRAkunAJ 9w5lavHK4TIUbex X+pSYmPla9oOQCf T8tM
      tzOhQgcpO7dEG7W hE6FNZ4w=
      =IqJ1
      -----END PGP SIGNATURE-----

      Comment

      • Gabriel Genellina

        #4
        Re: print problem

        En Tue, 17 Jun 2008 04:10:41 -0300, Rich Healey <healey.rich@gm ail.comescribió :
        Gabriel Genellina wrote:
        >En Tue, 17 Jun 2008 03:15:11 -0300, pirata <pirata@mars.in validescribió:
        >>
        >>I was trying to print a dot on console every second to indicates
        >>running process, so I wrote, for example:
        >>>
        >>for i in xrange(10):
        >> print ".",
        >> time.sleep(1)
        >>>
        >>Idealy, a dot will be printed out each second. But there is nothing
        >>print out until after 10 seconds, all 10 dots come out together.
        >>>
        >>I've tried lose the comma in the print statement, and it works.
        >>>
        >>Is that because of the print statement buffer the characters until
        >>there is a new line character?
        >>
        >Very probably, altough I can't reproduce it on Windows. Try invoking the script with python -u (unbuffered input/output):
        >>
        >python -u your_script.py
        >>
        Or just write to sys.stdout without the print wrapper..
        I think the output is still buffered, even if you write directly to sys.stdout, but I don't have a Linux box to test right now.

        --
        Gabriel Genellina

        Comment

        • Gabriel Genellina

          #5
          Re: print problem

          En Tue, 17 Jun 2008 04:10:41 -0300, Rich Healey <healey.rich@gm ail.comescribió :
          Gabriel Genellina wrote:
          >En Tue, 17 Jun 2008 03:15:11 -0300, pirata <pirata@mars.in validescribió:
          >>
          >>I was trying to print a dot on console every second to indicates
          >>running process, so I wrote, for example:
          >>>
          >>for i in xrange(10):
          >> print ".",
          >> time.sleep(1)
          >>>
          >>Idealy, a dot will be printed out each second. But there is nothing
          >>print out until after 10 seconds, all 10 dots come out together.
          >>>
          >>I've tried lose the comma in the print statement, and it works.
          >>>
          >>Is that because of the print statement buffer the characters until
          >>there is a new line character?
          >>
          >Very probably, altough I can't reproduce it on Windows. Try invoking the script with python -u (unbuffered input/output):
          >>
          >python -u your_script.py
          >>
          Or just write to sys.stdout without the print wrapper..
          I think the output is still buffered, even if you write directly to sys.stdout, but I don't have a Linux box to test right now.

          --
          Gabriel Genellina

          Comment

          • Chris

            #6
            Re: print problem

            On Jun 17, 8:15 am, pirata <pir...@mars.in validwrote:
            I was trying to print a dot on console every second to indicates
            running process, so I wrote, for example:
            >
            for i in xrange(10):
                print ".",
                time.sleep(1)
            >
            Idealy, a dot will be printed out each second. But there is nothing
            print out until after 10 seconds, all 10 dots come out together.
            >
            I've tried lose the comma in the print statement, and it works.
            >
            Is that because of the print statement buffer the characters until
            there is a new line character?
            >
            Thanks
            import sys
            for i in xrange(10):
            sys.stdout.writ e('.')
            sys.stdout.flus h()

            Comment

            • Peter Pearson

              #7
              Re: print problem

              On Tue, 17 Jun 2008 04:46:38 -0300, Gabriel Genellina wrote:
              En Tue, 17 Jun 2008 04:10:41 -0300, Rich Healey escribió:
              >Gabriel Genellina wrote:
              >>En Tue, 17 Jun 2008 03:15:11 -0300, pirata <pirata@mars.in validescribió:
              >>>
              >>>I was trying to print a dot on console every second to indicates
              >>>running process, so I wrote, for example:
              >>>>
              >>>for i in xrange(10):
              >>> print ".",
              >>> time.sleep(1)
              >>>>
              >>>Idealy, a dot will be printed out each second. But there is nothing
              >>>print out until after 10 seconds, all 10 dots come out together.
              >>>>
              >>>I've tried lose the comma in the print statement, and it works.
              >>>>
              >>>Is that because of the print statement buffer the characters until
              >>>there is a new line character?
              [snip]
              >Or just write to sys.stdout without the print wrapper..
              >
              I think the output is still buffered, even if you write
              directly to sys.stdout, but I don't have a Linux box to
              test right now.
              Here (Python 2.4.3), dots written to sys.stdout are saved up and
              appear all at once.

              However, you can flush the buffer yourself by invoking sys.stdout.flus h()
              immediately after either the "print" or the "sys.stdout.wri te".

              --
              To email me, substitute nowhere->spamcop, invalid->net.

              Comment

              Working...