Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9021579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:17:19+00:00 2026-06-16T05:17:19+00:00

Pretty simple, I’d like to run an external command/program from within a Python script,

  • 0

Pretty simple, I’d like to run an external command/program from within a Python script, once it is finished I would also want to know how much CPU time it consumed.

Hard mode: running multiple commands in parallel won’t cause inaccuracies in the CPU consumed result.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:17:21+00:00Added an answer on June 16, 2026 at 5:17 am

    On UNIX: either (a) use resource module (also see answer by icktoofay), or (b) use the time command and parse the results, or (c) use /proc filesystem, parse /proc/[pid]/stat and parse out utime and stime fields. The last of these is Linux-specific.

    Example of using resource:

    import subprocess, resource
    usage_start = resource.getrusage(resource.RUSAGE_CHILDREN)
    subprocess.call(["yourcommand"])
    usage_end = resource.getrusage(resource.RUSAGE_CHILDREN)
    cpu_time = usage_end.ru_utime - usage_start.ru_utime
    

    Note: it is not necessary to do fork/execvp, subprocess.call() or the other subprocess methods are fine here and much easier to use.

    Note: you could run multiple commands from the same python script simultaneously either using subprocess.Popen or subprocess.call and threads, but resource won’t return their correct individual cpu times, it will return the sum of their times in between calls to getrusage; to get the individual times, run one little python wrapper per command to time it as above (could launch those from your main script), or use the time method below which will work correctly with multiple simultaneous commands (time is basically just such a wrapper).

    Example of using time:

    import subprocess, StringIO
    time_output = StringIO.StringIO()
    subprocess.call(["time", "yourcommand", "youroptions"], stdout=time_output)
    # parse time_output
    

    On Windows: You need to use performance counters (aka “performance data helpers”) somehow. Here is a C example of the underlying API. To get it from python, you can use one of two modules: win32pdh (part of pywin32; sample code) or pyrfcon (cross-platform, also works on Unix; sample code).

    Any of these methods actually meet the “hard mode” requirements above: they should be accurate even with multiple running instances of different processes on a busy system. They may not produce the exact same results in that case compared to running just one process on an idle system, because process switching does have some overhead, but they will be very close, because they ultimately get their data from the OS scheduler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pretty simple question - I have an attribute that I would like to have
Pretty simple: I need a script that reads the element ij of TWO images
Pretty simple. I have a Python list that I am passing to a Django
Pretty simple: When I run startup.sh or shutdown.sh information is logged into catalina.out When
I have a pretty simple problem. When program has been started and user tries
This ought to be pretty simple, maybe use a regex but I would think
Pretty simple. trying to get XML back from Sharepoint site. Get blank white page...
Pretty simple what I want to do, just want to be able to run
Pretty simple question (probably) but I guess since im new to python im not
Pretty simple question I need to get an integer from the user and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.