首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python子进程超时,任务耗时

Python子进程超时,任务耗时
EN

Stack Overflow用户
提问于 2014-04-04 02:40:42
回答 1查看 191关注 0票数 1

下面的代码允许运行带有超时的子进程

代码语言:javascript
运行
复制
p = subprocess.Popen([...])
while timeConsumedSoFar < timeoutLimit
    if proc.poll() is not None:
        doSomething
    else:
        time.sleep(2)
os.kill([...])

它可以与常规的linux命令一起使用。但是有些命令很耗时,比如'shred‘

代码语言:javascript
运行
复制
'shred [OPTIONS] FILE [...] '

粉碎逻辑卷可能需要20分钟以上的时间。有没有更好的方法来处理这个问题?

EN

回答 1

Stack Overflow用户

发布于 2014-04-04 03:50:55

看起来像是子进程处理等待的中断,所以我不能使用它。

下面是一个(可能有buggy的)示例,这是一种比较传统的unix方式。注意,这不会产生任何来自的输出!事实上,它根本不会产生任何输出。

由于所有的评论,它看起来比它是什么更多。

你可能会认为这更好,是因为我们不会不断地对这个过程进行投票。相反,我们告诉内核在指定的超时后或命令完成后返回给我们。在任何一种情况下,响应都不会从事件发生后延迟超过几微秒,而且代码只需要设置第一次初始轮询。

代码语言:javascript
运行
复制
#!/usr/bin/python

def runwait(timeout, *args):
  from os import fork, execvp, waitpid, kill, dup2
  from signal import alarm, signal, SIGTERM, SIGALRM, SIG_DFL
  from errno import EINTR

  ## We dont do much in the alarm handler, we only care that we interrupted
  ## the wait() on the process.
  def onalarm(sig, frame):
    pass

  ## This is used for redirecting stdin/out/err to /dev/null
  devnull = open('/dev/null', 'r+')
  pid = fork()
  if not pid:
    ## The child process. Begin by redirecting all input and output.
    dup2(devnull.fileno(), 0)
    dup2(devnull.fileno(), 1)
    dup2(devnull.fileno(), 2)
    ## Probably not a great idea to pass this file descriptor to whatever we 
    ## end up executing.
    devnull.close()

    ## Overwrite child process with new execution context.
    execvp(args[0], args)
    ## If something failed (like command not found) exit 63. WARNING, because we
    ## redirected all output to /dev/null, you WILL NOT be informed the command was
    ## not found directly. Use the exit code to work that out.
    sys.exit(63)

  ## This is the parent process that initiated the fork.
  ## Arm a timer using timeout given by first parameter of function. This
  ## must be an int, not a float. I dont bother checking though cause I'm lazy.    
  signal(SIGALRM, onalarm)
  alarm(timeout)

  ## We wait on the pid, this call typically blocks forever.
  try:
    pid, rc = waitpid(pid, 0)
  except OSError as e:
    ## We will land here if the alarm triggered BEFORE the process completed!
    ## In this case, if we were interrupted to deal with the 
    ## signal handler its definitely an alarm. Otherwise
    ## a peripheral exception occurred (permissions for example) so just re-raise the exception.
    if e.errno == EINTR:
      ## We send a TERM signal to terminate the process and re-wait. This causes
      ## wait to (under normal conditions) come back immediately with the signal 
      ## we just killed it with which parse out further down.
      kill(pid, SIGTERM)
      pid, rc = waitpid(pid, 0)
    else:
      raise

  ## Waits status is a 16bit integer packing a 8bit signal and 8bit return code.
  ## Do some funky bitwise operations to separate the two..
  sig = rc & 0xff
  rc >>= 8
  ## Whatever happened above, always disable the alarm and signal handling.
  alarm(0)
  signal(SIGALRM, SIG_DFL)
  return sig, rc


if __name__ == "__main__":
  # An example when you time out
  print runwait(2, "sleep", "20")
  # An example on success
  print runwait(5, "sleep", "3")
  # More success, but demonstrating no output
  print runwait(5, "grep", "root", "/etc/passwd")

该函数返回终止进程的信号号(如果有)和已执行程序的返回码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22873633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档