美文网首页
2:golang程序超时检测

2:golang程序超时检测

作者: 陈玉涛 | 来源:发表于2019-07-31 17:24 被阅读0次

问题描述:在用代码执行adb命令时,由于adb命令长时间没有响应,导致程序一直没法执行完毕
解决办法:给出超时检测,若程序超时,则重新执行一次

//设置超时时间
var Timeout = 30 * time.Second
//超时检测
func Command(name string, arg ...string) (string, error) {
    ctxt, cancel := context.WithTimeout(context.Background(), Timeout)
    defer cancel()
    cmd := exec.CommandContext(ctxt, name, arg...)
        //当经过Timeout时间后,程序依然没有运行完,则会杀掉进程,ctxt也会有err信息
    if out, err := cmd.Output(); err != nil {
        //检测报错是否是因为超时引起的
        if ctxt.Err() != nil && ctxt.Err() == context.DeadlineExceeded {
            return "Timeout", nil
        }
        log.Println("cmd.Output() is fail !------>", err.Error())
        return string(out), err
    } else {
        return string(out), nil
    }
}

相关文章

  • 2:golang程序超时检测

    问题描述:在用代码执行adb命令时,由于adb命令长时间没有响应,导致程序一直没法执行完毕解决办法:给出超时检测,...

  • golang执行外部命令超时处理

    golang执行外部命令超时处理 不至于当前程序挂起,超时杀死超时进程。

  • 检测网络请求超时

    问题:小程序设置超时后,有时网络不好请求不到数据,页面空白。怎么检测网络超时,对空白页面及时作出处理,提升用户体验...

  • go get xxx timeout

    问题描述 想用golang做一爬虫小项目,安装golang包PuerkitoBio/goquery时发现被墙了超时...

  • Go每日精选(2019-06-16)

    1.io.Reader 解析 2.go 短连接和长连接 超时处理 3.golang 详解 defer 4. 理解 ...

  • 剑桥大学-Time,clocks,and ordering of

    分布式系统需要时间1.调度程序,超时,故障检测器2.性能测量3.日志文件和数据库:记录事件发生的时间 具有时间限制...

  • 以太坊 cd go-ethereum 、make geth超时问

    解决https://proxy.golang.org/github.com 报443 超时问题 make geth...

  • Golang time.After和context.WithTi

    Golang time.After和context.WithTimeout用于处理超时1.time.After定义...

  • 监控主要关注点

    服务监控 1、异常检测:场景流量波动百分比 2、异常检测:场景超时百分比 3、异常检测:场景异常百分比 4、空结果...

  • 面试总结

    笔试题 1.如何对Golang程序做性能分析和调优?如何排查Golang程序的内存泄漏? 1)使用golang的工...

网友评论

      本文标题:2:golang程序超时检测

      本文链接:https://www.haomeiwen.com/subject/lxwtdctx.html