从其他脚本重新启动 python 脚本

Restart python script from other script

我已经在此站点上搜索了多个解决方案,但遗憾的是提供的解决方案不适合我。

假设我有一个名为 "DataGen.py" 的 python 脚本,它会停止 运行(绿色箭头是可点击的),因为某些后台程序正在崩溃。不幸的是,没有抛出异常,这就是为什么我需要一个解决方法:

我要搜索的是另一个名为 "RestartScript.py" 的 python 脚本,该脚本会在停止 运行 60 秒后重新启动 "DataGen.py"。

我试过类似的方法:

from os import system
from time import sleep

while True:
    system('DataGen.py')
    sleep(300)

system 函数执行您用作输入的命令。请参阅 link。因此,这将是您 运行 实现您想要的结果的方式:

system('python DataGen.py')

此外,在 sleep 函数中用作输入的任何值都应该是秒.. 使用此逻辑,您的代码每 300 秒重新运行s。

请参阅下面的完整解决方案:

from os import system
from time import sleep

while True:
    system('python DataGen.py')
    sleep(60)