duanli6834 2015-08-07 11:01
浏览 18
已采纳

理解代码:通过交流共享资源

I've been trying to understand the code in https://golang.org/doc/codewalk/sharemem/ Though I get most of the part about passing resources via channel, I'm unable to understand the infinite loop the program runs in. How does the program execute Poller function infinitely when the "in" channel in poller function(that receives from main function) run only 3 poller go routines ?

I get the idea of StateMonitor having anonymous go function with infinite loop. But it can't update LogState without receiving from Poller function. I'm assuming the program performs Get request to the urls infinitely.

To confirm that I'm not wrong about what I've understood, I tested the program by turning the wifi on and off to see if the log changes. To my surprise, it does, for few iterations but after that it stops responding to my change and keeps on showing the same log. So, does that mean program is buggy? Or is it that I haven't understood some fundamental concepts?

  • 写回答

3条回答 默认 最新

  • dtr87341 2015-08-07 11:29
    关注

    How does the program execute Poller function infinitely when the "in" channel in poller function(that receives from main function) run only 3 poller go routines ?

    So, first the program creates two Pollers:

    for i := 0; i < numPollers; i++ {
        go Poller(pending, complete, status)
    }
    

    then it sends three resources to pending:

    for _, url := range urls {
        pending <- &Resource{url: url}
    }
    

    Every Poller reads from pending and polls the resource:

    for r := range in {
        s := r.Poll()
        status <- State{r.url, s}
        out <- r
    }
    

    This code seems to be executed infinitely, but it is blocking read from a queue, generally. So this loops wait for the next value to appear.

    Let's virtually step over it:

    1. There is two Pollers reading resources.
    2. Program sends first resource to the queue.
    3. One of the pollers gets the resource and start to pool. Another one waits.
    4. At some moment the program sends new resource to the queue.
    5. As the first poller is busy, the second one gets unblocked and start polling.
    6. Program sends third resource and blocks as two pollers are busy.
    7. When one of the pollers completes, it takes the last resource and continues.
    8. At the meantime, the main program reads values from the complete queue.

    for r := range in { s := r.Poll() status <- State{r.url, s} out <- r } How does this code run infinitely? If it loops over "in" channel, and "in" gets its resources from pending queue, it should terminate after few iterations. I think this is exactly the part that I don't understand.

    To be precise, in does not gets the resources from pending queue. in is pending queue. The queue (or channel, which I use interchangeably) can be closed by calling close but until it is not closed explicitly it is considered alive. Any reading from it blocks the current goroutine until the next value will be given. Then gorotine resumes.

    I suppose that you keep thinking about channels like they are array with fixed number of elements. They are not. Consider they like array with infinite number of elements but with blocking read that may throw exception (that the rough approximation of closing the queue if you are not familiar with the concept).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图