繁体   English   中英

异常时自动重启Python脚本

[英]Automatically restarting Python Script on Exception

我正在执行一个非常复杂的Python脚本,有时它只会得到一个错误,并且调试此错误的唯一方法是重新启动它,因为其他所有操作都没有意义,并且错误很快就会回来(我已经尝试过很多事情,所以请不要专心于此)

我想要一个.bat脚本(不幸的是在Windows上是im),它可以在结束时重新启动python脚本。 另一个python脚本也很好。

我怎样才能做到这一点? 提前致谢

set env=python.exe  
tasklist /FI "IMAGENAME eq python.exe" 2>NUL | find /I /N "python.exe">NUL if "%ERRORLEVEL%"!="0(
   start python script.py
)

从Python执行Python的其他方式

import subprocess
from subprocess import call

def processExists(processname):
    tlcall = 'TASKLIST', '/FI', 'imagename eq %s' % processname
    # shell=True hides the shell window, stdout to PIPE enables
    # communicate() to get the tasklist command result
    tlproc = subprocess.Popen(tlcall, shell=True, stdout=subprocess.PIPE)
    # trimming it to the actual lines with information
    tlout = tlproc.communicate()[0].strip().split('\r\n')
    # if TASKLIST returns single line without processname: it's not running
    if len(tlout) > 1 and processname in tlout[-1]:
        print('process "%s" is running!' % processname)
        return True
    else:
        print(tlout[0])
        print('process "%s" is NOT running!' % processname)
        return False



if not processExists('python.exe')
   call(["python", "your_file.py"])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM