var*_*unl 164 python subprocess popen
我想从Python调用外部程序.我已经使用了两者Popen()并且call()做到了这一点.
这两者有什么区别?
我的具体目标是从Python运行以下命令.我不确定重定向是如何工作的.
./my_script.sh > output
Run Code Online (Sandbox Code Playgroud)
我阅读了文档,它说这call()是一个便利功能或快捷功能.我们是否通过使用call()代替Popen()?而失去了任何力量?
agf*_*agf 245
有两种方法可以进行重定向.两者都适用于subprocess.Popen或subprocess.call.
设置关键字参数shell = True或者executable = /path/to/the/shell像在那里一样指定命令.
由于您只是将输出重定向到文件,因此请设置关键字参数
stdout = an_open_writeable_file_object
Run Code Online (Sandbox Code Playgroud)
对象指向output文件的位置.
subprocess.Popen比一般更普遍subprocess.call.
Popen不阻止,允许您在进程运行时与进程交互,或继续处理Python程序中的其他内容.调用Popen返回一个Popen对象.
call 确实块.虽然它支持所有与Popen构造函数相同的参数,因此您仍然可以设置进程的输出,环境变量等,您的脚本等待程序完成,并call返回表示进程退出状态的代码.
returncode = call(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
与呼叫基本相同
returncode = Popen(*args, **kwargs).wait()
Run Code Online (Sandbox Code Playgroud)
call只是一个便利功能.它在CPython中的实现是在subprocess.py中:
def call(*popenargs, timeout=None, **kwargs):
"""Run command with arguments. Wait for command to complete or
timeout, then return the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
retcode = call(["ls", "-l"])
"""
with Popen(*popenargs, **kwargs) as p:
try:
return p.wait(timeout=timeout)
except:
p.kill()
p.wait()
raise
Run Code Online (Sandbox Code Playgroud)
如你所见,它是一个薄的包装Popen.
Bas*_*asj 35
另一个答案非常完整,但这里有一个经验法则:
call 正在阻止:
call('notepad.exe')
print('hello') # only executed when notepad is closed
Run Code Online (Sandbox Code Playgroud)Popen 是非阻塞的:
Popen('notepad.exe')
print('hello') # immediately executed
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
92352 次 |
| 最近记录: |