[Tutor] working intime on pipes

Mats Wichmann mats at wichmann.us
Thu Apr 13 15:28:17 EDT 2023


On 4/13/23 04:47, Jan Kenin wrote:
> Hello,
> 
> I have command, that runs pretty long and outputs many lines, that I
> would like to filter. I call the command with
> 
> p=subprocess.Popen( command, stdout=out )
> 
> p.wait()
> 
> If out were sys.stdout, all output would be printed to the screen, which
> I dont want to.
> 
> If out were an object with a write( *args ) attribute, this is not
> called by p. p just calls out.fileno() and all is written to out without
> using the write attribute.
> 
> How can I work on the lines sent to out, _before_ p has finished?
> 
> Thanks for usefull advice!

If your question is how can you work with the output from the command 
before the command is complete, you want to create the Popen object with 
stdout pointing back to you (stdout=subprocess.PIPE), and then use 
p.poll(). As long as poll returns None, it's still running, but you can 
read stuff.  It's possible you may have to fiddle buffering, etc. in 
order to get things working as you want.  When it is done, poll() 
returns the returncode, to let you know the completion status.







More information about the Tutor mailing list