I am trying to count the elapsed time of a process(tracking time in a game). The following code works but keeps the CPU busy constantly. I'd imagine it's something to do with the while loop constantly polling to check if the process is still running. Could there be a more efficent way of implementing this?
def is_running(process_name):
try:
subprocess.check_output(['pidof', process_name])
return True
except subprocess.CalledProcessError:
return False
def monitor_process(process_name):
while True:
if is_running(process_name):
start_time = time.time()
while is_running(process_name):
pass
return round(time.time() - start_time, 3)
else:
pass