[Tutor] Difficulty with the subprocess module

Alan Gauld alan.gauld at btinternet.com
Fri Mar 2 09:13:49 CET 2007


"Dhiraj Sharma" <dhirajsharma at optonline.net> wrote 

> ----------------
>   p = subprocess.Popen("cat", buffer=0, stdin=subprocess.PIPE,
>       stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
> 
>   fdata = open('input');
>   for l in fdata:
>      p.stdin.write(l)
>      m = p.stdout.readline()
>      sys.stdout.write(m)
> ----------------
> 
> If I replace the "cat" process by "sed -e s/a/x/g", the 
> script hangs on the readline() call -- as indicated by 

> I would appreciate guidance regarding:
> 1. Why is the script behaving differently for cat and sed?

At a guess... sed doesn't process the data line by line so 
you will have to send all lines to sed (plus maybe an EOF marker?)
before trying to read the output.
Frankly that would surprise me since sed is supposed to be a 
steam editor so I wold expect it to be line oriented, but 
it would be one explanation...

Another might be that you have to explicitly flush() stdin 
after writing to it?

> 2. Why the python subprocess "kills" the parent script?

No idea!

> 3. What is the right way to write the above scripts?

You look to be on the right track to me.
But any kind of communication between programs via stdin/stdout
is frought with difficulty because of the inconsistencies in the 
behaviours of programs. Its always much better to use an API 
if one exists. And often its easier to just write the specific 
function you need in Python.

In the case of sed a regex replace function is probably easier!
And probably faster too since it avoids the process startup 
overhead...

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list