本文介绍了Golang goroutine不会在里面运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 以下是我的代码: package main import(fmtosbufio sync) //将数据加载到通道 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!=无{ fmt.Println(err) os.Exit(-1)} 推迟file.Close() reader:= bufio.NewReaderSize(file ,32 * 10 * 1024)i:= 0 用于{ line,err:= reader.ReadString('\\\') channel if err!= nil { break } i ++ if i%200 == 0 { fmt.Println( ), fmt.Println(finish laodData,路径)} wg.Done() } $ b $ //将数据行分派到不同的映射器 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(){路径:= os.Args 如果len(路径)< 2 { fmt.Println(Need Input Files) os.Exit(0)} var wg sync.WaitGroup wg.Add(2) 通道:= make(chan字符串)延迟关闭(通道) fmt.Println(调度前) go laodData 去调度员(频道,wg) wg.Wait() fmt.Println(调度员后) } 以下是我的输出: ... 完成laodData result.txt throw:所有goroutines都睡着了 - 死锁! goroutine 1 [semacquire]: sync.runtime_Semacquire(0x42154100,0x42154100) /usr/local/go/src/pkg/runtime/zsema_amd64.c:146 + 0x25 (* 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]:由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 由main.main创建 /Users/kuankuan/go/src/mreasy/main.go:65 + 0x228 退出状态2 谢谢! 当main goroutine退出时程序终止,因此 dispatcher()没有时间做任何事情。您需要在 main()中封锁,直到 dispatcher()完成。 Channel可用于此: 包主 导入(fmt osbufio) var done = make(chan bool)//创建频道 //载入文件并将它们发送到地图阅读器的频道。 func dispatcher(arr [] string,channel chan string){ for _,path:= range arr { file,err:= os.Open(path) fmt .Println(开始发送,路径) if err!= nil { fmt.Println(err) os.Exit(-1)} 推迟file.Close()阅读器:= bufio.NewReaderSize(文件,32 * 10 * 1024)i:= 0 用于{行,_:= reader。 ReadString('\\\') channel i ++ if i%200 == 0 { fmt.Println(i,lines parsed) fmt.Println(完成调度,路径)} 完成< - true //通知main()完成 } $ b func main(){ path:= os.Args if len(path)< 2 { fmt.Println(Need Input Files) os.Exit(0)} channel:= make(chan string) fmt.Println (dispatcher之前)去调度员(路径[1:],频道) fmt.Println(调度员后)} I'm trying to implement a word count program, but with the first step i got some problem.Here's my code:package mainimport ( "fmt" "os" "bufio" "sync")// Load data into channelfunc 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 mappersfunc 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")}And here's my output:...finish laodData result.txtthrow: all goroutines are asleep - deadlock!goroutine 1 [semacquire]:sync.runtime_Semacquire(0x42154100, 0x42154100) /usr/local/go/src/pkg/runtime/zsema_amd64.c:146 +0x25sync.(*WaitGroup).Wait(0x4213b440, 0x0) /usr/local/go/src/pkg/sync/waitgroup.go:79 +0xf2main.main() /Users/kuankuan/go/src/mreasy/main.go:66 +0x238goroutine 2 [syscall]:created by runtime.main /usr/local/go/src/pkg/runtime/proc.c:221goroutine 4 [chan receive]:main.dispatcher(0x42115a50, 0x0, 0x2, 0x0) /Users/kuankuan/go/src/mreasy/main.go:45 +0x223created by main.main /Users/kuankuan/go/src/mreasy/main.go:65 +0x228exit status 2Thanks ! 解决方案 Program terminates when main goroutine exits, so that dispatcher() has no time to do anything. You need to block in main() until dispatcher() completes. Channel can be used for this:package mainimport ( "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")} 这篇关于Golang goroutine不会在里面运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-24 04:17