Search code examples
pythontimetimer

How to stop timer when external program is closed [Python]


I am trying to build a Timer which is counting the time how long I used Google Chrome:

import subprocess
import time

start = time.time()
subprocess.Popen(['C:\Program Files (x86)\Google\Chrome\Application\\chrome.exe'])
end = time.time()

print(end - start)

The output is just the time how long the execution is taking. How can I create the argument: when Chrome is closed = end?


Solution

  • You Can put "subprocess.Popen()" in a variable and use poll() function to check if the process is alive or not:

    s = subprocess.Popen(['C:\Program Files (x86)\Google\Chrome\Application\\chrome.exe'])
    
    check = s.poll()
    
    if check == none:
       #subprocess is alive
    else:
       #subprocess is terminated