Keeping a process open and communicating with it in Python
<p>I am doing some iterations using python script. What I do is something like this.</p>

<p>python exec.py variables.in.1 output.out.1</p>

<p>Here 1 is the iteration number. </p>

<p>Now what this does is, it calls an executable using the values in the variables file and then the output of the executable is put into the output file. Unfortunately what I see is that it takes more time by calling the executable again and again. </p>

<p>In stead what I want to do is start the executable once and then send the variables to the executable using the same syntax, i.e. python exec.py variables.in.1 output.out.1.</p>

<p>I need to preserve this syntax is because the variables file is generated externally. I have no control over it, but once it is generated I need to use the executable to create the output file which will then need to be parsed in the required format.</p>

<p>EDIT: I'll try to represent what I am doing in a simplified manner, since its a large code.</p>

<pre><code>import sys
import os
import parseinput
import parseoutput

s1=sys.argv[1];s2=sys.argv[2]

op=open(s1,'r');inp=op.readlines();op.close()
variableswrite=parseinput.parseinput(inp)
op=open('pass.dat','w');op.write(variableswrite);op.close()

os.sytem('execute &lt; pass.dat')

op=open('executeout.dat','r');out=op.readlines();op.close()
outwrite=parseoutput.parseoutput(out)
op=open(s2,'w');op.write(outwrite);op.close()
</code></pre>

<p>This is a very crude way of showing what I am doing. If this is the code exec.py, every time I run it using python exec.py variables.in.1 results.out.1, I'll get an output file using the executable which will be parsed to get the output file. </p>

<p>Instead, if I could keep the 'execute' executable open, and still do python exec.py variables.in.1 results.out.1 where the script would parse the input file and then send it to the open executable which would then create the output, my job would be much faster and efficient. </p>
