我有一个Python脚本,我想让它自己重新启动。我在谷歌上找到了以下几行:
def restart_program():
"""Restarts the current program.
Note: this function does not return. Any cleanup action (like
saving data) must be done before calling this function."""
python = sys.executable
os.execl(python, python, * sys.argv)
但在尝试了这个之后,问题就变得很明显了。我在一个非常小的嵌入式系统上运行,我很快就用完了内存(在这个函数的2到3次迭代之后)。检查进程列表,我可以看到一大堆python进程。 现在,我意识到,我可以检查进程列表并杀死所有比我自己有另一个PID的进程——这是我必须做的还是有更好的Python解决方案?在
这将使用与生成第一个进程相同的调用来生成一个新的子进程,但它不会停止现有进程(更确切地说:现有进程等待子进程退出)。在
更简单的方法是重构程序,这样就不必重新启动它。你为什么要这么做?在
我将restart函数重写如下,它将在启动新的子进程之前杀死除自身以外的所有python进程:
不太确定这是否是最好的解决办法,但它对过渡时期有效。。。在
相关问题 更多 >
编程相关推荐