linux上python中的os.system vs子进程 [英] os.system vs subprocess in python on linux
问题描述
我有两个python脚本.第一个脚本调用第二个脚本表,在其中我需要执行第三方python脚本.看起来像这样:
I have two python scripts. The first script calls a table of second scripts in which I need to execute a third party python script. It looks something like this:
# the call from the first script.
cmd = "qsub -sync y -b -cwd -V -q long -t 1-10 -tc 5 -N 'script_two' ./script2.py"
script2thread = pexpect.spawn(cmd)
# end of script 1
所以在这里,我要发送10个作业到队列中.在脚本2中,我有一个基于task_id的case语句.在每个脚本中,我使用不同的参数对第三方脚本进行类似的调用.
So here i am sending 10 jobs out to the queue. In script 2 I have a case statement based on the task_id. In each one I make a similar call to the third party script using different parameters.
...
elif(task_id == 4)
subprocess.call(./script3)
# or
os.system(./script3 , shell=True)
这是我的问题所在.使用一种或另一种是否有区别/好处?我知道由于支持问题,在Windows上使用一个在另一个上会有很大的不同,但是我在Linux上,并且无意在Windows上运行它.有时,使用子进程会得到非常奇怪的结果,它无法在网络上找到其他东西,而这一次只能运行第三个脚本.
This is where my question lies. Is there a difference/benefit to using one or the other? I know that on windows using one over the other makes a big difference because of support issues but I am on linux and have no intention of running this on windows. Sometimes I get very weird results from using the subprocess, it cannot find other things on the network that it can when the third script is run independently one at a time.
推荐答案
您应使用 subprocess
.并不是说有什么区别,它只是一个旨在替换os.system
的更新模块(请查看本部分以进行直接替换).如果一天有需要,它还具有更多功能.
You should use subprocess
. Not that it makes any difference, it's just a newer module intended to replace os.system
(have a look at this section for a drop-in replacement). It also has more features in case you need them one day.
简而言之:没有理由使用os.system
(与旧版本的Python兼容除外).
In short: there is no reason to use os.system
(except for compatibility with older versions of Python).
这篇关于linux上python中的os.system vs子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!