首先,我想说我刚开始学习Python,我想在我的Python脚本中执行maven命令(见下面的部分代码)
os.system("mvn surefire:test")
但不幸的是,有时这个命令会超时,所以我想知道如何设置超时阈值来控制这个命令。
也就是说,如果执行时间超过X秒,程序将跳过该命令。
此外,还有其他有用的解决方案可以解决我的问题吗?提前感谢!
首先,我想说我刚开始学习Python,我想在我的Python脚本中执行maven命令(见下面的部分代码)
os.system("mvn surefire:test")
但不幸的是,有时这个命令会超时,所以我想知道如何设置超时阈值来控制这个命令。
也就是说,如果执行时间超过X秒,程序将跳过该命令。
此外,还有其他有用的解决方案可以解决我的问题吗?提前感谢!
shell=False
,当超时发生时,我们可以直接终止该进程。p = subprocess.Popen(['mvn', 'surfire:test'])
try:
p.wait(my_timeout)
except subprocess.TimeoutExpired:
p.kill()
psutil
可以帮助解决这个问题。以下是一个示例:https://dev59.com/E3M_5IYBdhLWcg3wyWWt#4229404 - tdelaneypsutil
。 - Yongfengos.system
不支持 timeout
参数。
您可以使用 Python 3
的 subprocess 替代,它支持 timeout
参数。
例如:
yourCommand = "mvn surefire:test"
timeoutSeconds = 5
subprocess.check_output(yourCommand, shell=True, timeout=timeoutSeconds)
进一步地,我已经为您封装了一个函数getCommandOutput:
def getCommandOutput(consoleCommand, consoleOutputEncoding="utf-8", timeout=2):
"""get command output from terminal
Args:
consoleCommand (str): console/terminal command string
consoleOutputEncoding (str): console output encoding, default is utf-8
timeout (int): wait max timeout for run console command
Returns:
console output (str)
Raises:
"""
# print("getCommandOutput: consoleCommand=%s" % consoleCommand)
isRunCmdOk = False
consoleOutput = ""
try:
# consoleOutputByte = subprocess.check_output(consoleCommand)
consoleOutputByte = subprocess.check_output(consoleCommand, shell=True, timeout=timeout)
# commandPartList = consoleCommand.split(" ")
# print("commandPartList=%s" % commandPartList)
# consoleOutputByte = subprocess.check_output(commandPartList)
# print("type(consoleOutputByte)=%s" % type(consoleOutputByte)) # <class 'bytes'>
# print("consoleOutputByte=%s" % consoleOutputByte) # b'640x360\n'
consoleOutput = consoleOutputByte.decode(consoleOutputEncoding) # '640x360\n'
consoleOutput = consoleOutput.strip() # '640x360'
isRunCmdOk = True
except subprocess.CalledProcessError as callProcessErr:
cmdErrStr = str(callProcessErr)
print("Error %s for run command %s" % (cmdErrStr, consoleCommand))
# print("isRunCmdOk=%s, consoleOutput=%s" % (isRunCmdOk, consoleOutput))
return isRunCmdOk, consoleOutput
演示:
isRunOk, cmdOutputStr = getCommandOutput("mvn surefire:test", timeout=5)
此外,您还可以在终端中使用“timeout”命令:
像这样进行操作:
import os
os.system('timeout 5s [Type Command Here]')
此外,您可以使用 s、m、h、d 表示秒、分、小时和天。您可以向命令发送不同的信号。如果您想了解更多信息,请参见: https://linuxize.com/post/timeout-command-in-linux/