如何关闭 "asleep" 的所有 goroutine?
How do I close all the goroutines that are "asleep"?
我在 for 循环中有一个 goroutine 运行ning:
func main(){
for _, i := range x{
go httpRequests(i, ch)
}
for i := range ch{
print i
}
}
func httpRequests(i, ch){
for _, x := range y{
go func(string x){
do something with i
ch <- result
}(x)
}
}
当我 运行 那个时,它说所有的 goroutines 都睡着了。有什么建议吗?
您启动了 3 个 goroutines (go serviceReq(i, httpCh)
),并向它们传递了一个通道。然后您在该频道上仅收到 一次 (ch := (<-httpCh).serviceData
).
你应该循环接收:
for resp := range httpCh {
output = append(output, resp.serviceData)
}
我在 for 循环中有一个 goroutine 运行ning:
func main(){
for _, i := range x{
go httpRequests(i, ch)
}
for i := range ch{
print i
}
}
func httpRequests(i, ch){
for _, x := range y{
go func(string x){
do something with i
ch <- result
}(x)
}
}
当我 运行 那个时,它说所有的 goroutines 都睡着了。有什么建议吗?
您启动了 3 个 goroutines (go serviceReq(i, httpCh)
),并向它们传递了一个通道。然后您在该频道上仅收到 一次 (ch := (<-httpCh).serviceData
).
你应该循环接收:
for resp := range httpCh {
output = append(output, resp.serviceData)
}