Python 创建标志文件
我对python比较陌生,所以请原谅我的早期理解 我正在创建一种标志文件。它的工作是监视Python可执行文件,标志文件持续运行,并在可执行文件启动时打印“开始”,在运行时打印“运行”,在停止或崩溃时打印“停止”,如果崩溃发生,我希望它能够重新启动脚本。到目前为止,我已记下以下内容,以便重新启动:Python 创建标志文件,python,python-3.x,Python,Python 3.x,我对python比较陌生,所以请原谅我的早期理解 我正在创建一种标志文件。它的工作是监视Python可执行文件,标志文件持续运行,并在可执行文件启动时打印“开始”,在运行时打印“运行”,在停止或崩溃时打印“停止”,如果崩溃发生,我希望它能够重新启动脚本。到目前为止,我已记下以下内容,以便重新启动: from subprocess import run from time import sleep # Path and name to the script you are trying to s
from subprocess import run
from time import sleep
# Path and name to the script you are trying to start
file_path = "py"
restart_timer = 2
def start_script():
try:
# Make sure 'python' command is available
run("python "+file_path, check=True)
except:
# Script crashed, lets restart it!
handle_crash()
def handle_crash():
sleep(restart_timer) # Restarts the script after 2 seconds
start_script()
start_script()
如何将其与标志文件一起实现?不确定“标志”是什么意思,但这至少实现了您想要的 主文件
Main.py
:
import subprocess
import sys
from time import sleep
restart_timer = 2
file_path = 'sub.py' # file name of the other process
def start():
try:
# sys.executable -> same python executable
subprocess.run([sys.executable, file_path], check=True)
except subprocess.CalledProcessError:
sleep(restart_timer)
return True
else:
return False
def main():
print("starting...")
monitor = True
while monitor:
monitor = start()
if __name__ == '__main__':
main()
from time import sleep
sleep(1)
print("doing stuff...")
# comment out to see change
raise ValueError("sub.py is throwing error...")
然后生成的进程称为sub.py
:
import subprocess
import sys
from time import sleep
restart_timer = 2
file_path = 'sub.py' # file name of the other process
def start():
try:
# sys.executable -> same python executable
subprocess.run([sys.executable, file_path], check=True)
except subprocess.CalledProcessError:
sleep(restart_timer)
return True
else:
return False
def main():
print("starting...")
monitor = True
while monitor:
monitor = start()
if __name__ == '__main__':
main()
from time import sleep
sleep(1)
print("doing stuff...")
# comment out to see change
raise ValueError("sub.py is throwing error...")
将这些文件放在同一目录中,并使用python main.py
您可以注释掉随机错误的抛出,以查看主脚本正常终止
更重要的是,这个例子并不是说这是一个很好的方法来达到你需要的质量…不确定你用“flag”是什么意思,但这是最低限度地达到你想要的 主文件
Main.py
:
import subprocess
import sys
from time import sleep
restart_timer = 2
file_path = 'sub.py' # file name of the other process
def start():
try:
# sys.executable -> same python executable
subprocess.run([sys.executable, file_path], check=True)
except subprocess.CalledProcessError:
sleep(restart_timer)
return True
else:
return False
def main():
print("starting...")
monitor = True
while monitor:
monitor = start()
if __name__ == '__main__':
main()
from time import sleep
sleep(1)
print("doing stuff...")
# comment out to see change
raise ValueError("sub.py is throwing error...")
然后生成的进程称为sub.py
:
import subprocess
import sys
from time import sleep
restart_timer = 2
file_path = 'sub.py' # file name of the other process
def start():
try:
# sys.executable -> same python executable
subprocess.run([sys.executable, file_path], check=True)
except subprocess.CalledProcessError:
sleep(restart_timer)
return True
else:
return False
def main():
print("starting...")
monitor = True
while monitor:
monitor = start()
if __name__ == '__main__':
main()
from time import sleep
sleep(1)
print("doing stuff...")
# comment out to see change
raise ValueError("sub.py is throwing error...")
将这些文件放在同一目录中,并使用python main.py
您可以注释掉随机错误的抛出,以查看主脚本正常终止
更重要的是,这个例子并不是说这是一个获得所需质量的好方法…@Shone118这有帮助吗?flag是什么意思?谢谢你的帮助,我有一个问题,所以你写的代码会监视子文件直到出现异常,但是它们应该同时运行吗?不,
python main.py
在其子进程(第11行)中调用sub.py
,所以这只是一个调用,所以我更改了名称,没有使用“main”正如您已经注意到的,程序并没有按照其预期运行,您是否了解原因?您能否更具体一些?你试过什么命令?文件名是什么?@Shone118这有帮助吗?flag是什么意思?谢谢你的帮助,我有一个问题,所以你写的代码会监视子文件直到出现异常,但是它们应该同时运行吗?不,python main.py
在其子进程(第11行)中调用sub.py
,所以这只是一个调用,所以我更改了名称,没有使用“main”正如您已经注意到的,程序并没有按照其预期运行,您是否了解原因?您能否更具体一些?你试过什么命令?文件名是什么?