简体   繁体   English

如何使用子功能重新启动Python脚本

[英]How to restart Python script with subfunctions

I have a massive Python script that needs to be restarted automatically when it breaks. 我有一个庞大的Python脚本,当它崩溃时需要自动重新启动。 When I try a standard approach by wrapping the script into a function and then calling it via while True , I get this error: 当我尝试通过将脚本包装到函数中然后通过while True调用它的标准方法while True ,出现以下错误:

SyntaxError: unqualified exec is not allowed in function 'wrappedFunc' because it contains a nested function with free variables

As I understand, this is due to the fact that my main script has sub functions. 据我了解,这是由于我的主脚本具有子功能。 Can I still do it somehow via Python or should this be done in Windows via a batch file? 我是否仍可以通过Python进行操作,还是应该在Windows中通过批处理文件完成操作?

The error is related to using the exec keyword inside a function that has subfunctions. 该错误与在具有子功能的函数中使用exec关键字有关。 Python needs you to explicitly determine which scope the exec will use to run in this case. Python的需要你明确地确定其范围exec将使用在这种情况下运行。

exec command: execfile('file.py') . exec命令: execfile('file.py') Insecure and kind of hack hacky. 不安全且有点hacky。 Avoid wherever possible. 尽可能避免。 Spawn a shell process: os.system('python file.py') . 产生一个shell进程: os.system('python file.py') Use when desperate. 绝望时使用。

PS: i haven't tried it to invoke/run same file PS:我还没有尝试过调用/运行相同的文件

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

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