Python reading output from a running, external process

B

Brian Elmegaard

Hi,

I am not sure if this is a python or a wx question. However, I am
working on a GUI for a simulation tool written in fortran. The output
from this program during simulation written to memory and at the end
to a file. Probably, this is the most portable way of handling
output(?)

Now, if I would like to build the GUI, so every time the simulator
writes new output to the output buffer in memory, the GUI would update
the onscreen output, could I do that? If not, is there any way to do
it except making a library function/dll from the application and run
that from python?

PS: I have taken a look at mmap, and perhaps this is what I am looking
for? But can the file to open be the output buffer from a process? On
windows? On unix?

tia
 
M

Matt Goodall

Hi,

I am not sure if this is a python or a wx question. However, I am
working on a GUI for a simulation tool written in fortran. The output
from this program during simulation written to memory and at the end
to a file. Probably, this is the most portable way of handling
output(?)

Now, if I would like to build the GUI, so every time the simulator
writes new output to the output buffer in memory, the GUI would update
the onscreen output, could I do that? If not, is there any way to do
it except making a library function/dll from the application and run
that from python?

PS: I have taken a look at mmap, and perhaps this is what I am looking
for? But can the file to open be the output buffer from a process? On
windows? On unix?

I'm not sure whether this will really help since your simulation tool
writes output to memory rather than stdout but it sounds like you need
os.popen(). See http://www.python.org/doc/current/lib/os-process.html
and
http://www.python.org/doc/current/lib/os-newstreams.html#os-newstreams.

Basically, os.popen() would allow you to execute the simulation tool and
have its output channelled back to the GUI application through a
file-like object.

I don't know wx much but it is generally possible to attach a file (i.e.
the file returned by popen) to the GUI's event queue so that one of your
event handlers is called when there is new output from the simulation
tool. It should be easy enough to parse that to update the UI.

Hope this helps.

Cheers, Matt
 
R

Robert Amesz

Matt said:
Basically, os.popen() would allow you to execute the simulation
tool and have its output channelled back to the GUI application
through a file-like object.

I don't know wx much but it is generally possible to attach a file
(i.e. the file returned by popen) to the GUI's event queue so that
one of your event handlers is called when there is new output from
the simulation tool. It should be easy enough to parse that to
update the UI.


In my experience it is *not* a good idea to use popen() in wxPython
apps, at least not under Windows: there seems to be some buffering
going on in popen(), totally ruining any sense of concurrency. Also,
reading the streams returned by popen() block, something to be avoided
at all costs in a GUI program. Finally, the process exit code is lost
when using popen().

Fortunately, in wxPython you can use a wxProcess, which doesn't buffer
or block, and can be polled using timer or idle events. There's an
example of how to use it in the wxPython demo.


Robert Amesz
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top