【问题标题】:Deadlock in goroutines pipelinegoroutines管道中的死锁
【发布时间】:2021-01-05 17:18:42
【问题描述】:

我需要您的帮助来了解为什么我的 readFromWorker 函数会导致死锁。当我注释掉下面这样的行时,它可以正常工作(因此我知道问题就在这里)。

全在这里https://play.golang.org/p/-0mRDAeD2tr

非常感谢您的帮助

func readFromWorker(inCh <-chan *data, wg *sync.WaitGroup) {
    defer func() {
        wg.Done()
    }()

    //stageIn1 := make(chan *data)
    //stageOut1 := make(chan *data)

    for v := range inCh {
        fmt.Println("v", v)

        //stageIn1 <- v
    }

    //go stage1(stageIn1, stageOut1)
    //go stage2(stageOut1)
}

【问题讨论】:

  • 当您发送到stageIn1 时,没有同时从它接收任何内容,并且通道没有缓冲。
  • 将您的 go stage1() 和 go stage2() 调用移至 for 循环之前。

标签: go goroutine


【解决方案1】:

我已经评论了你做错的相关部分。另外,我建议考虑一个更好的模式。

请记住,频道上的for range 不会停止循环,除非在它循环的同一频道上调用close。此外,关闭通道的经验法则是发送到通道的发送者也必须关闭它,因为发送到关闭的通道会导致panic。

此外,在使用无缓冲和缓冲通道时要非常小心。对于无缓冲通道,发送方和接收方必须准备好,否则您的情况也会发生死锁。

package main

import (
    "fmt"
    "sync"
)

type data struct {
    id    int
    url   string
    field int
}

type job struct {
    id  int
    url string
}

func sendToWorker(id int, inCh <-chan job, outCh chan<- *data, wg *sync.WaitGroup) {
    // wg.Done() is itself a function call, no need to wrap it inside
    // an anonymous function just to use defer.
    defer wg.Done()

    for v := range inCh {
        // some pre process stuff and then pass to pipeline
        outCh <- &data{id: v.id, url: v.url}
    }
}

func readFromWorker(inCh <-chan *data, wg *sync.WaitGroup) {
    // wg.Done() is itself a function call, no need to wrap it inside
    // an anonymous function just to use defer.
    defer wg.Done()

    var (
        stageIn1  = make(chan *data)
        stageOut1 = make(chan *data)
    )

    // Spawn the goroutines so that there's no deadlock
    // as the sender and receiver both should be ready
    // when using unbuffered channels.
    go stage1(stageIn1, stageOut1)
    go stage2(stageOut1)

    for v := range inCh {
        fmt.Println("v", v)
        stageIn1 <- v
    }
    close(stageIn1)
}

func stage1(in <-chan *data, out chan<- *data) {
    for s := range in {
        fmt.Println("stage1 = ", s)
        out <- s
    }
    // Close the out channel
    close(out)
}

func stage2(out <-chan *data) {
    // Loop until close
    for s := range out {
        fmt.Println("stage2 = ", s)
    }
}

func main() {
    const chanBuffer = 1

    var (
        inputsCh  = make(chan job, chanBuffer)
        resultsCh = make(chan *data, chanBuffer)

        wgInput  sync.WaitGroup
        wgResult sync.WaitGroup
    )

    for i := 1; i <= 4; i++ {
        wgInput.Add(1)
        go sendToWorker(i, inputsCh, resultsCh, &wgInput)
    }

    wgResult.Add(1)
    go readFromWorker(resultsCh, &wgResult)

    for j := 1; j <= 10; j++ {
        inputsCh <- job{id: j, url: "google.com"}
    }

    close(inputsCh)
    wgInput.Wait()
    close(resultsCh)
    wgResult.Wait()
}

【讨论】:

  • 谢谢,现在我明白我的错误了。我首先使用无缓冲通道,以便在发生任何情况时更容易捕获死锁。更好的模式是什么意思?只是不使用管道还是重构我的所有代码?我刚开始学习 Go,所以任何建议对我都很有价值。
  • 好!作为 Go 的初学者,您可以观看 Web 上与并发模式相关的教程。另请阅读:blog.golang.org/pipelines。通过经常练习,您将能够以非常干净和有效的方式编写您的逻辑。
  • 如果您对我的回答满意,能否将其标记为已接受?它将帮助 StackOverflow 上面临类似问题的其他人。
猜你喜欢
  • 1970-01-01
  • 2018-07-29
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-03
相关资源
最近更新 更多