dps69208 2019-04-05 12:19
浏览 35

如何以惯用方式正常关闭链接的goroutine

Creating multiple goroutines which will have nested goroutines while processing in a multilevel manner (Imagine a tree of goroutines each level can have many leaves).

What is the idiomatic way to gracefully shutdown these goroutines in order and wait for them to come back? Order is the bottom top (deepest child first) and also assume I dont know how many goroutines I will launch beforehand (dynamic).

the example below just gracefully shuts them down in an non ordered manner.

package main

import (
    "context"
    "fmt"
    "time"
)

func main() {
    ctx := context.Background()
    ctx, cancel := context.WithCancel(ctx)

    //level1
    go func() {
        fmt.Println("level1 started")
        //level2
        go func() {
            fmt.Println("level2 started")

            //level3
            go func() {
                fmt.Println("level3 started")
                select {
                case <-ctx.Done():
                    fmt.Println("Done called on level3")
                case <-time.After(5* time.Second):
                    fmt.Println("After called on level3")
                }

            }()
            select {
            case <-ctx.Done():
                fmt.Println("Done called on level2")
            case <-time.After(7* time.Second):
                fmt.Println("After called on level2")
            }

        }()
        select {
        case <-ctx.Done():
            fmt.Println("Done called on level1")
        case <-time.After(10* time.Second):
            fmt.Println("After called on level1")
        }


    }()
    time.Sleep(1*time.Second)
    cancel()
    time.Sleep(1 * time.Second)
}
  • 写回答

2条回答 默认 最新

  • doujindou4356 2019-04-05 12:28
    关注

    To wait for a group of goroutines, sync.WaitGroup is the idiomatic solution. You can add 1 to its counter when you launch a new goroutine (WaitGroup.Add()), and the goroutine can signal that it's done with WaitGroup.Done(). The parent goroutine may call WaitGroup.Wait() to wait all its children to finish.

    You may do the same on each level. Create a WaitGroup on each level where child goroutines are launched, and only return when Wait() of that goroutine returns.

    Here's how it's applied on your example:

    ctx := context.Background()
    ctx, cancel := context.WithCancel(ctx)
    
    //level1
    wg1 := &sync.WaitGroup{}
    wg1.Add(1)
    go func() {
        defer wg1.Done()
        fmt.Println("level1 started")
        //level2
        wg2 := &sync.WaitGroup{}
        wg2.Add(1)
        go func() {
            defer wg2.Done()
            fmt.Println("level2 started")
    
            //level3
            wg3 := &sync.WaitGroup{}
            wg3.Add(1)
            go func() {
                defer wg3.Done()
                fmt.Println("level3 started")
                select {
                case <-ctx.Done():
                    fmt.Println("Done called on level3")
                case <-time.After(5 * time.Second):
                    fmt.Println("After called on level3")
                }
                fmt.Println("Level 3 ended.")
            }()
    
            select {
            case <-ctx.Done():
                fmt.Println("Done called on level2")
            case <-time.After(7 * time.Second):
                fmt.Println("After called on level2")
            }
            wg3.Wait()
            fmt.Println("Level 2 ended.")
        }()
    
        select {
        case <-ctx.Done():
            fmt.Println("Done called on level1")
        case <-time.After(10 * time.Second):
            fmt.Println("After called on level1")
        }
        wg2.Wait()
        fmt.Println("Level 1 ended.")
    }()
    
    time.Sleep(1 * time.Second)
    cancel()
    wg1.Wait()
    fmt.Println("Main ended.")
    

    This outputs (try it on the Go Playground):

    level1 started
    level2 started
    level3 started
    Done called on level1
    Done called on level3
    Level 3 ended.
    Done called on level2
    Level 2 ended.
    Level 1 ended.
    Parent ended.
    

    What's important from the output:

    Level 3 ended.
    Level 2 ended.
    Level 1 ended.
    Main ended.
    

    Levels end in descending level order (from bottom-up), closing with "Main ended.".

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀