I run a program test.py
.
Since it collapses frequently, I import subprocess
to restart it when it stops.
Sometimes I found subprocess can't successfully restart it.
Hence, I force the program to restart every 60 minutes.
But I find that there sometimes two test.py processing running simutanously.
What's wrong with my code and how to fix it?
I use windows 7 OS.
Plz check the following codes and thanks in advance:
import subprocess
import time
from datetime import datetime
p = subprocess.Popen(['python.exe', r'D:\test.py'], shell=True)
minutes = 1
total_time = 0
while True:
now = datetime.now()
#periodly restart
total_time += 1
if total_time % 100 == 0:
try:
p.kill()
except Exception as e:
terminated = True
finally:
p = subprocess.Popen(['python.exe', r'D:\test.py'], shell=True)
#check and restart if it stops
try:
terminated = p.poll()
except Exception as e:
terminated = True
if terminated:
p = subprocess.Popen(['python.exe', r'D:\test.py'], shell=True)
time.sleep(minutes * 60)