using subprocess for non-terminating command

Phoe6 orsenthil at gmail.com
Wed Jul 4 07:38:38 EDT 2007


Hi all,
Consider this scenario, where in I need to use subprocess to execute a
command like 'ping 127.0.0.1' which will have a continuous non-
terminating output in Linux.

# code
>>>import subprocess
>>>process = subprocess.Popen('ping 127.0.0.1', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

>>>print process.pid # returns pid
>>>print process.poll() # returns None!!! this is strange.
>>>print process.stdout.read()

# This hangs at this point.
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.

Thanks,
Senthil




More information about the Python-list mailing list