python Popen卡死问题
2016-02-01 19:26
609 查看
程序经常卡死,定位了半天才定位到原因,原来是Popen导致的卡死;
程序如下:
s = subprocess.Popen([*,*,*], stdout=subprocess.PIPE)
ret = s.stdout.read()
return ret
官方文档的解释是:
This will deadlock when using
原因是使用
解决办法:使用communicate()
例如:
s = subprocess.Popen([*,*,*], stdout=subprocess.PIPE)
stdoutdata, stderrdata = s.communicate()
return stdoutdata
此外,最后在调用Popen的时候加上参数close_fds=True,参见官方文档说明:
popen2 closes all file descriptors by default, but you have to specify close_fds=True with Popen
以后使用Popen还是小心点,这里面坑很多。
程序如下:
s = subprocess.Popen([*,*,*], stdout=subprocess.PIPE)
ret = s.stdout.read()
return ret
官方文档的解释是:
This will deadlock when using
stdout=PIPEand/or
stderr=PIPEand the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use
communicate()to avoid that.
原因是使用
Popen.
wait()后直接读PIPE.stdout.read()之前,可能缓存已经满了,此时导致了卡死。
解决办法:使用communicate()
例如:
s = subprocess.Popen([*,*,*], stdout=subprocess.PIPE)
stdoutdata, stderrdata = s.communicate()
return stdoutdata
此外,最后在调用Popen的时候加上参数close_fds=True,参见官方文档说明:
popen2 closes all file descriptors by default, but you have to specify close_fds=True with Popen
以后使用Popen还是小心点,这里面坑很多。
相关文章推荐
- PYTHON之路(三)
- pyinstaller使用-python项目转换成exe可执行文件
- pyinstaller使用-python项目转换成exe可执行文件
- Python中的self
- Python 10.3 base64
- 关于python函数的默认参数,可变参数,关键字参数
- Python 模块学习:os模块
- python自定义模块和开源模块使用方法
- python 几种常见的测试框架
- 在win7下python的xlrd和xlwt的安装于应用
- Python使用socket实现简单的文件传输并校验
- Python第二讲基本数据类型
- python packaging
- django1.7 与python3.4结合连接mysql数据库
- python数据导入
- python MySQLdb在windows环境下的快速安装、问题解决方式
- 《利用python做数据分析》第十章:时间序列分析
- python中的数字
- 用Python操作Named pipe命名管道,实用做法
- python中文编码的问题