为什么我的 Golang 通道写入会一直阻塞?

huangapple go评论99阅读模式
英文:

Why is my Golang Channel Write Blocking Forever?

问题

我最近几天一直在尝试使用Golang进行并发编程,通过重构我的命令行实用程序,但是我遇到了问题。

这里是原始代码(主分支)。

这里是并发代码的分支(x_concurrent分支)。

当我使用go run jira_open_comment_emailer.go执行并发代码时,如果将JIRA问题添加到这里的通道中,defer wg.Done()永远不会执行,导致我的wg.Wait()永远挂起。

我的想法是,我有大量的JIRA问题,我想为每个问题启动一个goroutine,以查看是否有我需要回复的评论。如果有,我希望将其添加到某个结构(经过一些研究后选择了通道),稍后可以像队列一样从中读取,以构建一个电子邮件提醒。

这是代码的相关部分:

// 给定一个问题,确定它是否有一个开放的评论
// 如果问题有一个开放的评论,则返回true,否则返回false
func getAndProcessComments(issue Issue, channel chan<- Issue, wg *sync.WaitGroup) {
    // 当函数返回时,减少等待计数器
    defer wg.Done()

    needsReply := false

    // 遍历问题中的评论
    for _, comment := range issue.Fields.Comment.Comments {
        commentMatched, err := regexp.MatchString("~"+config.JIRAUsername, comment.Body)
        checkError("Failed to regex match against comment body", err)

        if commentMatched {
            needsReply = true
        }

        if comment.Author.Name == config.JIRAUsername {
            needsReply = false
        }
    }

    // 只有当需要回复时才将问题添加到通道中
    if needsReply == true {
        // 这里为什么不允许执行deferred wg.Done()?
        channel <- issue
    }
}

func main() {
    start := time.Now()

    // 从JIRA检索所有问题
    allIssues := getFullIssueList()

    // 初始化一个等待组
    var wg sync.WaitGroup

    // 将等待数设置为要处理的问题数
    wg.Add(len(allIssues))

    // 创建一个通道来存储需要回复的问题
    channel := make(chan Issue)

    for _, issue := range allIssues {
        go getAndProcessComments(issue, channel, &wg)
    }

    // 阻塞,直到所有的goroutine都处理完它们的问题。
    wg.Wait()

    // 只有当通道中有一个或多个问题时才发送电子邮件
    if len(channel) > 0 {
        sendEmail(channel)
    }

    fmt.Printf("Script ran in %s", time.Since(start))
}
英文:

I've been attempting to take a swing at concurrency in Golang by refactoring one of my command-line utilities over the past few days, but I'm stuck.

Here's the original code (master branch).

Here's the branch with concurrency (x_concurrent branch).

When I execute the concurrent code with go run jira_open_comment_emailer.go, the defer wg.Done() never executes if the JIRA issue is added to the channel here, which causes my wg.Wait() to hang forever.

The idea is that I have a large amount of JIRA issues, and I want to spin off a goroutine for each one to see if it has a comment I need to respond to. If it does, I want to add it to some structure (I chose a channel after some research) that I can read from like a queue later to build up an email reminder.

Here's the relevant section of the code:

<!-- language-all: go -->

// Given an issue, determine if it has an open comment
// Returns true if there is an open comment on the issue, otherwise false
func getAndProcessComments(issue Issue, channel chan&lt;- Issue, wg *sync.WaitGroup) {
// Decrement the wait counter when the function returns
defer wg.Done()
needsReply := false
// Loop over the comments in the issue
for _, comment := range issue.Fields.Comment.Comments {
commentMatched, err := regexp.MatchString(&quot;~&quot;+config.JIRAUsername, comment.Body)
checkError(&quot;Failed to regex match against comment body&quot;, err)
if commentMatched {
needsReply = true
}
if comment.Author.Name == config.JIRAUsername {
needsReply = false
}
}
// Only add the issue to the channel if it needs a reply
if needsReply == true {
// This never allows the defered wg.Done() to execute?
channel &lt;- issue
}
}
func main() {
start := time.Now()
// This retrieves all issues in a search from JIRA
allIssues := getFullIssueList()
// Initialize a wait group
var wg sync.WaitGroup
// Set the number of waits to the number of issues to process
wg.Add(len(allIssues))
// Create a channel to store issues that need a reply
channel := make(chan Issue)
for _, issue := range allIssues {
go getAndProcessComments(issue, channel, &amp;wg)
}
// Block until all of my goroutines have processed their issues.
wg.Wait()
// Only send an email if the channel has one or more issues
if len(channel) &gt; 0 {
sendEmail(channel)
}
fmt.Printf(&quot;Script ran in %s&quot;, time.Since(start))
}

答案1

得分: 32

goroutine在向无缓冲通道发送数据时会被阻塞。为了解除goroutine的阻塞,最简单的方法是创建一个具有足够容量的缓冲通道:

channel := make(chan Issue, len(allIssues))

并在调用wg.Wait()之后关闭通道。

英文:

The goroutines block on sending to the unbuffered channel.
A minimal change unblocks the goroutines is to create a buffered channel with capacity for all issues:

channel := make(chan Issue, len(allIssues))

and close the channel after the call to wg.Wait().

huangapple
  • 本文由 发表于 2016年5月25日 22:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/37439776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定