How do you teardown late finishers?

polaris · · 455 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><a href="https://www.youtube.com/watch?v=cN_DpYBzKso&amp;feature=youtu.be&amp;t=1767" rel="nofollow">https://www.youtube.com/watch?v=cN_DpYBzKso&amp;feature=youtu.be&amp;t=1767</a></p> <p>This is towards the end of Rob&#39;s &#34;Concurrency Is Not Parallelism&#34; talk where he talks about sending a request to a bunch of servers at once and taking the first result that comes in. He leaves the teardown of late finishers as an exercise. I&#39;ve been looking but haven&#39;t found anything that explains this in more detail...the only thing I can think of would be to close the channel but that creates problems (sending on a closed channel errors) and my understanding is that the channels will close automagically in go. Can someone help me understand what should be done to teardown the late finishers?</p> <p>EDIT: A specific example: <a href="https://www.reddit.com/r/golang/comments/4n08z8/program_your_next_server_in_go/d40b81w" rel="nofollow">https://www.reddit.com/r/golang/comments/4n08z8/program_your_next_server_in_go/d40b81w</a></p> <hr/>**评论:**<br/><br/>Redundancy_: <pre><p>I typically do it with a readable &#34;cancel&#34; channel. Instead of just sending to your result channel, select and read the cancel channel and send to the result channel. Close the cancel channel after you have your first result, and the workers will an read an empty value from that.</p> <p>You can either use a waitgroup to let them all finish, then close the result channel, or just leave the result channel to be garbage collected when all the goroutines that used it are gone (iirc)</p> <p>Something like this: <a href="https://gist.github.com/Redundancy/93ff15ba47dbc110975e2f2bc26e8ef3" rel="nofollow">https://gist.github.com/Redundancy/93ff15ba47dbc110975e2f2bc26e8ef3</a></p></pre>sybrandy: <pre><p>Speaking from experience, if you&#39;re getting &#34;sending on closed channel errors&#34;, you&#39;re doing it wrong. Trust me. I&#39;ve done it. You want to only close channels on the sender side. On the receiver side, if you use a for loop to read from a channel (using range), it will automatically stop reading from the channel. In other cases, you can do something like this:</p> <pre><code>value, ok := &lt;- someChan if !ok { // Exit goroutine } </code></pre> <p>(I hope I got the syntax right) In this case, you can detect when the channel closes.</p> <p>As for channels closing automagically, unless I&#39;m missing something, no, they don&#39;t. You have to explicitly close the channel.</p> <p>Now, if you want goroutines to finish before you exit, you&#39;ll want to look into sync.WaitGroup. That can be used to track how many goroutines are still running so that you don&#39;t exit before they are finished.</p> <p>I know this isn&#39;t a lot of detail, but I hope it helps.</p></pre>madman2233: <pre><p>Awesome! I use item, ok := map[key] to check if an item in a map exists, but i didn&#39;t know it worked on channels too. Thanks for the tip!</p></pre>patrickdlogan: <pre><p>If you are looking to cancel an http request, there is a Cancel channel on the Request struct. When one of the requests wins, send to the Cancel channels on each of the other outstanding requests. For example, cancelling a single request... <a href="https://bitbucket.org/snippets/patrickdlogan/G6BMb" rel="nofollow">https://bitbucket.org/snippets/patrickdlogan/G6BMb</a></p></pre>

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

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