GOLANG:致命错误:所有的goroutine都处于休眠状态 – 死锁

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

GOLANG: fatal error: all goroutines are asleep - deadlock

问题

package main
import (
    "fmt"
    "os"
    "os/exec"
    "bufio"
    "reflect"
)

func runTheCommand(ch chan<- string, cmD string)  {
    ouT,_ := exec.Command("sh","-c",cmD).Output()
    ch <- string(ouT)
}
// Readln returns a single line (without the ending \n) from the input buffered reader. An error is returned iff there is an error with the buffered reader.
func Readln(r *bufio.Reader) (string, error) {
    var (
        isPrefix bool = true
        err error = nil 
        line, ln []byte
    )   
    for isPrefix && err == nil {
        line, isPrefix, err = r.ReadLine()
        ln = append(ln, line...)
    }   
    return string(ln),err
}



func main() {
    var chans = []chan string{}
    f, _ := os.Open("../tmpStatus.config")
    r := bufio.NewReader(f)
    cmD, e := Readln(r)
    for e == nil {
        ch := make(chan string)
        chans = append(chans, ch) 
        go runTheCommand(ch,cmD)
        cmD,e = Readln(r)
    }   

    cases := make([]reflect.SelectCase, len(chans))
    for i, ch := range chans {
        cases[i] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(ch)}
    }   

    remaining := len(cases)
    for remaining > 0 { 
        chosen, value, ok := reflect.Select(cases)
        if !ok {
            cases[chosen].Chan = reflect.ValueOf(nil)
            remaining -= 1
            continue
        }
        fmt.Printf("%s", value.String())
    }   
}

这段代码正确地打印出了从通道接收到的所有值。但最后却出现了以下错误:

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [select]: reflect.rselect(0xc200283480, 0x22, 0x22, 0xffffffffffffffff, 0x0, ...)
/usr/local/go/src/pkg/runtime/chan.c:1212 +0x10d
reflect.Select(0xc200066800, 0x22, 0x22, 0x1, 0x0, ...)
/usr/local/go/src/pkg/reflect/value.go:1957 +0x1fb

我是一个Go的新手。我参考了谷歌并设法编写了一个可工作的代码。我并没有完全理解这个脚本是如何工作的,特别是reflect包的命令。我在这里的目的是并行执行../tmpStatus.config文件中列出的Unix命令并打印结果。我对Go提供的并发特性感到兴奋,所以决定试一试。现在它打印结果的速度非常快。到目前为止,我一直在用Python一个一个地执行这些命令。我在go version go1.1.2 linux/amd64上运行了这个脚本,它安装在一个自定义位置。有没有人知道为什么会发生死锁?

英文:
package main
import (
&quot;fmt&quot;
&quot;os&quot;
&quot;os/exec&quot;
&quot;bufio&quot;
&quot;reflect&quot;
)
func runTheCommand(ch chan&lt;- string, cmD string)  {
ouT,_ := exec.Command(&quot;sh&quot;,&quot;-c&quot;,cmD).Output()
ch &lt;- string(ouT)
}
// Readln returns a single line (without the ending \n) from the input buffered reader. An error is returned iff there is an error with the buffered reader.
func Readln(r *bufio.Reader) (string, error) {
var (isPrefix bool = true
err error = nil 
line, ln []byte
)   
for isPrefix &amp;&amp; err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}   
return string(ln),err
}
func main() {
var chans = []chan string{}
f, _ := os.Open(&quot;../tmpStatus.config&quot;)
r := bufio.NewReader(f)
cmD, e := Readln(r)
for e == nil {
ch := make(chan string)
chans = append(chans, ch) 
go runTheCommand(ch,cmD)
cmD,e = Readln(r)
}   
cases := make([]reflect.SelectCase, len(chans))
for i, ch := range chans {
cases[i] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(ch)}
}   
remaining := len(cases)
for remaining &gt; 0 { 
chosen, value, ok := reflect.Select(cases)
if !ok {
cases[chosen].Chan = reflect.ValueOf(nil)
remaining -= 1
continue
}
fmt.Printf(&quot;%s&quot;, value.String())
}   
}    

This code is correctly printing out all the received values from the channels.
But in the end it is exiting with the below error :

>fatal error: all goroutines are asleep - deadlock!

> goroutine 1 [select]: reflect.rselect(0xc200283480, 0x22, 0x22,
> 0xffffffffffffffff, 0x0, ...)
> /usr/local/go/src/pkg/runtime/chan.c:1212 +0x10d
reflect.Select(0xc200066800, 0x22, 0x22, 0x1, 0x0, ...)
> /usr/local/go/src/pkg/reflect/value.go:1957 +0x1fb

I am a newbee at GO. I referred google and somehow managed to write a working code. I didn't understand fully how this script works especially the reflect package commands. My intention here is to execute the unix commands listed in ../tmpStatus.config file parallely and print out the results. I am excited by the concurrency features offered by GO and hence decided to give it a try. Now it is printing the results so fast. Till now, I was doing this in python one by one.
I ran this script in go version go1.1.2 linux/amd64 installed to a custom location.
Any idea why the deadlock is happening ?

答案1

得分: 2

看起来你正在尝试从文件中读取内容,然后将工作分配给多个工作器(扇出)。我在你的代码中没有看到任何关闭通道的操作。你可以参考这个链接https://gist.github.com/kylewolfe/7c54018a9ed16517c0dd,里面有一个我所指的模式的示例。对于文件的每一行,你可以将其读入一个通道,然后x个工作器可以从该通道中读取内容(并进行处理/工作)。

英文:

It looks like you are trying to to read from a file and then split the work across multiple workers (fan out). I do not see any closing of channels in your code. See https://gist.github.com/kylewolfe/7c54018a9ed16517c0dd for an example of the pattern I'm referring to. For each line of your file, you would read into the one channel, where x workers would read from it (and reflect / do work).

huangapple
  • 本文由 发表于 2015年2月3日 21:51:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/28300581.html
匿名

发表评论

匿名网友

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

确定