首页 > 解决方案 > 如何强制使产生子进程的进程超时

问题描述

我需要运行可能会产生其他进程的进程(程序、bash 脚本等),但我还需要应用超时以防某些事情挂起,并且我希望程序在超时到期后继续执行。我正在使用以下 run() 函数:

import subprocess, shlex, threading
from threading import Timer

def run(cmd, timeout_sec=5):
    def kill_proc(proc, timedout):
        timedout['value'] = True
        proc.kill()

    try:
        proc = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        timedout = {'value': False}
        timer = threading.Timer(timeout_sec, kill_proc, [proc, timedout])
        timer.start()
        stdout, stderr = proc.communicate()
        timer.cancel()

        return stdout.decode('utf-8'), stderr.decode('utf-8'), proc.returncode, timedout['value']
    except Exception as e:
        print('Could not execute: %s - with error: %s' % (cmd, e))
        return '', '', 1, False

在大多数情况下,它能够优雅地超时调用,例如:

run(cmd='sleep 1m', timeout_sec=2)

似乎挂断的地方是假设我有以下名为 sleeper.sh 的 bash 脚本:

#!/bin/bash
sleep 1m

如果我发出以下命令:

run(cmd='sleeper.sh', timeout_sec=2)

它会坐在那里等待 1 分钟,直到命令完成。有没有一种方法可以让我重写我的 run() 函数以强制使进程超时,而不管产生了什么子进程?

标签: pythonbashtimeoutsubprocesspopen

解决方案


这是有关如何在 bash 中实现此目的的示例代码。您也许可以将其应用于您的程序:

$ date; timeout 1 sleep 5 ; date
Sat Dec  8 18:02:44 UTC 2018
Sat Dec  8 18:02:45 UTC 2018

简而言之,看看在命令开头添加“超时持续时间”是否有效。


推荐阅读