Interprocess comunication

MRAB python at mrabarnett.plus.com
Thu Jun 7 12:29:02 EDT 2012


On 07/06/2012 17:04, Julio Sergio wrote:
> I'm trying to call an external process to filter some of my data, i.e., I'm
> trying to pass some information to the called process, and have this information
> back transformed. I started testing with the linux 'cat' command, in this way:
>
>
> ->>>  import subprocess as sp
> ->>>  p = sp.Popen(["cat"],stdin=sp.PIPE,stdout=sp.PIPE,close_fds=True)
> ->>>  (fi,fo) = (p.stdin, p.stdout)
> ->>>  fi.write("SOMETHING\n")
> ->>>  fi.flush()
> ->>>  fo.readline()
> 'SOMETHING\n'
> ->>>  fi.write("OTHER\n")
> ->>>  fi.flush()
> ->>>  fo.readline()
> 'OTHER\n'
> ->>>  fi.write("NEXT\n")
> ->>>  fi.flush()
> ->>>  fo.readline()
> 'NEXT\n'
> ->>>  fi.write("NEXT1\n")
> ->>>  fi.flush()
> ->>>  s = fo.readline()
> ->>>  s
> 'NEXT1\n'
>
> Up to this point it worked as expected. However, when I tryied with the methods
> that write and read several lines, apparently the process got stalled:
>
> ->>>  fi.writelines(["uno\n","dos\n","tres\n"])
> ->>>  fi.flush()
> ->>>  s = fo.readlines()
> .
> .
> .
>
> Do you have any comments on this?
>
I believe it's waiting for the end of the input, i.e. for the pipe to
close.

Have you tried calling fo.readline() 3 times instead?



More information about the Python-list mailing list