首页 > 其他 > 详细

subprocess添加超时功能

时间:2016-06-03 12:49:57      阅读:277      评论:0      收藏:0      [点我收藏+]
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添加超时功能

原文:http://www.cnblogs.com/xiaobaichuangtianxia/p/5555675.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!