dqyuipw44576 2019-03-19 17:22
浏览 97
已采纳

如何停止同一goroutine的多个

As it will be made obvious soon, I am a golang n00b.

I have some go code that starts goroutines based on an event channel. Say it starts 2 goroutines because we receive 2 events of type START.

The goroutine is started with an uri as parameter, which gives us something unique about it.

Later we receive one event of type STOP.

How can I stop the goroutine that was started with the same uri ?

for {
            select {
            case event := <-eventCh:
                if event.Entry != nil {
                    switch event.Action {
                    case foo.START:
                        log.Println("uri: ", event.Entry.URI)

                        go func(c chan []byte, u string) error{
                            //awesome goroutine code
                        }(myChan, event.Entry.URI)

                    case foo.STOP:
                        log.Println("uri: ", event.Entry.URI)
                        //I'd like to terminate the goroutine that matches event.Entry.URI
                    }
                }
            }
        }
  • 写回答

1条回答 默认 最新

  • dousi6192 2019-03-19 17:41
    关注

    You cannot stop a goroutine "from the outside". You have to pass some kind of cancellation signal to each goroutine and remember them for later in the main goroutine. A Context is typically used as a cancellation signal. The goroutine then has to check for cancellation and exit voluntarily:

    package main
    
    import (
        "context"
    )
    
    type Event struct {
        Action string
        URI    string
    }
    
    func main() {
        var eventCh chan Event
    
        ctx := context.Background()
    
        cancels := make(map[string]context.CancelFunc) // Maps URIs to cancellation functions.
    
        for event := range eventCh {
            switch event.Action {
            case "START":
                if cancels[event.URI] != nil {
                    panic("duplicate URI: " + event.URI)
                }
    
                ctx, cancel := context.WithCancel(ctx)
                cancels[event.URI] = cancel
                defer cancel() // cancel must always be called to free resources.
    
                go func(u string) {
                    // Awesome goroutine code
    
                    // Check ctx.Done or ctx.Err in strategic places and return if done.
                    select {
                    case <-ctx.Done():
                        return
                    default:
                    }
    
                    // More awesome goroutine code
    
                    if ctx.Err() != nil {
                        return
                    }
    
                    // Even more awesome goroutine code
    
                }(event.URI)
    
            case "STOP":
                if cancel, ok := cancels[event.URI]; ok {
                    cancel()
                    delete(cancels, event.URI)
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决