Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'output': 0.04; 'modify': 0.05; 'sys': 0.05; 'bash': 0.07; 'builtin': 0.07; 'false,': 0.07; 'skip:/ 10': 0.07; 'space.': 0.07; 'subject:two': 0.07; 'python': 0.09; 'command-line': 0.09; 'invocation': 0.09; 'server:': 0.09; 'stderr': 0.09; 'stdout': 0.09; 'truncated': 0.09; 'zero.': 0.09; '0.06': 0.16; 'decimal.': 0.16; 'have)': 0.16; 'invoking': 0.16; 'pythonic': 0.16; 'runs.': 0.16; 'skip:( 60': 0.16; 'skip:> 20': 0.16; 'subject:questions': 0.16; 'subprocess': 0.16; 'shell': 0.18; 'appears': 0.18; 'sender:addr:gmail.com': 0.18; 'to:name:python-list@python.org': 0.20; 'trying': 0.21; 'import': 0.21; 'explicit': 0.22; "i'd": 0.22; "python's": 0.23; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'received:209.85.210.46': 0.27; 'lines': 0.28; 'run': 0.28; '>>>>': 0.29; 'behaviour': 0.29; 'overhead': 0.29; 'writes:': 0.29; "i'm": 0.29; "skip:' 10": 0.30; 'function': 0.30; 'code': 0.31; 'running': 0.32; 'print': 0.32; 'skip:s 30': 0.33; 'function.': 0.33; 'utility': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'third': 0.34; 'server': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'should': 0.36; 'possible': 0.37; 'two': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'space': 0.39; 'your': 0.60; 'skip:a 30': 0.60; 'address': 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; 'received:117': 0.75; 'answer:': 0.84; 'recover': 0.84; 'unclear': 0.84; 'canonical': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:subject:in-reply-to:references:user-agent:date :message-id:mime-version:content-type; bh=zqUB6YxfbUccy5/O12ziKKWz+DuXmeMsRjV9RI+xpMU=; b=BnBC0yyJ1G6BFLytCOHSRdNY0S9YZNqUvwowKnhu+jqmTjJkU6b7MapMEM/HfhBYms t4jnOTC0jzebasekeugJTBjYUNGxsH0WHp2whKhuQwDT6SQv+iZdhtmQcU9lwIv41u1X VGt7YtdO8shsFzGkT51X+YamvBMZBSqDTF9JL4nBtENTLId39phWTrBJTE0tnJggX3hA +e6bEfYFvvcf9ypgdmXbVGGmQrVu076tNFdaSIyZzvz1YgwuQtJ0ofx7iBVaUW6Nyezv aA1hhaPykry3lxvhM4foOjgR7mqPMzJTeQNEjsNoizz7G3wlhVKyMgIT6pzMn2FHZ8XQ eK8w== Sender: Kushal Kumaran From: Kushal Kumaran To: "python-list\@python.org" Subject: Re: Subprocess puzzle and two questions In-Reply-To: <09CE8A6B-19D5-46D6-BF36-F8BC7926A0DD@mac.com> References: <09CE8A6B-19D5-46D6-BF36-F8BC7926A0DD@mac.com> User-Agent: Notmuch/0.14 (http://notmuchmail.org) Emacs/24.1.1 (x86_64-pc-linux-gnu) Date: Wed, 14 Nov 2012 12:25:19 +0530 MIME-Version: 1.0 Content-Type: text/plain X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352876131 news.xs4all.nl 6842 [2001:888:2000:d::a6]:43688 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33306 wrw@mac.com writes: > 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: > It is unclear to me what overhead you are avoiding. > $ 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 > It is possible that the "time" invocation from the shell is invoking your shell's builtin time implementation, and your python code is running /usr/bin/time or /bin/time. You should see the same behaviour from the shell if you run /bin/time or /usr/bin/time (whatever you have) instead of just "time". subprocess.Popen should never modify the output of programs it runs. -- regards, kushal