zoukankan      html  css  js  c++  java
  • python Popen卡死问题

    程序经常卡死,定位了半天才定位到原因,原来是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还是小心点,这里面坑很多。

  • 相关阅读:
    初賽
    SA
    高斯-约旦消元法
    AC自动机
    KMP
    关于scanf
    网络流
    常用SQL语句
    Java开发中的23种设计模式详解(转)
    generatorConfig.xml
  • 原文地址:https://www.cnblogs.com/chybot/p/5176118.html
Copyright © 2011-2022 走看看