Accessing Console Application IO
I have a linux console application - a scientific simulation program that I use. It opens up a TCL shell that you then issue commands to. Normally what I do is pre-write all my test vectors and look at the output by manually inputting the data, but now I'd like to move on to something more complicated: incorporating external feedback.
My idea is, I'll have a external simulation running that takes the output of the simulator and then generates new test vectors on the fly to feed back into the simulation. I'm kind of hazy on the on the details of how to implement this. I am semi-fa开发者_开发技巧miliar with C and with Python.
I guess, getting into specifics - how do I hook into the program's terminal I/O? I'd prefer to use Python if possible. Are there any references I can read to get up to speed on this?
Your idea is quite reasonable. Python supports this very well: Sub-process launching, and inter-process communication. Documentation like the following might be helpful:
http://docs.python.org/library/subprocess.html
In short, you're going to "read from" child-process stdout (and maybe stderr), and "write to" child-process stdin. You can have your interactive console like you describe, or read from/write to text files, and even "hook-together" processes to talk (like piping "mycommand | mycommand2").
For Python, there are many strong examples (like the "scons" build system written in Python that does this a lot). Further, the Qt's QProcess
class makes this pretty easy, and there are a few really good Python wrappers available like "PySide", "PyQt", and "PythonQt" (probably others).
精彩评论