栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

子流程Popen和call有什么区别(如何使用它们)?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

子流程Popen和call有什么区别(如何使用它们)?

有两种方法可以进行重定向。两者都适用于

subprocess.Popen
subprocess.call

  1. 设置关键字参数

    shell = True
    executable = /path/to/the/shell
    并在那里指定命令。

  2. 由于您只是将输出重定向到文件,因此请设置关键字参数

    stdout = an_open_writeable_file_object

对象指向

output
文件的位置。

subprocess.Popen
subprocess.call

Popen
不会阻塞,允许您在进程运行时与它进行交互,或者在Python程序中继续进行其他操作。调用
Popen
返回一个
Popen
对象。

call
确实会
阻止。它支持与
Popen
构造函数相同的所有参数,因此您仍可以设置进程的输出,环境变量等,脚本将等待程序完成,并
call
返回表示进程退出状态的代码。

returnpre = call(*args, **kwargs)

与通话基本相同

returnpre = Popen(*args, **kwargs).wait()

call
只是一种便利功能。它在CPython的实现是在subprocess.py:

def call(*popenargs, timeout=None, **kwargs):    """Run command with arguments.  Wait for command to complete or    timeout, then return the returnpre attribute.    The arguments are the same as for the Popen constructor.  Example:    retpre = call(["ls", "-l"])    """    with Popen(*popenargs, **kwargs) as p:        try: return p.wait(timeout=timeout)        except: p.kill() p.wait() raise

如您所见,它周围是薄薄的包装纸

Popen



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/637661.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号