Need Help: fatal error: all goroutines are asleep - deadlock! WaitGroup Error

agolangf · · 477 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi, I get this error after a few seconds. I just need that my worker can add new jobs to pool, is it posible?</p> <pre><code>package main import ( &#34;flag&#34; &#34;fmt&#34; &#34;os&#34; &#34;sync&#34; &#34;time&#34; ) type Link struct { URI string Level int } var wg sync.WaitGroup func main() { uri := flag.String(&#34;uri&#34;, &#34;http://test.com&#34;, &#34;URI to parse&#34;) workers := flag.Int(&#34;w&#34;, 2, &#34;Size workers pool&#34;) flag.Parse() links := make(chan *Link, 50) filteredLinks := make(chan *Link, 50) links &lt;- &amp;Link{URI: *uri} go filterLinks(links, filteredLinks) for i := 0; i &lt; *workers; i++ { go func() { wg.Add(1) for item := range links { fmt.Printf(&#34;%s\n&#34;, item) if item.Level &gt; 2 { break } time.Sleep(time.Second) filteredLinks &lt;- &amp;Link{URI: item.URI, Level: item.Level + 1} } wg.Done() }() } os.Stdout.Sync() wg.Wait() } func filterLinks(out chan *Link, in chan *Link) { wg.Add(1) seen := make(map[string]bool) for item := range in { if !seen[item.URI] { out &lt;- item } seen[item.URI] = true } wg.Done() } </code></pre> <p>This is what I get after compile &amp; run</p> <pre><code>&amp;{http://test.com %!s(int=0)} &amp;{http://test.com %!s(int=1)} fatal error: all goroutines are asleep - deadlock! goroutine 1 [semacquire]: sync.runtime_Semacquire(0x1172d7c) /usr/local/opt/go/libexec/src/runtime/sema.go:56 +0x39 sync.(*WaitGroup).Wait(0x1172d70) /usr/local/opt/go/libexec/src/sync/waitgroup.go:131 +0x72 main.main() /Users/thuglife/dev/parser.go:49 +0x1e3 goroutine 5 [chan receive]: main.filterLinks(0xc4200560c0, 0xc420056120) /Users/thuglife/dev/parser.go:56 +0x127 created by main.main /Users/thuglife/dev/parser.go:27 +0x179 goroutine 6 [chan receive]: main.main.func1(0xc4200560c0, 0xc420056120) /Users/thuglife/dev/parser.go:33 +0x8b created by main.main /Users/thuglife/dev/parser.go:30 +0x1ae goroutine 7 [chan receive]: main.main.func1(0xc4200560c0, 0xc420056120) /Users/thuglife/dev/parser.go:33 +0x8b created by main.main /Users/thuglife/dev/parser.go:30 +0x1ae </code></pre> <hr/>**评论:**<br/><br/>Justinsaccount: <pre><p>the deadlock is because filterLinks never finishes</p></pre>d05cfea: <pre><p>i get it. Also go func() in main never finishes too</p></pre>ImLopshire: <pre><p>The wait group in <code>filterLinks()</code> is superfluous. Ranging over a channel blocks until the channel is closed.</p></pre>ImLopshire: <pre><p>This seems like a very strange way to use a wait group. Look at this simple example of using worker pools: <a href="https://gobyexample.com/worker-pools" rel="nofollow">https://gobyexample.com/worker-pools</a></p></pre>tv64738: <pre><p>Those wg.Adds happen too late to be of any use.</p> <p>The code is very confusing, but what you seem to be trying to do is a web crawler that fetches links, parses the pages, and adds discovered links as more work to do. You can&#39;t use a channel as queue for that, because you cannot guarantee the amount of outstanding work queued stays below your limit of 50.</p></pre>_crackling: <pre><p>/Users/<strong>thuglife</strong>/dev/parser.go:30 </p> <p>haha, love it</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

477 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传