473,242 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

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 2366
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Darren Dale | last post by:
I'm a developer on the matplotlib project, and I am having trouble with the subprocess module on windows (Python 2.4.2 on winXP). No trouble to report with linux. I need to use _subprocess instead...
3
by: Fuzzyman | last post by:
Hello all, Before I ask the question a couple of notes : * This question is for implementing a script inside the Wing IDE. For some reason using the subprocess module doesn't work so I need a...
3
by: Tom Plunket | last post by:
I'm using subprocess to launch, well, sub-processes, but now I'm stumbling due to blocking I/O. Is there a way for me to know that there's data on a pipe, and possibly how much data is there so...
13
by: bayer.justin | last post by:
Hi, I am trying to communicate with a subprocess via the subprocess module. Consider the following example: <subprocess.Popen object at 0x729f0> Here hey is immediately print to stdout of...
9
by: Phoe6 | last post by:
Hi all, Consider this scenario, where in I need to use subprocess to execute a command like 'ping 127.0.0.1' which will have a continuous non- terminating output in Linux. # code # This...
12
by: bhunter | last post by:
Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's...
0
by: Christian Heimes | last post by:
Dominique.Holzwarth@ch.delarue.com schrieb: The pdflatex job stales when the standard stream buffers are full. Why do you need stdin anyway? The community method should do the trick for you: ...
8
by: rdabane | last post by:
I'm trying to perform following type of operation from inside a python script. 1. Open an application shell (basically a tcl ) 2. Run some commands on that shell and get outputs from each command...
2
by: newlasdjfk | last post by:
I have read tons of posts but still can't seem to figure it out. I want to subprocess.Popen() rsync.exe in windows, and print the stdout in python. This works... import subprocess, time, os,...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.