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

子进程communication()的“是”报告错误

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

子进程communication()的“是”报告错误

问题在于,Python
3.2+之前的

subprocess
模块无法将
SIGPIPE
信号处理程序恢复为默认操作。这就是为什么您会收到
EPIPE
写入错误的原因。

在Python 3.2+中

>>> from subprocess import check_output>>> check_output("yes | head -3", shell=True)b'ynynyn'

yes
退出
SIGPIPE
时被杀死
head

在Python 2中:

>>> from subprocess import check_output>>> check_output("yes | head -3", shell=True)yes: standard output: Broken pipeyes: write error'ynynyn'

yes
得到
EPIPE
写错误。可以忽略该错误

要解决该问题,您可以

restore_signals
使用
preexec_fn
参数在Python 2中进行仿真:

>>> from subprocess import check_output>>> import signal>>> def restore_signals(): # from http://hg.python.org/cpython/rev/768722b2ae0a/...     signals = ('SIGPIPE', 'SIGXFZ', 'SIGXFSZ')...     for sig in signals:...         if hasattr(signal, sig):... signal.signal(getattr(signal, sig), signal.SIG_DFL)... >>> check_output("yes | head -3", shell=True, preexec_fn=restore_signals)'ynynyn'


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

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

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