deadlock with double channel
我正在尝试创建一个程序(通过通道)将字符串发送到goroutine池。一旦goroutine完成工作,他们就会(通过其他渠道)发送一些结果。
代码为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package main import"fmt" import"os" import"sync" import"bufio" func worker(linkChan <-chan string, outChan chan<- string, wg *sync.WaitGroup, jobId int) { defer wg.Done() for url := range linkChan { // ... outChan <- url } } func main() { lCh := make(chan string) wg := new(sync.WaitGroup) outCh := make(chan string) urls := []string{} if len(os.Args) > 1 { for _, link := range os.Args[1:] { urls = append(urls, link) } } else { s := bufio.NewScanner(os.Stdin) for s.Scan() { urls = append(urls, s.Text()) } } num_worker := 10 for i := 0; i < num_worker; i++ { wg.Add(1) go worker(lCh, outCh, wg, i) } for _, link := range urls { lCh <- link } close(lCh) for res := range outCh { fmt.Printf("%s\ ", res) } close(outCh) wg.Wait() } |
运行
据我了解,
如何解决此僵局?
谢谢您的回答。
您需要在goroutine中泵送这些URL,否则
因此重新排列代码,使其看起来像这样
1 2 3 4 5 6 7 8 9 10 11 12 13 | go func() { for _, link := range urls { lCh <- link } close(lCh) wg.Wait() close(outCh) }() for res := range outCh { fmt.Printf("%s\ ", res) } |
它将正常工作
完整代码
https://golang.org/ref/spec#For_range:
For channels, the iteration values produced are the successive values sent on the channel until the channel is closed. If the channel is nil, the range expression blocks forever.
在关闭