cmd.Output in concurrent, get empty byte slide randomly

Viewed 44

I am using Cmd.Output to get the results from a series of commands concurrently. It can worked well in serial, but after I turned to code into parallel, some output byte slides return empty (should be contents inside), and some are well. Can I ask anyone know the reason? Thank you! I tried to make a reseach but no answer.

for _, nodeArn := range nodeArnList {
           wgManifestIO.Add(1)
           go generateCmd(stage, nodeArn)
       }
func generateCmd(stage string, arn string) {
    var cmdSlice []string = make([]string, 0)   
    outfile, err := os.Create(meshName + ".json")
    if err != nil {
        fmt.Printf("os.Create: %v\n", err)
    }
    defer func() {
        if err := recover(); err != nil {
            fmt.Printf("recover: %v\n", err)
        }
        outfile.Close()
        wgManifestIO.Done()
    }()
    cmdSlice = append(cmdSlice, arg1, arg2, arg3)
    cmd := exec.Command(arg0, cmdSlice...)
    ioLock.Lock() // if wrapped the output() statement in lock, it worked again, but then it is still serial. 
    out, err := cmd.Output()
    ioLock.Unlock()
    if len(out) == 0 {
        fmt.Println("empty exception") // get some empty slides, and some successful slides, in random
    }
    _, err = outfile.Write(out)
}
0 Answers
Related