程序经常卡死,定位了半天才定位到原因,原来是Popen导致的卡死;

程序如下:

s = subprocess.Popen([*,*,*], stdout=subprocess.PIPE)

ret = s.stdout.read()

return ret

官方文档的解释是:

This will deadlock when using stdout=PIPE and/or stderr=PIPE and 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还是小心点,这里面坑很多。

最新文章

  1. C/C++ char a[ ] 和 char *a 的差别,改变 char *a爆内存错误的原因
  2. MVC 全局异常处理及禁用显示头
  3. Bootstrap学习笔记上(带源码)
  4. WINAPI 变量(2861个)
  5. Languages
  6. MapReduce流程、如何统计任务数目以及Partitioner
  7. 微信支付 v 3.3.6
  8. C++ map简单运用
  9. Codeforces 474A Keyboard (水
  10. servlet 用法
  11. [Swift]LeetCode27. 移除元素 | Remove Element
  12. MongoDB集群的搭建
  13. 剑指offer PART 2
  14. MySQL中间件之ProxySQL(3):Admin管理接口
  15. VCS双机+oracle 11gR2+ASM主机名修改
  16. 基于 Dojo toolkit 实现 web2.0 的 MVC 模式
  17. sleep() 和 wait() 有什么区别?
  18. shell脚本之分析oracle数据库数据泵日志中表的大小
  19. Linux安装Nginx报错: ./configure: error: C compiler cc is not found
  20. 乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array

热门文章

  1. 初识TPOT:一个基于Python的自动化机器学习开发工具
  2. 上google的方法
  3. 微信小程序之分享或转发功能(自定义button样式)
  4. 微信小程序之缓存——不同页面传递数据
  5. [CF587F]Duff is Mad[AC自动机+根号分治+分块]
  6. 如何唯一确定一个 Java 类?
  7. JavaScript快速入门-ECMAScript基础语法
  8. 基于.NET的3D开发框架/工具比较
  9. javascript典型bug——错误的闭包
  10. 数据中心网络(1)-VXLAN