duancenxiao0482 2015-02-03 13:51
浏览 127


package main
import (

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 
) 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
        fmt.Printf("%s", 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条回答 默认 最新

  • drhs13583567608 2015-02-03 14:39

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 Mac虚拟机安装Windows,Windows中用idea跑项目,报错process finished with exit code -1073741819(0xc0000005) 如何解决?
  • ¥15 关于python爬虫返回js数据结构的处理
  • ¥15 ubuntu所用stm32标准库开发烧录没反映
  • ¥15 运维工程师(在职人员回答)
  • ¥15 python 爬虫问题
  • ¥15 Kmeans的趋势分析有会的吗
  • ¥20 R语言单细胞测序 对seurat对象进行标准化时出现问题
  • ¥15 Anaconda navigator updateor一直更新
  • ¥15 请问如何在matlab里使用raven工具?
  • ¥100 关于ios手游充值到一定金额,再点充值提示公众号的问题。