Go 超时中断然后终止
本文向大家介绍Go 超时中断然后终止,包括了Go 超时中断然后终止的使用技巧和注意事项,需要的朋友参考一下
示例
c := exec.Command(name, arg...) b := &bytes.Buffer{} c.Stdout = b c.Stdin = stdin if err := c.Start(); err != nil { return nil, err } timedOut := false intTimer := time.AfterFunc(timeout, func() { log.Printf("Process taking too long. Interrupting: %s %s", name, strings.Join(arg, " ")) c.Process.Signal(os.Interrupt) timedOut = true }) killTimer := time.AfterFunc(timeout*2, func() { log.Printf("Process taking too long. Killing: %s %s", name, strings.Join(arg, " ")) c.Process.Signal(os.Kill) timedOut = true }) err := c.Wait() intTimer.Stop() killTimer.Stop() if timedOut { log.Print("the process timed out\n") }