Python reading output from a running, external process

Matt Goodall matt at pollenation.net
Wed Sep 10 06:16:57 EDT 2003


On Wed, 2003-09-10 at 10:52, Brian Elmegaard wrote:
> 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
-- 
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: matt at pollenation.net





More information about the Python-list mailing list