English 中文(简体)
Python,Popen和select - 等待进程终止或超时
原标题:
  • 时间:2008-12-03 16:53:17
  •  标签:

我使用以下代码运行子进程:

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

这个子进程可能立即通过stderr退出错误,也可能继续运行。我想检测这些条件中的任何一个——后者通过等待几秒钟来实现。

我尝试了这个:

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

但它只是返回:

  ([],[],[])

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

问题回答

你尝试过使用Popen.Poll()方法吗?你可以只需要这样做:

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秒钟,但如果故障情况很少,这将在所有成功情况下摊销。


编辑:

怎么样? (zěn me yàng?)

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

这样做会显得有点繁忙,但我认为它可以达到你想要的效果。

此外,再次查看 select 调用文档,我认为您可能希望将其更改如下:

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

由于通常你想从stderr中读取,因此你要知道何时有可读的内容(即失败情况)。

我希望这能帮到你。

这就是我想到的。当你需要和不需要在进程上超时时都有效,但需要半忙循环。

def runCmd(cmd, timeout=None):
       
    Will execute a command, read the output and return it back.

    @param cmd: command to execute
    @param timeout: process timeout in seconds
    @return: a tuple of three: first stdout, then stderr, then exit code
    @raise OSError: on missing command or if a timeout was reached
       

    ph_out = None # process output
    ph_err = None # stderr
    ph_ret = None # return code

    p = subprocess.Popen(cmd, shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
    # if timeout is not set wait for process to complete
    if not timeout:
        ph_ret = p.wait()
    else:
        fin_time = time.time() + timeout
        while p.poll() == None and fin_time > time.time():
            time.sleep(1)

        # if timeout reached, raise an exception
        if fin_time < time.time():

            # starting 2.6 subprocess has a kill() method which is preferable
            # p.kill()
            os.kill(p.pid, signal.SIGKILL)
            raise OSError("Process timeout has been reached")

        ph_ret = p.returncode


    ph_out, ph_err = p.communicate()

    return (ph_out, ph_err, ph_ret)

这里有一个好的例子:

from threading import Timer
from subprocess import Popen, PIPE

proc = Popen("ping 127.0.0.1", shell=True)
t = Timer(60, proc.kill)
t.start()
proc.wait()

使用select和睡眠并没有太多意义。select(或任何内核轮询机制)本质上对于异步编程很有用,但是您的示例是同步的。因此,要么重新编写代码以使用正常的阻塞方式,要么考虑使用Twisted:

from twisted.internet.utils import getProcessOutputAndValue
from twisted.internet import reactor    

def stop(r):
    reactor.stop()
def eb(reason):
    reason.printTraceback()
def cb(result):
    stdout, stderr, exitcode = result
    # do something
getProcessOutputAndValue( /bin/someproc , []
    ).addCallback(cb).addErrback(eb).addBoth(stop)
reactor.run()

顺便说一下,使用自己编写的 ProcessProtocol 是使用 Twisted 更安全的方法:

将此翻译为中文:http://twistedmatrix.com/projects/core/documentation/howto/process.html http://twistedmatrix.com/projects/core/documentation/howto/process.html

Python 3.3

import subprocess as sp

try:
    sp.check_call(["/subprocess"], timeout=10,
                  stdin=sp.DEVNULL, stdout=sp.DEVNULL, stderr=sp.DEVNULL)
except sp.TimeoutError:
    # timeout (the subprocess is killed at this point)
except sp.CalledProcessError:
    # subprocess failed before timeout
else:
    # subprocess ended successfully before timeout

请参阅TimeoutExpired文件

如果像您在上面的评论中所说的那样,您只是每次微调输出并重新运行命令,那么以下内容是否可行?

from threading import Timer
import subprocess

WAIT_TIME = 10.0

def check_cmd(cmd):
    p = subprocess.Popen(cmd,
        stdout=subprocess.PIPE, 
            stderr=subprocess.PIPE)
    def _check():
        if p.poll()!=0:
            print cmd+" did not quit within the given time period."

    # check whether the given process has exited WAIT_TIME
    # seconds from now
    Timer(WAIT_TIME, _check).start()

check_cmd( echo )
check_cmd( python )

上面的代码,当运行时,输出:

python did not quit within the given time period.

我能想到以上代码的唯一缺点是在继续运行check_cmd时可能存在重叠的进程。

这是对埃文答案的改述,但考虑了以下因素:

  1. Explicitly canceling the Timer object : if the Timer interval would be long and the process will exit by its "own will" , this could hang your script :(
  2. 计时器方法中存在固有的竞态条件(当进程已经死亡时定时器试图杀死该进程之后,在Windows上将引发异常)。

      DEVNULL = open(os.devnull, "wb")
      process = Popen("c:/myExe.exe", stdout=DEVNULL) # no need for stdout
    
      def kill_process():
      """ Kill process helper"""
      try:
         process.kill()
       except OSError:
         pass  # Swallow the error
    
      timer = Timer(timeout_in_sec, kill_process)
      timer.start()
    
      process.wait()
      timer.cancel()
    




相关问题
热门标签