重新启动 Python 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2107317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Restart a Python Program
提问by Smashery
I'm writing a Python program that, if the user changes settings while running it, needs to be restarted to apply the changed settings. Is there a way of doing this? I'm thinking something along the lines of:
我正在编写一个 Python 程序,如果用户在运行它时更改了设置,则需要重新启动以应用更改后的设置。有没有办法做到这一点?我在想一些事情:
import sys
command_line = ' '.join(sys.argv)
# Now do something with command_line
# Now call my custom exit procedure
Note: I'm on Windows, if that makes a difference
注意:我在 Windows 上,如果这有区别的话
采纳答案by paxdiablo
I would bypass all the angst you're likely to get from trying to re-run yourself and leave it in the hands of the environment.
我会绕过您在尝试重新运行自己时可能会产生的所有焦虑,并将其交给环境处理。
By that, I mean:
我的意思是:
Have a controlling program which does nothing more than run your program (with the same parameters it was given) in a loop while your program exits with a specific "restart" code. This could be as simple as a
cmd
file or as complex as another (very simple) Python program that usesos.system
). Basically, as long as the controlling program gets the code "restart", it will re-run your program with exactly the same parameters. Any other code will cause it to exit with that code.When you want to exit fully, have your real Python program exit with return code 0 (or anything that's not the "restart" code in an error situation).
If you just want to cycle to another iteration of your program (to re-read the config for example), exit with the "restart" code recognised by the controlling program.
有一个控制程序,它只在循环中运行您的程序(使用给定的相同参数),而您的程序以特定的“重新启动”代码退出。这可以像
cmd
文件一样简单,也可以像另一个(非常简单的)使用 的 Python 程序一样复杂os.system
。基本上,只要控制程序得到代码“重启”,它就会以完全相同的参数重新运行您的程序。任何其他代码都会导致它以该代码退出。当你想完全退出时,让你真正的 Python 程序退出并返回 0(或任何不是错误情况下的“重启”代码)。
如果您只想循环到程序的另一个迭代(例如重新读取配置),请使用控制程序识别的“重新启动”代码退出。
But you may also want to think about re-engineering your application so that it can re-read its configuration at any time. This will make the whole problem go away. You don't mention why that's not an option so I'm assuming you have some reason why it won't work.
但是您可能还想考虑重新设计您的应用程序,以便它可以随时重新读取其配置。这将使整个问题消失。你没有提到为什么这不是一个选项,所以我假设你有一些理由让它不起作用。
But, if you don't, that's the path I'd be choosing.
但是,如果你不这样做,这就是我会选择的道路。
To provide some sample code for the first option (this is on Windows but under Cygwin - the same basic rules should apply for Windows native Python but you should check the return values from os.system
).:
为第一个选项提供一些示例代码(这是在 Windows 上,但在 Cygwin 下 - 相同的基本规则应该适用于 Windows 原生 Python,但您应该检查来自 的返回值os.system
):
> cat phase1.py
#!/usr/bin/python
import os
status = 9
while status == 9:
status = int(os.system ("./phase2.py") / 256) # exit code is upper 8 bits
print "Controller: %d"%(status)
> cat phase2.py
#!/usr/bin/python
import sys
import time
time.sleep(1)
val = int(time.time())%10
if val == 0:
rc = 0
else:
rc = 9
print "Program: %d -> %d"%(val,rc)
sys.exit(rc)
> ./phase1.py
Program: 2 -> 9
Controller: 9
Program: 3 -> 9
Controller: 9
Program: 4 -> 9
Controller: 9
Program: 5 -> 9
Controller: 9
Program: 7 -> 9
Controller: 9
Program: 8 -> 9
Controller: 9
Program: 9 -> 9
Controller: 9
Program: 0 -> 0
Controller: 0
You can see the controller using an exit code of 9 to decide whether to re-run the program. The program itself is a dumb one which returns 9 unless it's on a 10-second multiple.
您可以看到控制器使用退出代码 9 来决定是否重新运行程序。该程序本身是一个愚蠢的程序,它返回 9,除非它是 10 秒的倍数。
回答by ars
On unix, you can use os.execlfamily of functions:
在 unix 上,您可以使用os.execl系列函数:
These functions all execute a new program, replacing the current process; they do not return. On Unix, the new executable is loaded into the current process, and will have the same process id as the caller. Errors will be reported as OSError exceptions.
这些函数都执行一个新的程序,替换当前的进程;他们不回来。在 Unix 上,新的可执行文件被加载到当前进程中,并且与调用者具有相同的进程 ID。错误将报告为 OSError 异常。
On windows, take a look at the os.spawnlfamily, which are less efficient than the unix calls.
在 Windows 上,查看os.spawnl系列,它的效率低于 unix 调用。