468,761 Members | 1,824 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,761 developers. It's quick & easy.

Catching subprocess stdout stream

Dear all

I have tkinkter based frontend to a Fortran based program. I use
subprocess to launch the fortran program as a child process and I wish
to see the output of the fortran program as it is created in the
console.

The fortran program can take up to 20 minuttes to finish and at the
moment the I will first see any output after the fortran program is
done. How make my function write the output of the process as it
comes?

def runprogram(Icommand, Ijobfile, Ioutput):
if os.name == "posix":
os.system(pythonpath+"/bin/"+Icommand+"< "+Ijobfile+" | tee
"+Ioutput)
elif os.name == "nt":
import subprocess
ofile = open(Ioutput, 'w')
p = subprocess.Popen([os.path.join(pythonpath, "bin", Icommand
+ '.exe')],
stdin=open(Ijobfile,
"rb"),bufsize=1024,shell=False,
stdout=subprocess.PIPE)

while p.poll() is None: #Check if child process has terminated.
o = p.stdout.readline()
ofile.writelines(o)
print o,
ofile.close

Kind regards
Thomas Jansson
Sep 8 '08 #1
3 2210
On Sep 8, 8:37*am, Thomas Jansson <tjansso...@gmail.comwrote:
Dear all

I have tkinkter based frontend to a Fortran based program. I use
subprocess to launch the fortran program as a child process and I wish
to see the output of the fortran program as it is created in the
console.

The fortran program can take up to 20 minuttes to finish and at the
moment the I will first see any output after the fortran program is
done. How make my function write the output of the process as it
comes?

def runprogram(Icommand, Ijobfile, Ioutput):
* * if os.name == "posix":
* * * * os.system(pythonpath+"/bin/"+Icommand+"< "+Ijobfile+" | tee
"+Ioutput)
* * elif os.name == "nt":
* * * * import subprocess
* * * * ofile = open(Ioutput, 'w')
* * * * p = subprocess.Popen([os.path.join(pythonpath, "bin", Icommand
+ '.exe')],
* * * * * * * * * * * * * * *stdin=open(Ijobfile,
"rb"),bufsize=1024,shell=False,
* * * * * * * * * * * * * * *stdout=subprocess.PIPE)

* * * * while p.poll() is None: #Check if child process has terminated.
* * * * * * o = p.stdout.readline()
* * * * * * ofile.writelines(o)
* * * * * * print o,
* * * * ofile.close

Kind regards
Thomas Jansson
import threading, Queue, subprocess

class iCommand(threading.Thread):
def __init__ (self, iCommand, iJobFile, queue):
threading.Thread.__init__(self)
self.iCommand = iCommand
self.queue = queue
self.iJobFile = iJobFile

def run(self):
p = subprocess.Popen([os.path.join("C:/Python25", "bin",
self.iCommand + '.exe')],
stdin=open(self.iJobFile,
"rb"),bufsize=1024,shell=False,
stdout=subprocess.PIPE)
while p.poll() == None:
q.put(p.stdout.readline())

command = "FOO"
jobFile = ="FAH"
aQueue = Queue.Queue()
fo = open("C:/temp/foo.out", 'w')

aThread = iCommand(command, jobFile, aQueue)
aThread.start()

while aThread.isAlive() or not aQueue.empty():
l = aQueue.get().rstrip()
fo.write(l)
print l

fo.close()

A bit of fun for a sleepless night...

~Sean
Sep 10 '08 #2
Sean DiZazzo wrote:
while aThread.isAlive() or not aQueue.empty():
l = aQueue.get().rstrip()
fo.write(l)
print l

fo.close()

A bit of fun for a sleepless night...
and unless you add a call to time.sleep somewhere in that loop, you'll
keep your CPU up all night as well...

</F>

Sep 10 '08 #3
On Sep 8, 5:37*pm, Thomas Jansson <tjansso...@gmail.comwrote:
Dear all

I have tkinkter based frontend to a Fortran based program. I use
subprocess to launch the fortran program as a child process and I wish
to see the output of the fortran program as it is created in the
console.

The fortran program can take up to 20 minuttes to finish and at the
moment the I will first see any output after the fortran program is
done. How make my function write the output of the process as it
comes?
Sometimes very low technology solutions may be enough. For instance on
Unix
you could call os.system on something like that:

xterm -e "the-fortran-executable|less"

Make sure the Fortran executable output is unbuffered.

Michele Simionato
Sep 10 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Darren Dale | last post: by
3 posts views Thread by Tom Plunket | last post: by
9 posts views Thread by Phoe6 | last post: by
12 posts views Thread by bhunter | last post: by
reply views Thread by Christian Heimes | last post: by
8 posts views Thread by rdabane | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.