Re: Subprocess puzzle and two questions

Discussion in 'Python' started by Kushal Kumaran, Nov 14, 2012.

  1. 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
    Kushal Kumaran, Nov 14, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oliver Wong
    Replies:
    1
    Views:
    3,441
  2. GenxLogic
    Replies:
    3
    Views:
    1,262
    andrewmcdonagh
    Dec 6, 2006
  3. hiral
    Replies:
    2
    Views:
    595
    Jean-Michel Pichavant
    May 5, 2010
  4. Roy Smith
    Replies:
    14
    Views:
    332
    Nobody
    Nov 17, 2012
  5. Dennis Lee Bieber

    Re: Subprocess puzzle and two questions

    Dennis Lee Bieber, Nov 14, 2012, in forum: Python
    Replies:
    0
    Views:
    187
    Dennis Lee Bieber
    Nov 14, 2012
Loading...

Share This Page