python - 如何强制使产生子进程的进程超时
问题描述
我需要运行可能会产生其他进程的进程(程序、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() 函数以强制使进程超时,而不管产生了什么子进程?
解决方案
这是有关如何在 bash 中实现此目的的示例代码。您也许可以将其应用于您的程序:
$ date; timeout 1 sleep 5 ; date
Sat Dec 8 18:02:44 UTC 2018
Sat Dec 8 18:02:45 UTC 2018
简而言之,看看在命令开头添加“超时持续时间”是否有效。
推荐阅读
- dataframe - 我应该什么时候重新分区我的数据框?
- python - 尝试使用 pandasgui 时遇到 Bad Display Name 错误
- pycharm - 我第一次看到这个,这个特定的 IDE 致命错误是什么?
- python - 删除熊猫列名中的多余空格
- amazon-web-services - AWS 网络 ACL - 子网关联如何工作?
- java - 为什么 Tomcat 允许具有相同路径的应用程序?
- reactjs - 如何用玩笑模拟localStorage isLoggedIn true
- python - RMSE 的 Python 函数不断返回 NaN
- javascript - Javascript卡片搜索过滤卡片概览页面
- java - 哈希映射到函数
到哈希图?