using subprocess for non-terminating command

prikar20 at gmail.com prikar20 at gmail.com
Wed Jul 4 16:55:17 EDT 2007


On Jul 4, 12:29 pm, "O.R.Senthil Kumaran"
<orsent... at users.sourceforge.net> wrote:
> * Jerry Hill <malaclyp... 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)

I think you meant ping -c 10 (and not ping 10). If you pass first arg
as string, are you sure you didn't get an exception? I get one if I
use 'ping -c 10 <ip-addr>' -- because looks like subprocess is
searching for an executable named as "ping -c 10 ..." (not just
'ping').
So I sent in a sequence and it all worked as expected (I didn't have
shell=True, but it shouldn't matter in getting the required output).

Try the sequence as first arg.

>>> process = subprocess.Popen('ping -c 10 127.0.0.1', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/auto/xxxkarthikxxx/python251/lib/python2.5/subprocess.py",
line 593, in __init__
    errread, errwrite)
  File "/auto/xxxkarthikxxx/python251/lib/python2.5/subprocess.py",
line 1079, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
>>> process = subprocess.Popen('ping -c 10 127.0.0.1'.split(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

>>> print process
<subprocess.Popen object at 0xb7580aac>
>>> print process.pid
13435
>>> print process.stdout.read()
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.025 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.006 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.005 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.004 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.011 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.007 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.020 ms
64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.006 ms
64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.006 ms
64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.006 ms

--- 127.0.0.1 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9013ms
rtt min/avg/max/mdev = 0.004/0.009/0.025/0.007 ms, pipe 2

>>>

-- Karthik

>
> >>> process.pid
> 3475
> >>> import os
> >>> import signal
> >>> os.kill(process.pid,signal.SIGINT)
> >>> process.stdout.read()
> ''
> >>> # required output is lost!
>
> --
> O.R.Senthil Kumaranhttp://uthcode.sarovar.org





More information about the Python-list mailing list