如何停止多个 go 例程

我必须从循环中调用戈鲁图因/线程。由于循环,有许多 go 例程并行执行。如果任何例程/线程成功执行,那么我必须停止所有其他线程/例程。

有没有办法实现这个?


湖上湖
浏览 87回答 3
3回答

狐的传说

除了Burak回答的上下文之外,您还可以使用退出频道。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "math/rand"&nbsp; &nbsp; "time")func foo(channel, quit chan string, i int) {&nbsp; &nbsp; channel <- fmt.Sprintf("goroutine %d started!", i)&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; rand.Seed(time.Now().UnixNano())&nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(time.Duration(rand.Intn(500)+500) * time.Millisecond)&nbsp; &nbsp; &nbsp; &nbsp; quit <- fmt.Sprintf("goRoutine %d completed!", i)&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; channel := make(chan string)&nbsp; &nbsp; quit := make(chan string)&nbsp; &nbsp; for i := 0; i < 3; i++ {&nbsp; &nbsp; &nbsp; &nbsp; go foo(channel, quit, i)&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; case update:= <-channel:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(update)&nbsp; &nbsp; &nbsp; &nbsp; case quit:= <-quit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(quit)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

莫回无

您可以使用上下文:ctx, cancel:= context.WithCancel(context.Background())for ... {&nbsp; &nbsp;go func() {&nbsp; &nbsp; &nbsp; defer cancel() // cancel context once this goroutine ends&nbsp; &nbsp; &nbsp; doStuff(ctx)&nbsp; &nbsp;}()}您必须在戈鲁丁中检查上下文取消:func doStuff(ctx context.Context) {&nbsp; &nbsp;...&nbsp; &nbsp;if ctx.Err()!=nil {&nbsp; &nbsp; &nbsp; &nbsp;// Canceled, return&nbsp; &nbsp; &nbsp; &nbsp;return&nbsp; &nbsp;}&nbsp; ...}这并不能保证一旦上下文被取消,其他戈鲁廷将立即结束,但它保证,如果您正确检查上下文取消,所有戈鲁廷最终都会结束。

暮色呼如

您可以使用上下文和错误组。一些类似的东西...package mainimport (&nbsp; &nbsp; "context"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "math/rand"&nbsp; &nbsp; "os"&nbsp; &nbsp; "time"&nbsp; &nbsp; "golang.org/x/sync/errgroup")func doStuff(ctx context.Context, i int) error {&nbsp; &nbsp; count := 0&nbsp; &nbsp; for ctx.Err() == nil {&nbsp; &nbsp; &nbsp; &nbsp; // do the stuff&nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(time.Millisecond * time.Duration(rand.Intn(500)))&nbsp; &nbsp; &nbsp; &nbsp; count++&nbsp; &nbsp; &nbsp; &nbsp; if count > 6 { // error condition&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintf(os.Stdout, "%v reached count %v\n", i, count)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return fmt.Errorf("Error %v, count %v\n", i, count)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Fprintf(os.Stdout, "Killed %v @ count %v\n", i, count)&nbsp; &nbsp; return ctx.Err()}func main() {&nbsp; &nbsp; rand.Seed(int64(time.Now().Nanosecond()))&nbsp; &nbsp; ctxWc, _ := context.WithCancel(context.Background())&nbsp; &nbsp; g, ctx := errgroup.WithContext(ctxWc)&nbsp; &nbsp; for i := 0; i < 5; i++ {&nbsp; &nbsp; &nbsp; &nbsp; i := i&nbsp; &nbsp; &nbsp; &nbsp; g.Go(func() error {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return doStuff(ctx, i)&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; &nbsp; err := g.Wait()&nbsp; &nbsp; fmt.Println("The End")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go