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 multisim中关于74ls192n和DSWPK开关仿真图分析(减法计数器)
  • ¥15 基于8255的交通灯设计
  • ¥15 w3wp,exe 中发生未处理的 Microsoft ,NETFramework 异常。
  • ¥20 C51单片机程序及仿真(加减器)
  • ¥15 AQWA | 水动力分析 二阶波浪力
  • ¥15 c语言题目:成绩管理系统
  • ¥22 可否请教一下matlab未定义与 'double' 类型的输入参数相对应的函数 'finverse'是什么情况
  • ¥15 利用数据集,用机器学习模型对股市预测,私我
  • ¥15 tauri + vue3 如何实现在一个页面上局部加载外部网页
  • ¥15 系统(子系统)设计(结构设计)说明(SSDD)