real time updating of popen, bufsize=0 problems

Discussion in 'Python' started by =?iso-8859-1?B?aWFuYXLp?=, Apr 6, 2007.

  1. hey all, I'm trying to get real time updates of batch file output.

    Here is my batch file:
    @echo off
    echo 1
    @ping 127.0.0.1 -n 2 -w 1500 > nul
    echo 2
    @ping 127.0.0.1 -n 2 -w 1500 > nul
    echo 3

    If I run it in cmd.exe it will print "1", wait 15sec, print "2", wait
    15sec, print "3".

    I tried doing it like this:

    r, w, e = popen2.popen3('"C:/path/to/test.bat"',bufsize=0)
    for line in r:
    self.display.WriteText(line)

    .... but get: ValueError: popen3() arg 3 must be -1

    If I use -1, then it waits for the batch file to complete, and prints
    out all 3 lines at once.


    So I tried subprocess:
    proc = subprocess.Popen('"C:/path/to/test.bat"', bufsize=0,
    stdout=subprocess.PIPE)
    for line in proc.stdout:
    self.display.WriteText(line)

    No error message, but no real time printing either.

    info:
    self.display is a wx.TextCtrl - not that it should matter,as
    'WriteText()' behaves basically like 'print'
    winXP pro SP2, python 2.5, wxPython 2.6.3.3


    You help is appreciated.
     
    =?iso-8859-1?B?aWFuYXLp?=, Apr 6, 2007
    #1
    1. Advertising

  2. =?iso-8859-1?B?aWFuYXLp?=

    Guest

    On Apr 6, 1:44 pm, "ianaré" <> wrote:
    > hey all, I'm trying to get real time updates of batch file output.
    >
    > Here is my batch file:
    > @echo off
    > echo 1
    > @ping 127.0.0.1 -n 2 -w 1500 > nul
    > echo 2
    > @ping 127.0.0.1 -n 2 -w 1500 > nul
    > echo 3
    >
    > If I run it in cmd.exe it will print "1", wait 15sec, print "2", wait
    > 15sec, print "3".
    >
    > I tried doing it like this:
    >
    > r, w, e = popen2.popen3('"C:/path/to/test.bat"',bufsize=0)
    > for line in r:
    > self.display.WriteText(line)
    >
    > ... but get: ValueError: popen3() arg 3 must be -1
    >
    > If I use -1, then it waits for the batch file to complete, and prints
    > out all 3 lines at once.
    >
    > So I tried subprocess:
    > proc = subprocess.Popen('"C:/path/to/test.bat"', bufsize=0,
    > stdout=subprocess.PIPE)
    > for line in proc.stdout:
    > self.display.WriteText(line)
    >
    > No error message, but no real time printing either.
    >
    > info:
    > self.display is a wx.TextCtrl - not that it should matter,as
    > 'WriteText()' behaves basically like 'print'
    > winXP pro SP2, python 2.5, wxPython 2.6.3.3
    >
    > You help is appreciated.


    Hi,

    I think this script on another post will help:

    http://groups.google.com/group/comp.lang.python/msg/9fa3a3c287e8e2a3?hl=en&

    The 4 line code example (from Daniel) in one of the posts at this link
    worked with your batch file:
    http://www.velocityreviews.com/forums/t350573-redirect-ossystem-output.html

    Mike
     
    , Apr 6, 2007
    #2
    1. Advertising

  3. On Apr 6, 3:22 pm, wrote:
    > On Apr 6, 1:44 pm, "ianaré" <> wrote:
    >
    >
    >
    > > hey all, I'm trying to get real time updates of batch file output.

    >
    > > Here is my batch file:
    > > @echo off
    > > echo 1
    > > @ping 127.0.0.1 -n 2 -w 1500 > nul
    > > echo 2
    > > @ping 127.0.0.1 -n 2 -w 1500 > nul
    > > echo 3

    >
    > > If I run it in cmd.exe it will print "1", wait 15sec, print "2", wait
    > > 15sec, print "3".

    >
    > > I tried doing it like this:

    >
    > > r, w, e = popen2.popen3('"C:/path/to/test.bat"',bufsize=0)
    > > for line in r:
    > > self.display.WriteText(line)

    >
    > > ... but get: ValueError: popen3() arg 3 must be -1

    >
    > > If I use -1, then it waits for the batch file to complete, and prints
    > > out all 3 lines at once.

    >
    > > So I tried subprocess:
    > > proc = subprocess.Popen('"C:/path/to/test.bat"', bufsize=0,
    > > stdout=subprocess.PIPE)
    > > for line in proc.stdout:
    > > self.display.WriteText(line)

    >
    > > No error message, but no real time printing either.

    >
    > > info:
    > > self.display is a wx.TextCtrl - not that it should matter,as
    > > 'WriteText()' behaves basically like 'print'
    > > winXP pro SP2, python 2.5, wxPython 2.6.3.3

    >
    > > You help is appreciated.

    >
    > Hi,
    >
    > I think this script on another post will help:
    >
    > http://groups.google.com/group/comp.lang.python/msg/9fa3a3c287e8e2a3?...
    >
    > The 4 line code example (from Daniel) in one of the posts at this link
    > worked with your batch file:http://www.velocityreviews.com/forums/t350573-redirect-ossystem-outpu...
    >
    > Mike


    Thanks but it doesn't work. Still prints it out all at once. It is
    supposed to print, then wait 15sec for the next line to print.
     
    =?iso-8859-1?B?aWFuYXLp?=, Apr 6, 2007
    #3
  4. =?iso-8859-1?B?aWFuYXLp?=

    Rob Wolfe Guest

    "ianaré" <> writes:

    > hey all, I'm trying to get real time updates of batch file output.


    [...]

    > So I tried subprocess:
    > proc = subprocess.Popen('"C:/path/to/test.bat"', bufsize=0,
    > stdout=subprocess.PIPE)


    Instead of that:

    > for line in proc.stdout:
    > self.display.WriteText(line)


    try that:

    while True:
    line = proc.stdout.readline()
    if not line: break
    self.display.WriteText(line)


    When a file is used in a for loop it works like an iterator.
    You can read details here (description of method ``next``):
    http://docs.python.org/lib/bltin-file-objects.html

    --
    HTH,
    Rob
     
    Rob Wolfe, Apr 6, 2007
    #4
  5. On Apr 6, 3:59 pm, Rob Wolfe <> wrote:
    > "ianaré" <> writes:
    > > hey all, I'm trying to get real time updates of batch file output.

    >
    > [...]
    >
    > > So I tried subprocess:
    > > proc = subprocess.Popen('"C:/path/to/test.bat"', bufsize=0,
    > > stdout=subprocess.PIPE)

    >
    > Instead of that:
    >
    > > for line in proc.stdout:
    > > self.display.WriteText(line)

    >
    > try that:
    >
    > while True:
    > line = proc.stdout.readline()
    > if not line: break
    > self.display.WriteText(line)
    >
    > When a file is used in a for loop it works like an iterator.
    > You can read details here (description of method ``next``):http://docs.python.org/lib/bltin-file-objects.html
    >
    > --
    > HTH,
    > Rob


    Rob, you are the man =) thanks!
     
    =?iso-8859-1?B?aWFuYXLp?=, Apr 6, 2007
    #5
    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. Thomas =?ISO-8859-15?Q?G=FCttler?=

    Maxmium bufsize using open2?

    Thomas =?ISO-8859-15?Q?G=FCttler?=, Jul 10, 2003, in forum: Python
    Replies:
    2
    Views:
    354
    Piet van Oostrum
    Jul 12, 2003
  2. Jonathan Hudgins

    popen bufsize not allowed on windows

    Jonathan Hudgins, Jul 24, 2004, in forum: Python
    Replies:
    1
    Views:
    368
    Jonathan Hudgins
    Jul 25, 2004
  3. I. Myself
    Replies:
    2
    Views:
    503
    I. Myself
    Apr 30, 2006
  4. Steve Holden
    Replies:
    0
    Views:
    791
    Steve Holden
    Feb 8, 2009
  5. bufsize in subprocess

    , Jan 22, 2012, in forum: Python
    Replies:
    3
    Views:
    645
Loading...

Share This Page