<small id='046dX'></small><noframes id='046dX'>

  • <i id='046dX'><tr id='046dX'><dt id='046dX'><q id='046dX'><span id='046dX'><b id='046dX'><form id='046dX'><ins id='046dX'></ins><ul id='046dX'></ul><sub id='046dX'></sub></form><legend id='046dX'></legend><bdo id='046dX'><pre id='046dX'><center id='046dX'></center></pre></bdo></b><th id='046dX'></th></span></q></dt></tr></i><div id='046dX'><tfoot id='046dX'></tfoot><dl id='046dX'><fieldset id='046dX'></fieldset></dl></div>
      <bdo id='046dX'></bdo><ul id='046dX'></ul>

    <legend id='046dX'><style id='046dX'><dir id='046dX'><q id='046dX'></q></dir></style></legend>

      <tfoot id='046dX'></tfoot>

      1. Python 子进程模块在段错误时不返回标准输出

        时间:2023-08-06
          <tbody id='XfHWx'></tbody>
          <bdo id='XfHWx'></bdo><ul id='XfHWx'></ul>
              <i id='XfHWx'><tr id='XfHWx'><dt id='XfHWx'><q id='XfHWx'><span id='XfHWx'><b id='XfHWx'><form id='XfHWx'><ins id='XfHWx'></ins><ul id='XfHWx'></ul><sub id='XfHWx'></sub></form><legend id='XfHWx'></legend><bdo id='XfHWx'><pre id='XfHWx'><center id='XfHWx'></center></pre></bdo></b><th id='XfHWx'></th></span></q></dt></tr></i><div id='XfHWx'><tfoot id='XfHWx'></tfoot><dl id='XfHWx'><fieldset id='XfHWx'></fieldset></dl></div>

              <tfoot id='XfHWx'></tfoot>

                1. <legend id='XfHWx'><style id='XfHWx'><dir id='XfHWx'><q id='XfHWx'></q></dir></style></legend>
                2. <small id='XfHWx'></small><noframes id='XfHWx'>

                  本文介绍了Python 子进程模块在段错误时不返回标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在从 Python 运行一个 C 可执行文件,这个可执行文件有时会出现段错误.当它发生段错误时,子进程模块不会在 stdout 或 stderr 中返回任何内容.

                  I'm running a C executable from Python and this executable sometimes segfaults. When it segfaults the subprocess module does not return anything in stdout or stderr.

                  示例代码:

                  import subprocess
                  
                  proc = subprocess.Popen(['./a.out'], stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                  out, err = proc.communicate()
                  print 'out: "%s"' % out
                  print 'err: "%s"' % err
                  print proc.returncode
                  

                  a.out的来源是:

                  int main() {
                      printf("hello world
                  ");
                      int x = 1 / 0;
                  }
                  

                  ./a.out的输出是:

                  hello world
                  Floating point exception
                  

                  Python 代码的输出是(在 Linux 中,python 2.7):

                  The output of the Python code is (in Linux, python 2.7):

                  out: ""
                  err: ""
                  -8
                  

                  有没有办法在可执行文件崩溃的情况下获取它的输出?

                  Is there a way to get the output of the executable even if it crashes?

                  将返回码转换为字符串消息的通用方法也不错.

                  A generic method to translate returncode to a string message, would also be nice.

                  推荐答案

                  你的 C 程序没有刷新它的输出缓冲区.解决此问题的最简单方法是将 STDOUT 的输出模式更改为无缓冲:

                  Your C program is not flushing its output buffer. The easiest way around this problem is to change the output mode of STDOUT to unbuffered:

                  #include <stdio.h>
                  int main(int argc,char **argv) {
                      setvbuf(stdout,_IONBF,0,0);
                      printf("hello world
                  ");
                      int x = 1 / 0;
                  }
                  

                  现在您的程序(我对其进行了编辑以修复错字)产生了正确的输出:

                  Now your program (which I edited to fix a typo) produces the correct output:

                  $ python2.7 x.py
                  out: "hello world
                  "
                  err: ""
                  0
                  $
                  

                  在我的 Mac 上返回码是 0,令人困惑,因为因信号而终止的程序没有返回码.

                  The return code is 0 on my Mac, confusingly, because programs that are terminated for a signal don't have a return code.

                  这篇关于Python 子进程模块在段错误时不返回标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:segfault 使用 numpy 的 lapack_lite 在 osx 上进行多处理 下一篇:pi 计算中的分段错误(python)

                  相关文章

                  最新文章

                  • <bdo id='ztuPQ'></bdo><ul id='ztuPQ'></ul>

                  <tfoot id='ztuPQ'></tfoot>

                  <small id='ztuPQ'></small><noframes id='ztuPQ'>

                3. <i id='ztuPQ'><tr id='ztuPQ'><dt id='ztuPQ'><q id='ztuPQ'><span id='ztuPQ'><b id='ztuPQ'><form id='ztuPQ'><ins id='ztuPQ'></ins><ul id='ztuPQ'></ul><sub id='ztuPQ'></sub></form><legend id='ztuPQ'></legend><bdo id='ztuPQ'><pre id='ztuPQ'><center id='ztuPQ'></center></pre></bdo></b><th id='ztuPQ'></th></span></q></dt></tr></i><div id='ztuPQ'><tfoot id='ztuPQ'></tfoot><dl id='ztuPQ'><fieldset id='ztuPQ'></fieldset></dl></div>

                      <legend id='ztuPQ'><style id='ztuPQ'><dir id='ztuPQ'><q id='ztuPQ'></q></dir></style></legend>