Repeatedly write to stdin and read from stdout of a process from python
<p>I have a piece of fortran code that reads some numbers from STDIN and writes results to STDOUT. For example:</p>

<pre><code>do
  read (*,*) x
  y = x*x
  write (*,*) y
enddo
</code></pre>

<p>So I can start the program from a shell and get the following sequence of <em>inputs</em>/<strong>outputs</strong>:</p>

<p><em>5.0</em><br>
<strong>25.0</strong><br>
<em>2.5</em><br>
<strong>6.25</strong>  </p>

<p>Now I need to do this from within python. After futilely wrestling with subprocess.Popen and looking through old questions on this site, I decided to use pexpect.spawn:</p>

<pre><code>import pexpect, os
p = pexpect.spawn('squarer')
p.setecho(False)
p.write("2.5" + os.linesep)
res = p.readline()
</code></pre>

<p>and it works. The problem is, the <strong>real</strong> data I need to pass between python and my fortran program is an array of 100,000 (or more) double precision floats. If they're contained in an array called <code>x</code>, then</p>

<pre><code>p.write(' '.join(["%.10f"%k for k in x]) + os.linesep)
</code></pre>

<p>times out with the following error message from pexpect:</p>

<pre><code>buffer (last 100 chars):   
before (last 100 chars):   
after: &lt;class 'pexpect.TIMEOUT'&gt;  
match: None  
match_index: None  
exitstatus: None
flag_eof: False
pid: 8574
child_fd: 3
closed: False
timeout: 30
delimiter: &lt;class 'pexpect.EOF'&gt;
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
</code></pre>

<p>unless <code>x</code> has less than 303 elements. Is there a way to pass large amounts of data to/from STDIN/STDOUT of another program?</p>

<p>I have tried splitting the data into smaller chunks, but then I lose a <strong>lot</strong> in speed.</p>

<p>Thanks in advance.</p>
