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

      <tfoot id='FGclh'></tfoot>
    1. <small id='FGclh'></small><noframes id='FGclh'>

      1. <legend id='FGclh'><style id='FGclh'><dir id='FGclh'><q id='FGclh'></q></dir></style></legend>

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

        Python,Popen 和 select - 等待进程终止或超时

        Python, Popen and select - waiting for a process to terminate or a timeout(Python,Popen 和 select - 等待进程终止或超时)
          <tfoot id='3bHFW'></tfoot>
            <tbody id='3bHFW'></tbody>

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

          • <small id='3bHFW'></small><noframes id='3bHFW'>

                • <bdo id='3bHFW'></bdo><ul id='3bHFW'></ul>
                  本文介绍了Python,Popen 和 select - 等待进程终止或超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我使用以下方式运行子进程:

                  I run a subprocess using:

                    p = subprocess.Popen("subprocess", 
                                         stdout=subprocess.PIPE, 
                                         stderr=subprocess.PIPE, 
                                         stdin=subprocess.PIPE)
                  

                  此子进程可以在 stderr 上出现错误时立即退出,或者继续运行.我想检测其中任何一种情况 - 后者需要等待几秒钟.

                  This subprocess could either exit immediately with an error on stderr, or keep running. I want to detect either of these conditions - the latter by waiting for several seconds.

                  我试过了:

                    SECONDS_TO_WAIT = 10
                    select.select([], 
                                  [p.stdout, p.stderr], 
                                  [p.stdout, p.stderr],
                                  SECONDS_TO_WAIT)
                  

                  但它只是返回:

                    ([],[],[])
                  

                  在任何一种情况下.我能做什么?

                  on either condition. What can I do?

                  推荐答案

                  您是否尝试过使用 Popen.Poll() 方法.你可以这样做:

                  Have you tried using the Popen.Poll() method. You could just do this:

                  p = subprocess.Popen("subprocess", 
                                     stdout=subprocess.PIPE, 
                                     stderr=subprocess.PIPE, 
                                     stdin=subprocess.PIPE)
                  
                  time.sleep(SECONDS_TO_WAIT)
                  retcode = p.poll()
                  if retcode is not None:
                     # process has terminated
                  

                  这将导致您始终等待 10 秒,但如果失败案例很少见,这将在所有成功案例中摊销.

                  This will cause you to always wait 10 seconds, but if the failure case is rare this would be amortized over all the success cases.

                  怎么样:

                  t_nought = time.time()
                  seconds_passed = 0
                  
                  while(p.poll() is not None and seconds_passed < 10):
                      seconds_passed = time.time() - t_nought
                  
                  if seconds_passed >= 10:
                     #TIMED OUT
                  

                  这有一个忙着等待的丑陋,但我认为它完成了你想要的.

                  This has the ugliness of being a busy wait, but I think it accomplishes what you want.

                  另外再看一下选择调用文档,我想你可能想按如下方式进行更改:

                  Additionally looking at the select call documentation again I think you may want to change it as follows:

                  SECONDS_TO_WAIT = 10
                    select.select([p.stderr], 
                                  [], 
                                  [p.stdout, p.stderr],
                                  SECONDS_TO_WAIT)
                  

                  由于您通常希望从 stderr 中读取数据,因此您想知道它何时有可读取的内容(即失败案例).

                  Since you would typically want to read from stderr, you want to know when it has something available to read (ie the failure case).

                  我希望这会有所帮助.

                  这篇关于Python,Popen 和 select - 等待进程终止或超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                  • <small id='xHVzC'></small><noframes id='xHVzC'>

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

                      <tfoot id='xHVzC'></tfoot>

                          <bdo id='xHVzC'></bdo><ul id='xHVzC'></ul>