Daylen R. answered 04/28/22
Data Engineer, linguist, and former college writing instructor
If doing the time calculation within the script itself is what's "kludgey", then another script can be written to do that. I hacked this up to do just that - certainly there's more eloquent ways to do something like this, but this gets an accurate time calculation
import time
import psutil
import sys
def foo(script_name):
t0 = time.time()
times = []
times.append(time.time())
for process in psutil.process_iter():
if 'python' in str(process.name) and script_name in str(process.cmdline()):
t = process.create_time()
return t, True
return time.time(), False
def main(script_name):
print('um hello')
r = False
while True:
a = foo(script_name)
r = a[1]
if r is True:
start = a[0]
else:
print('start the script already')
while r is True:
a = foo(script_name)
r = a[1]
if r is False:
end = time.time()
return round(start), round(end)
if __name__ == "__main__":
script_name = sys.argv[1]
start, end = main(script_name)
print('program ran for approx', end-start, 'seconds')