码迷,mamicode.com
首页 > 其他好文 > 详细

subprocess添加超时功能

时间:2016-06-03 12:52:20      阅读:405      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!