免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1891 | 回复: 1
打印 上一主题 下一主题

python的subprocess为什么要有close_fd参数? [复制链接]

论坛徽章:
2
2015年迎新春徽章
日期:2015-03-04 10:16:532015元宵节徽章
日期:2015-03-06 15:53:22
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-01-17 09:58 |只看该作者 |倒序浏览
例如,建立一个子进程,如果调用者打开了fd=0,1,2(标准输入,输出,错误)和fd=3(文件 ./a.txt),那么用subprocess.POpen建立一个子进程的时候,如果设置了"close_fd=True",比如:
  1.     p=subprocess.Popen(cmd,shell=True,close_fds=True,stdout=None...
复制代码
那么这是否意味着在子进程中:

(1) fd 0-3都已经被关闭了?
(2) 如果都关闭了,那么调用者如何通过管道和子进程做输入、输出的交互呢?
  1. p.communicate()
复制代码
看起来就不能工作了,对吗?

但是我看到不少代码都有上面两句,先close_fds=True,然后communicate()

所以疑惑了,还请解释一下

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
2 [报告]
发表于 2017-01-17 18:56 |只看该作者
回复 1# cdsfiui

https://docs.python.org/3/library/subprocess.html

If close_fds is true, all file descriptors except 0, 1 and 2 will be closed before the child process is executed. (POSIX only). The default varies by platform: Always true on POSIX. On Windows it is true when stdin/stdout/stderr are None, false otherwise. On Windows, if close_fds is true then no handles will be inherited by the child process. Note that on Windows, you cannot set close_fds to true and also redirect the standard handles by setting stdin, stdout or stderr.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP