Keeping a pipe to a process open
Question: I have an `app` that reads in stuff from `stdin` and returns, after a newline,
results to `stdout`

A simple (stupid) example:

    
    
    $ app
    Expand[(x+1)^2]<CR>
    x^2 + 2*x + 1
    100 - 4<CR>
    96
    

Opening and closing the `app` requires a lot of initialization and clean-up
(its an interface to a Computer Algebra System), so I want to keep this to a
minimum.

I want to open a pipe in Python to this process, write strings to its `stdin`
and read out the results from `stdout`. `Popen.communicate()` doesn't work for
this, as it closes the file handle, requiring to reopen the pipe.

I've tried something along the lines of this related question: [Communicate
multiple times with a process without breaking the
pipe?](http://stackoverflow.com/questions/3065060/communicate-multiple-times-
with-a-process-without-breaking-the-pipe) but I'm not sure how to wait for the
output. It is also difficult to know a priori how long it will take the `app`
to finish to process for the input at hand, so I don't want to make any
assumptions. I guess most of my confusion comes from this question: [Non-
blocking read on a subprocess.PIPE in
python.](http://stackoverflow.com/questions/375427/non-blocking-read-on-a-
subprocess-pipe-in-python) where it is stated that mixing high and low level
functions is not a good idea.

**EDIT** : Sorry that I didn't give any code before, got interrupted. This is
what I've tried so far and it seems to work, I'm just worried that something
goes wrong unnoticed:

    
    
    from subprocess import Popen, PIPE
    pipe = Popen(["MathPipe"], stdin=PIPE, stdout=PIPE)         
    expressions = ["Expand[(x+1)^2]", "Integrate[Sin[x], {x,0,2*Pi}]"] # ...                                                                                        
    for expr in expressions:
        pipe.stdin.write(expr)                                                        
        while True:                                                                         
            line = pipe.stdout.readline()                                                    
            if line != '':     
                print line  
            # output of MathPipe is always terminated by ';'                                                                                                                         
            if ";" in line:                                                                  
                break                                                                       
    

Potential problems with this?


Answer: Using subprocess, you can't do this reliably. You might want to look at using
the [pexpect](http://www.noah.org/wiki/Pexpect) library. That won't work on
Windows - if you're on Windows, try
[winpexpect](http://pypi.python.org/pypi/winpexpect/1.4).

Also, if you're trying to do mathematical stuff in Python, check out
[SAGE](http://sagemath.org/). They do a lot of work on interfacing with other
open-source maths software, so there's a chance they've already done what
you're trying to.

