5

I currently have a method that executes other python scripts by using subprocess calls, I was wondering if there was anyway I could time how long it takes for this to complete? The scripts are running in an interval, what I want to achieve from this is to check whether the scripts finish within that interval.

def execute_scripts(script_name):
    process = sp.Popen(['python2.7', script_name])
    print 'executing - ' + script_name
DorkMonstuh
  • 819
  • 2
  • 19
  • 38
  • related: [Python subprocess with /usr/bin/time: how to capture timing info but ignore all other output?](http://stackoverflow.com/q/28520489/4279) – jfs May 25 '16 at 18:42
  • @DorkMonstuh are any of these answers acceptable? – hhbilly May 26 '16 at 03:45
  • Hi, This seems to be a blocking call, is it possible to have non blocking and time how long the script takes to run? – DorkMonstuh May 26 '16 at 18:05
  • @DorkMonstuh: yes. The simplest way (code-wise) is to put `process.wait()` call into a background thread, to avoid blocking the main thread. Or you could store the start time and [define `SIGCHILD` signal handler to find the end time.](http://stackoverflow.com/a/30281111/4279) (the latter approach might be too low level). – jfs May 27 '16 at 10:54

2 Answers2

2

Use timeit to time the execution of small bits of code.

#sleep2.py
import time
time.sleep(2)

You need to use subprocess.call to block until the call is finished.

import timeit
import subprocess as sp

def execute_scripts(script_name):
    process = sp.call(['python2.7', script_name])
    print 'executing - ' + script_name

t = timeit.Timer("execute_scripts('sleep2.py')", setup="from __main__ import execute_scripts")


print 'time taken : %f seconds' % t.timeit(1)


executing - sleep2.py
time taken : 2.032273 seconds

Alternatively, you can generalise this by writing a decorator to time any function call

import time
import  subprocess as sp

def timed_execution(function):
    def wrapper(arg):
        t1 = time.time()
        function(arg)
        t2 = time.time()
        return 'time taken : %f seconds' % (t2 - t1) + "\n"
   return wrapper


@timed_execution
def execute_scripts(script_name):
    sp.call(['python2.7', script_name])
    print 'executing - ' + script_name


print execute_scripts('sleep2.py')

executing - sleep2.py
time taken : 2.025291 seconds
hhbilly
  • 1,265
  • 10
  • 18
0

Do you need the program to keep running while the scripts are executing? If not, you can block your program execution until the process finishes and report the time it took:

def execute_scripts(script_name):
    time_start = time.time()
    print "starting process"
    process = sp.call(['python2.7', script_name])
    print 'finished process %s in %s s" % (process, time.time() - start_time)
mprat
  • 2,451
  • 15
  • 33