Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33299

Subprocess puzzle and two questions

Path csiph.com!usenet.pasdenom.info!news.albasani.net!news.mixmin.net!feed.xsnews.nl!border-1.ams.xsnews.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <wrw@mac.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.04; 'sys': 0.05; 'bash': 0.07; 'false,': 0.07; 'space.': 0.07; 'subject:two': 0.07; 'python': 0.09; 'command-line': 0.09; 'server:': 0.09; 'stderr': 0.09; 'stdout': 0.09; 'truncated': 0.09; 'zero.': 0.09; 'advance': 0.10; '0.06': 0.16; 'decimal.': 0.16; 'pythonic': 0.16; 'received:mac.com': 0.16; 'skip:( 60': 0.16; 'skip:> 20': 0.16; 'subject:questions': 0.16; 'subprocess': 0.16; 'wing': 0.16; 'received:10.0.1': 0.17; 'shell': 0.18; '>>>': 0.18; 'appears': 0.18; 'to:name:python-list@python.org': 0.20; 'trying': 0.21; 'import': 0.21; 'explicit': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; "python's": 0.23; 'lines': 0.28; "i'm": 0.29; "skip:' 10": 0.30; 'function': 0.30; 'print': 0.32; 'skip:s 30': 0.33; 'function.': 0.33; 'utility': 0.33; 'received:10.0': 0.33; 'to:addr:python- list': 0.33; 'third': 0.34; 'thanks': 0.34; 'server': 0.35; 'received:17': 0.35; 'there': 0.35; 'but': 0.36; 'charset:us- ascii': 0.36; 'two': 0.37; 'received:10': 0.38; 'to:addr:python.org': 0.39; 'space': 0.39; 'skip:a 30': 0.60; 'address': 0.60; 'from:no real name:2**0': 0.60; 'real': 0.61; 'first': 0.61; 'time,': 0.62; 'skip:n 10': 0.63; 'information': 0.63; 'more': 0.63; 'within': 0.64; 'applying': 0.69; 'answer:': 0.84; 'recover': 0.84; 'canonical': 0.91
MIME-version 1.0
Content-transfer-encoding 7BIT
Content-type text/plain; CHARSET=US-ASCII
X-Proofpoint-Virus-Version vendor=fsecure engine=2.50.10432:5.9.8185,1.0.431,0.0.0000 definitions=2012-11-14_01:2012-11-13, 2012-11-14, 1970-01-01 signatures=0
X-Proofpoint-Spam-Details rule=notspam policy=default score=0 spamscore=0 ipscore=0 suspectscore=0 phishscore=0 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=6.0.2-1203120001 definitions=main-1211130322
From wrw@mac.com
Subject Subprocess puzzle and two questions
Date Tue, 13 Nov 2012 22:34:55 -0500
To "python-list@python.org" <python-list@python.org>
X-Mailer Apple Mail (2.1499)
Cc "William R. Wing" <wrw@mac.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3664.1352867713.27098.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352867713 news.xs4all.nl 6883 [2001:888:2000:d::a6]:43831
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33299

Show key headers only | View raw


I need to time the operation of a command-line utility (specifically nslookup) from within a python program I'm writing.  I don't want to use python's timeit function because I'd like to avoid python's subprocess creation overhead.  That leads me to the standard UNIX time function.  So for example, in my bash shell, if I enter:

	$ time nslookup www.es.net 8.8.4.4

I get:

	Server:	 	8.8.4.4
	Address:	8.8.4.4#53

	Non-authoritative answer:
	www.es.net	canonical name = www3.es.net.
	Name:	www3.es.net
	Address: 128.55.22.201

	 real	0m0.069s
	 user	0m0.006s
	 sys	0m0.004s 

The first lines are the result of an nslookup of the IP address of "www.es.net" using the server at 8.8.4.4 (Google's public DNS server b).
The last three lines are what I'm after: the real elapsed wall-clock time, the time spent in user space and the time spent in kernel space.

However, if I try the same operation in the python interpreter using subprocess.Popen like so:

>>> import subprocess
>>> result = subprocess.Popen(['time', 'nslookup', 'www.es.net', '8.8.4.4'], shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()
>>> print result
('Server:\t\t8.8.4.4\nAddress:\t8.8.4.4#53\n\nNon-authoritative answer:\nwww.es.net\tcanonical name = www3.es.net.\nName:\twww3.es.net\nAddress: 128.55.22.201\n\n', '        0.06 real         0.00 user         0.00 sys\n')

And the timing information I'm after has been truncated to two digits after the decimal.  It appears that Popen is applying a default format. If I do explicit formatting:

>>> time = result[1].lstrip().split(' ')[0]
>>> formatted_time = '{: >7.3f}'.format(float(time))
>>> print formatted_time
  0.060

I get three digits, BUT that third digit isn't real, the format operation has simply appended a zero.  So:

1) how can I recover that third digit from the subprocess?
2) is there a more pythonic way to do what I'm trying to do?

python 2.7, OS-X 10.8.2

Thanks in advance -
Bill Wing

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Subprocess puzzle and two questions wrw@mac.com - 2012-11-13 22:34 -0500
  Re: Subprocess puzzle and two questions Roy Smith <roy@panix.com> - 2012-11-13 23:41 -0500
    Re: Subprocess puzzle and two questions William Ray Wing <wrw@mac.com> - 2012-11-14 00:03 -0500
      Re: Subprocess puzzle and two questions Roy Smith <roy@panix.com> - 2012-11-14 09:22 -0500
        Re: Subprocess puzzle and two questions Chris Angelico <rosuav@gmail.com> - 2012-11-15 01:40 +1100
          Re: Subprocess puzzle and two questions roy@panix.com (Roy Smith) - 2012-11-14 11:20 -0500
            Re: Subprocess puzzle and two questions Chris Angelico <rosuav@gmail.com> - 2012-11-15 08:54 +1100
              Re: Subprocess puzzle and two questions Roy Smith <roy@panix.com> - 2012-11-14 20:49 -0500
                Re: Subprocess puzzle and two questions Chris Angelico <rosuav@gmail.com> - 2012-11-15 13:04 +1100
                Re: Subprocess puzzle and two questions Roy Smith <roy@panix.com> - 2012-11-14 21:10 -0500
                Re: Subprocess puzzle and two questions Chris Angelico <rosuav@gmail.com> - 2012-11-15 13:21 +1100
                Re: Subprocess puzzle and two questions Dave Angel <d@davea.name> - 2012-11-14 21:55 -0500
                Re: Subprocess puzzle and two questions Kushal Kumaran <kushal.kumaran+python@gmail.com> - 2012-11-15 10:23 +0530
                Re: Subprocess puzzle and two questions Nobody <nobody@nowhere.com> - 2012-11-15 22:54 +0000
                Re: Subprocess puzzle and two questions Roy Smith <roy@panix.com> - 2012-11-15 20:07 -0500
                Re: Subprocess puzzle and two questions Nobody <nobody@nowhere.com> - 2012-11-17 00:17 +0000
              DNS from Python (was Re: Subprocess puzzle and two questions) aahz@pythoncraft.com (Aahz) - 2012-11-14 21:42 -0800
        Re: Subprocess puzzle and two questions wrw@mac.com - 2012-11-14 09:37 -0500
  Re: Subprocess puzzle and two questions Tim Roberts <timr@probo.com> - 2012-11-13 23:17 -0800

csiph-web