• <bdo id='tpPum'></bdo><ul id='tpPum'></ul>
  • <small id='tpPum'></small><noframes id='tpPum'>

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

        当子进程不退出时,Python 的 subprocess.Popen 对象挂起收集子输出

        Python#39;s subprocess.Popen object hangs gathering child output when child process does not exit(当子进程不退出时,Python 的 subprocess.Popen 对象挂起收集子输出)
        <i id='WUhHQ'><tr id='WUhHQ'><dt id='WUhHQ'><q id='WUhHQ'><span id='WUhHQ'><b id='WUhHQ'><form id='WUhHQ'><ins id='WUhHQ'></ins><ul id='WUhHQ'></ul><sub id='WUhHQ'></sub></form><legend id='WUhHQ'></legend><bdo id='WUhHQ'><pre id='WUhHQ'><center id='WUhHQ'></center></pre></bdo></b><th id='WUhHQ'></th></span></q></dt></tr></i><div id='WUhHQ'><tfoot id='WUhHQ'></tfoot><dl id='WUhHQ'><fieldset id='WUhHQ'></fieldset></dl></div>

              <tbody id='WUhHQ'></tbody>

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

              <tfoot id='WUhHQ'></tfoot>

                • <bdo id='WUhHQ'></bdo><ul id='WUhHQ'></ul>
                  <legend id='WUhHQ'><style id='WUhHQ'><dir id='WUhHQ'><q id='WUhHQ'></q></dir></style></legend>
                • 本文介绍了当子进程不退出时,Python 的 subprocess.Popen 对象挂起收集子输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当进程异常退出或根本不退出时,我仍然希望能够收集到该点之前它可能生成的输出.

                  When a process exits abnormally or not at all, I still want to be able to gather what output it may have generated up until that point.

                  此示例代码的明显解决方案是使用 os.kill 杀死子进程,但在我的实际代码中,子进程挂起等待 NFS 并且不响应 SIGKILL.

                  The obvious solution to this example code is to kill the child process with an os.kill, but in my real code, the child is hung waiting for NFS and does not respond to a SIGKILL.

                  #!/usr/bin/python
                  import subprocess
                  import os
                  import time
                  import signal
                  import sys
                  child_script = """
                  #!/bin/bash
                  i=0
                  while [ 1 ]; do
                      echo "output line $i"
                      i=$(expr $i + 1)
                      sleep 1
                  done
                  """
                  childFile = open("/tmp/childProc.sh", 'w')
                  childFile.write(child_script)
                  childFile.close()
                  
                  cmd = ["bash", "/tmp/childProc.sh"]
                  finish = time.time() + 3
                  p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
                  while p.poll() is None:
                      time.sleep(0.05)
                      if finish < time.time():
                          print "timed out and killed child, collecting what output exists so far"
                          out, err = p.communicate()
                          print "got it"
                          sys.exit(0)
                  

                  在这种情况下,出现有关超时的打印语句,python 脚本永远不会退出或继续.有谁知道我可以如何以不同的方式做到这一点,并且仍然从我的子进程中获得输出

                  In this case, the print statement about timing out appears and the python script never exits or progresses. Does anybody know how I can do this differently and still get output from my child processe

                  推荐答案

                  问题是 bash 在未连接终端时不响应 CTRL-C.切换到 SIGHUP 或 SIGTERM 似乎可以解决问题:

                  Problem is that bash doesn't answer to CTRL-C when not connected with a terminal. Switching to SIGHUP or SIGTERM seems to do the trick:

                  cmd = ["bash", 'childProc.sh']
                  p = subprocess.Popen(cmd, stdout=subprocess.PIPE, 
                                            stderr=subprocess.STDOUT, 
                                            close_fds=True)
                  time.sleep(3)
                  print 'killing pid', p.pid
                  os.kill(p.pid, signal.SIGTERM)
                  print "timed out and killed child, collecting what output exists so far"
                  out  = p.communicate()[0]
                  print "got it", out
                  

                  输出:

                  killing pid 5844
                  timed out and killed child, collecting what output exists so far
                  got it output line 0
                  output line 1
                  output line 2
                  

                  这篇关于当子进程不退出时,Python 的 subprocess.Popen 对象挂起收集子输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                    <tbody id='lMTIh'></tbody>
                        <bdo id='lMTIh'></bdo><ul id='lMTIh'></ul>

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

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

                          <legend id='lMTIh'><style id='lMTIh'><dir id='lMTIh'><q id='lMTIh'></q></dir></style></legend>
                          • <tfoot id='lMTIh'></tfoot>