subprocess can't successfully restart the targeted python file

烈酒焚心 提交于 2019-12-07 18:10:29
jfs

There are two parts in your question:

How to restart

The priority order:

  1. fix my_test.py, to avoid crashing due to known issues
  2. use a supervisor program to run your script such as upstart or supervisord -- they can restart it automatically if it crashes
  3. write your own supervisor program with its own bugs that you have to maintain

It is best to limit yourself to options 1 and/or 2 if you can find an already-written supervisor program that works on Windows (upstart and supervisord do not work on Windows).

Your current supervisor script could be improved to avoid waiting a minute before restarting the program after it crashes if it has been running more than a minute already:

#!/usr/bin/env python3
import sys
import subprocess
import time
try:
    from time import monotonic as timer
except ImportError:
    from time import time as timer # time() can be set back

while True:
    earliest_next_start = timer() + 60
    subprocess.call([sys.executable, r'D:\my_test.py'])
    while timer() < earliest_next_start:
        time.sleep(max(0, earliest_next_start - timer()))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!