试图理解go上下文取消将如何中止后续代码的执行
实验细节:
2sec中超时的上下文sum -用于测试运行的1sec -1和测试运行的4sec -2。3sec的主睡眠让旋转完成执行package main
import (
"context"
"fmt"
"log"
"time"
)
func main() {
c := context.Background()
childCtx, cancel := context.WithTimeout(c, 2*time.Second)
defer cancel()
ch := make(chan int, 1)
go sum(5, 6, ch)
var msg string
select {
case <-childCtx.Done():
msg = "return from ctx done channel"
case res := <-ch:
msg = fmt.Sprintf("return from go routine: %v", res)
}
log.Print(msg)
time.Sleep(3 * time.Second) //sleeping here to test if go-routine is still running
}
func sum(x int, y int, c chan<- int) {
time.Sleep(1 * time.Second)
//testcase-1: sleep - 1s
//testcase-2: sleep - 4s
result := x + y
log.Printf("print from sum fn: %v", result)
c <- result
}对testcase的响应-1:睡眠和函数1秒:
2021/04/12 01:06:58 print from sum fn: 11
2021/04/12 01:06:58 return from go routine: 11对testcase-2的响应:睡眠和函数4秒:
2021/04/12 01:08:25 return from ctx done channel
2021/04/12 01:08:27 print from sum fn: 11在testcase-2中,当sum func休眠4秒时,上下文在2秒后已经被超时所取消,为什么它仍然在diff go例程中执行sum func并打印print from sum fn: 1?
来自文档:取消此上下文将释放与其相关的资源。
我的假设是,所有的计算都会在2秒后立即中止,包括旋转的go例程。
让我知道如何正确地做这件事,谢谢
发布于 2021-04-11 20:27:46
正如@AndySchweig所指出的,context发出取消事件的信号,但不强制取消。这取决于任何潜在的阻塞戈鲁丁,尽其最大的努力,试图取消/清理后,它检测到取消。
要更新sum函数以支持取消,您可以尝试:
// add context parameter as the first argument
// add a return error - to indicate any errors (i.e. function was interrupted due to cancelation)
func sum(ctx context.Context, x int, y int, c chan<- int) (err error) {
wait := 1 * time.Second // testcase-1
//wait := 4 * time.Second // testcase-2
// any blocking called - even sleeps - should be interruptible
select {
case <-time.After(wait):
case <-ctx.Done():
err = ctx.Err()
return
}
result := x + y
log.Printf("print from sum fn: %v", result)
select {
case c <- result:
case <-ctx.Done(): // check for ctx cancelation here - as no one may be listening on result channel
err = ctx.Err()
}
return
}发布于 2021-04-11 20:34:05
上下文不会中止go例程。在您的例子中,如果上下文的时间超出,您就不会打印go例程的结果。go例程对上下文一无所知。
https://stackoverflow.com/questions/67049687
复制相似问题