python中os.execl()和os.execv()之间的区别 [英] Difference between os.execl() and os.execv() in python
问题描述
python中的os.execl()和os.execv()之间有区别吗?我正在使用
Is there a difference between os.execl() and os.execv() in python? I was using
os.execl(python, python, *sys.argv)
重新启动我的脚本(来自这里).但这似乎是从上一个脚本离开的地方开始的.
to restart my script (from here). But it seems to start from where the previous script left.
我希望脚本在重新启动时从头开始.这会
I want the script to start from the beginning when it restarts. Will this
os.execv(__file__,sys.argv)
做这份工作吗? 来自此处的命令和想法.我从python帮助/文档中找不到它们之间的区别.有没有办法彻底重启?
do the job? command and idea from here. I couldn't find difference between them from the python help/documentation. Is there a way do clean restart?
For a little more background on what I am trying to do please see my other question
推荐答案
在底层,他们做同样的事情:用新的流程替换正在运行的流程映像.
At the low level they do the same thing: they replace the running process image with a new process.
execv
和execl
之间的仅区别在于它们采用参数的方式. execv
需要一个参数列表(第一个参数应该是可执行文件的名称),而execl
需要一个变量参数列表.
The only difference between execv
and execl
is the way they take arguments. execv
expects a single list of arguments (the first of which should be the name of the executable), while execl
expects a variable list of arguments.
因此,实质上execv(file, args)
完全等同于execl(file, *args)
.
Thus, in essence, execv(file, args)
is exactly equivalent to execl(file, *args)
.
请注意,sys.argv[0]
已经是脚本名称.但是,这是传递给Python的脚本名称,可能不是程序在其下运行的实际脚本名称.为了正确和安全,传递给exec*
的参数列表应该为
Note that sys.argv[0]
is already the script name. However, this is the script name as passed into Python, and may not be the actual script name that the program is running under. To be correct and safe, your argument list passed to exec*
should be
['python', __file__] + sys.argv[1:]
我刚刚使用以下命令测试了重启脚本:
I have just tested a restart script with the following:
os.execl(sys.executable, 'python', __file__, *sys.argv[1:])
,这很好.确保您没有忽略或默默地捕获execl
中的任何错误-如果执行失败,您将最终继续从上次中断的地方".
and this works fine. Be sure you're not ignoring or silently catching any errors from execl
- if it fails to execute, you'll end up "continuing where you left off".
这篇关于python中os.execl()和os.execv()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!