real time updating of popen, bufsize=0 problems

  • Thread starter =?iso-8859-1?B?aWFuYXLp?=
  • Start date
?

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

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

kyosohma

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
 
?

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

R

Rob Wolfe

ianaré said:
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
 
?

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

ianaré said:
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

Rob, you are the man =) thanks!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top