subprocess添加超时功能
Posted 小白闯天下
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了subprocess添加超时功能相关的知识,希望对你有一定的参考价值。
def TIMEOUT_COMMAND(command, timeout): """call shell-command and either return its output or kill it if it doesn‘t normally exit within timeout seconds and return None""" import subprocess, datetime, os, time, signal,sys cmd = command.split(" ") start = datetime.datetime.now() process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,shell=True) while process.poll() is None: time.sleep(1) now = datetime.datetime.now() if (now - start).seconds> timeout: subprocess.Popen("cmd /c taskkill /f /im notepad.exe") return False return True print TIMEOUT_COMMAND("notepad.exe",5)
以上是关于subprocess添加超时功能的主要内容,如果未能解决你的问题,请参考以下文章