using subprocess for non-terminating command

Steve Holden steve at holdenweb.com
Wed Jul 4 17:59:10 CEST 2007


O.R.Senthil Kumaran wrote:
> * zacherates <zacherates at gmail.com> [2007-07-04 12:09:03]:
> 
>>> How should I handle these kind of commands (ping 127.0.0.1) with
>>> subprocess module. I am using subprocess, instead of os.system because
>>> at anypoint in time, I need access to stdout and stderr of execution.
>> Ping, for one, allows you to set an upper bound on how long it runs
>> (the -c option).  This is probably the cleanest approach if it's
>> available.
>>
> 
> Yes, I am aware of the ping -c option. But again even that does not help.
> try
> process = subprocess.Popen('ping -c 10 127.0.0.1', stdin=subprocess.PIPE,
> shell=True)
> process.stdout.read()  # This will hang again.
> 
> I am not sure, why subprocess is behaving so.
> 
>> You can also send the subprocess signals if you need it to exit
>> (although, this is a unix thing so I'm not sure how portable it is).
> 
> Yes, I have tried to kill and then get the standard output result.
> But the result has been the same. I could not read the Popen returned file
> object.
> 
>> You could emulate having a timeout on child.stdout.read by registering
>> a callback with Timer to kill the child.
> 
> I dont know how to do this. I shall give it a try ( by looking around ) and
> trying. 
> 

Is it possible your ping implementation outputs to stderr?

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list