[英]Golang goroutine doesn't run with channel inside
我正在嘗試實施一個字數統計程序,但第一步我遇到了一些問題。
這是我的代碼:
package main
import (
"fmt"
"os"
"bufio"
"sync"
)
// Load data into channel
func laodData(arr []string,channel chan string,wg sync.WaitGroup) {
for _,path := range arr {
file,err := os.Open(path)
fmt.Println("begin to laodData ", path)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
defer file.Close()
reader := bufio.NewReaderSize(file, 32*10*1024)
i := 0
for {
line,err := reader.ReadString('\n')
channel <- line
if err != nil {
break
}
i++
if i%200 == 0 {
fmt.Println(i," lines parsed")
}
}
fmt.Println("finish laodData ", path)
}
wg.Done()
}
// dispatch data lines into different mappers
func dispatcher(channel chan string,wg sync.WaitGroup){
fmt.Println("pull data 11")
line,ok := <- channel
fmt.Println(ok)
for ok {
fmt.Println(line)
line,ok = <- channel
}
fmt.Println("pull data 22")
wg.Done()
}
func main() {
path := os.Args
if len(path) < 2 {
fmt.Println("Need Input Files")
os.Exit(0)
}
var wg sync.WaitGroup
wg.Add(2)
channel := make(chan string)
defer close(channel)
fmt.Println("before dispatcher")
go laodData(path[1:],channel,wg)
go dispatcher(channel,wg)
wg.Wait()
fmt.Println("after dispatcher")
}
這是我的輸出:
...
finish laodData result.txt
throw: all goroutines are asleep - deadlock!
goroutine 1 [semacquire]:
sync.runtime_Semacquire(0x42154100, 0x42154100)
/usr/local/go/src/pkg/runtime/zsema_amd64.c:146 +0x25
sync.(*WaitGroup).Wait(0x4213b440, 0x0)
/usr/local/go/src/pkg/sync/waitgroup.go:79 +0xf2
main.main()
/Users/kuankuan/go/src/mreasy/main.go:66 +0x238
goroutine 2 [syscall]:
created by runtime.main
/usr/local/go/src/pkg/runtime/proc.c:221
goroutine 4 [chan receive]:
main.dispatcher(0x42115a50, 0x0, 0x2, 0x0)
/Users/kuankuan/go/src/mreasy/main.go:45 +0x223
created by main.main
/Users/kuankuan/go/src/mreasy/main.go:65 +0x228
exit status 2
謝謝 !
當main goroutine退出時程序終止,因此dispatcher()
沒有時間做任何事情。 您需要在main()
阻塞,直到dispatcher()
完成。 頻道可用於此:
package main
import (
"fmt"
"os"
"bufio"
)
var done = make(chan bool) // create channel
// Load files and send them into a channel for mappers reading.
func dispatcher(arr []string,channel chan string) {
for _,path := range arr {
file,err := os.Open(path)
fmt.Println("begin to dispatch ", path)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
defer file.Close()
reader := bufio.NewReaderSize(file, 32*10*1024)
i := 0
for {
line,_ := reader.ReadString('\n')
channel <- line
i++
if i%200 == 0 {
fmt.Println(i," lines parsed")
}
}
fmt.Println("finish dispatch ", path)
}
done <- true // notify main() of completion
}
func main() {
path := os.Args
if len(path) < 2 {
fmt.Println("Need Input Files")
os.Exit(0)
}
channel := make(chan string)
fmt.Println("before dispatcher")
go dispatcher(path[1:],channel)
<-done // wait for dispatcher()
fmt.Println("after dispatcher")
}
我修改你的例子在Go操場上運行,那里沒有文件I / O; 它會在頻道上發送隨機數。
@Victor Deryagin的解釋和他使用“完成”頻道的建議是正確的。 你遇到死鎖的原因是你的goroutine在頻道上發送,但沒有人從中讀取,所以程序在這一點上被卡住了。 在上面的鏈接中,我添加了一個消費者goroutine。 然后程序按預期同時運行。
請注意,要等待幾個goroutine,使用sync.WaitGroup更清晰,更容易。
原始問題需要解決兩個問題。
laodData
,請使用close(channel)post發送所有數據。 sync.Waitgroup
作為參考傳遞。您將wg作為參數中的值發送到以下函數... laodData
和dispatcher函數。 修復這兩個問題將解決您的死鎖問題。 代碼中死鎖的原因如下:
sync.Waitgroup
的參數作為值發送。 它應該作為參考發送,否則它將創建您要發送的對象的新副本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.