using subprocess for non-terminating command

O.R.Senthil Kumaran orsenthil at users.sourceforge.net
Wed Jul 4 15:29:31 EDT 2007


* Jerry Hill <malaclypse2 at gmail.com> [2007-07-04 11:23:33]:
> 
>  That's because you tied stdin to a pipe in your Popen call, but then
>  tried to read from stdout.  Try this instead:

My mistake. I had just 'typed' the command in the mail itself and forgot to
include the stdin, stdout, and stderr and mentioned it as hung based on some
recollection.

> 
> >>> process = subprocess.Popen("ping -c 10 127.0.0.1",
>  stdout=subprocess.PIPE, shell=True)
> >>> process.stdout.readlines()

I tried it again and found that giving the -c 10 returns a well defined
output. 
Only when the program has executed and the output available, subprocess can
read through PIPE's stdout it seems ( not at any other time). 
With killing, I loose the output.

>>> process = subprocess.Popen('ping 10 127.0.0.1', stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,shell=True)
>>> process.pid
3475
>>> import os
>>> import signal
>>> os.kill(process.pid,signal.SIGINT)
>>> process.stdout.read()
''
>>> # required output is lost!


-- 
O.R.Senthil Kumaran
http://uthcode.sarovar.org



More information about the Python-list mailing list