简体   繁体   English

Python 脚本崩溃后不会重新启动

[英]Python script does not restart after it crashes

I have a python script that is doing some stuff called "python1.py".我有一个 python 脚本,它正在做一些名为“python1.py”的东西。 Sometimes because connection issue, it crashes.有时因为连接问题,它崩溃了。 I have another script called "loop.py" that is supposed to monitor the first one when it crashes and restart it.我有另一个名为“loop.py”的脚本,它应该在第一个崩溃并重新启动它时监视它。 So far, it fails to restart.到目前为止,它无法重新启动。 Meaning, when an exception is risen ( IOError or WatsonException ( I am using a Watson API ) ) the script stops意思是,当出现异常(IOError 或 WatsonException(我使用的是 Watson API))时,脚本会停止

python1.py is something like this : python1.py 是这样的:

def mainfunction ():
    a = randrange(0, 1)
    Print (' my routine is doing something')
    if a = 1 :
        Print ('a = 1 ')
    else :
        Print (' a is not equals to 1') 

mainfunction ()

The other script that is supposed to restart the first one is something like this :应该重新启动第一个脚本的另一个脚本是这样的:

def loopApp():
    while True :
        try:
            python1.mainfunction ()
        except IOError :
            print (' IOError y')
        except WatsonException :
            print (' Exception from watson API')

loopApp ()

python1.py should restart when every time the exceptions happens, but it is not.每次发生异常时,python1.py 都应该重新启动,但事实并非如此。

I found the way using python subprocess .我找到了使用 python subprocess的方法。 It works fine and I could get expections working.它工作正常,我可以得到预期的工作。

def loopApp():
   
    loop = 1
    while loop == 1:
        print ("wa_loop is starting")
        try:
            process = subprocess.call(['python', 'wa_dir_watch.py'])
        except 'IOError':
            print ("\nretrying after IOError")
            continue         
        except KeyboardInterrupt:
            print ("\nstopped by user Ctr+C")
            quit() 
            
loopApp()

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

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