我遇到了一个goroutines问题。假设有一个通道,我们通过main上的goroutine通过了该通道。现在,如果我们无法从main收听此频道(以防在收听之前发生返回/恐慌)。 goroutine不会停止。出现错误时如何停止此goroutine?
如果在goroutine中多次调用该函数,则例程的数量会不断增加。
package main
import (
"fmt"
"runtime"
)
func test(a chan string) {
defer func() {
close(a)
fmt.Println("channel close")
}()
fmt.Println("sending to channel")
a <- "1"
fmt.Println("sent to channel")
}
func method() string {
fmt.Println("method starting no. of routine=>",
runtime.NumGoroutine())
b := make(chan string)
go test(b)
fmt.Println("method current no. of routine=>",
runtime.NumGoroutine())
return "error" //if this is executed the routines keeps on
//increasing
a := <-b
return a
}
func main() {
defer fmt.Println("final main no. of routine=>",
runtime.NumGoroutine())
i := 0
//firing 10 request for method
for {
if i < 10 {
fmt.Println(method())
i++
} else {
break
}
}
}
输出:
method starting no. of routine=> 1
method current no. of routine=> 2
error
method starting no. of routine=> 2
method current no. of routine=> 3
error
method starting no. of routine=> 3
method current no. of routine=> 4
error
.....这样的增长趋势不断
答案 0 :(得分:1)
例程可以根据上下文停止。 在使用上下文之前,应该知道只有带循环的例程才是期望的停止控制,那些可破坏的例程无需停止。
上下文示例:
func main(){
ctx, cancel := context.WithCancel(context.Background())
go func(c context.Context){
for {
select{
case <-c.Done():
fmt.Println("exit success")
default:
// service
fmt.Println("my logic service loop")
}
}
}(ctx)
time.Sleep(5 * time.Second)
cancel()
}