【发布时间】:2016-10-06 10:20:46
【问题描述】:
我有以下 go-Code:
func execTask(input int, results chan<- int) {
//do stuff (in my case, start process and return something)
results <- someResult
}
func main() {
results := make(chan int)
for _, task := range tasks {
go execTask(task, results)
}
for result := range results {
fmt.Println(result)
}
}
对于for result := range results { 行,我得到一个错误:
fatal error: all goroutines are asleep - deadlock!。在例程execTask中我其实用os/exec执行了一个进程,所以不知道results中有多少结果。所以我必须等待我所有的过程完成,但同时对结果做一些事情。当所有进程都终止时,我的 go-Programm 也可能会终止。
我该怎么做?
谢谢, 拉尔斯
【问题讨论】:
-
results := make(chan int) for _, task := range tasks { go execTask(task, results) } 任务也在调用结果,这会导致死锁..
标签: go concurrency goroutine