GO程序卡在一个循环中

时间:2017-09-13 00:42:50

标签: go

// _Closing_ a channel indicates that no more values
// will be sent on it. This can be useful to communicate
// completion to the channel's receivers.

package main

import "fmt"

// In this example we'll use a `jobs` channel to
// communicate work to be done from the `main()` goroutine
// to a worker goroutine. When we have no more jobs for
// the worker we'll `close` the `jobs` channel.
func main() {
    jobs := make(chan int, 5)
    done := make(chan bool)

    // Here's the worker goroutine. It repeatedly receives
    // from `jobs` with `j, more := <-jobs`. In this
    // special 2-value form of receive, the `more` value
    // will be `false` if `jobs` has been `close`d and all
    // values in the channel have already been received.
    // We use this to notify on `done` when we've worked
    // all our jobs.

    for i := 1; i <= 3; i++ {
        go func() {
            for {
                j, more := <-jobs
                if more {
                    fmt.Println("received job", j)
                } else {
                    fmt.Println("received all jobs")
                    done <- true
                    return
                }
            }
        }()
    }

    // This sends 3 jobs to the worker over the `jobs`
    // channel, then closes it.
    j := 0
    for {
        j++
        jobs <- j
        fmt.Println("sent job", j)
    }
    close(jobs)
    fmt.Println("sent all jobs")

    // We await the worker using the
    // [synchronization](channel-synchronization) approach
    // we saw earlier.
    <-done
}

https://play.golang.org/p/x28R_g8ftS

我要做的是从分页的url端点获取所有响应。作业是存储页码的通道。我有一个函数,如果更多{}检查空响应,我有

done <- true
return

我认为这会关闭常规程序。 但是,页面生成器for{j++; jobs <- j}导致它陷入循环。知道如何解决这个问题吗?

3 个答案:

答案 0 :(得分:2)

根据定义,没有条件的for循环是无限循环。除非你用一些逻辑来打破这种无限循环,否则你永远不会摆脱它。

在您的游乐场,您的评论意味着您要发送3个职位。您应该相应地更改for循环:

for j := 0; j < 3; j++ {
  jobs <- j
  fmt.Println("sent job", j)
}

答案 1 :(得分:1)

这是一个工人的简化版本..它对生产级别的流量不是很有用,但应该作为一个简单的例子,有很多它们: - )

package main

import (
    "log"
    "sync"
)

type worker struct {
    jobs chan int
    wg   *sync.WaitGroup
}

func main() {
    w := worker{
        jobs: make(chan int, 5), // I only want to work on 5 jobs at any given time
        wg:   new(sync.WaitGroup),
    }

    for i := 0; i < 3; i++ {
        w.wg.Add(1)
        go func(i int) {
            defer w.wg.Done()
            w.jobs <- i
        }(i)

    }

    // wait in the background so that i can move to line 34 and start consuming my job queue
    go func() {
        w.wg.Wait()
        close(w.jobs)
    }()

    for job := range w.jobs {
        log.Println("Got job, I should do something with it", job)
    }

}

答案 2 :(得分:0)

这是我在寻找的。我在无限循环中有一个数字生成器。并且程序在某些条件下退出,在这个例子中,它在j值上,但它也可以是其他东西。

https://play.golang.org/p/Ud4etTjrmx

package main

import "fmt"



func jobs(job chan int) {
    i := 1
    for {
        job <- i
        i++
    }
}

func main() {
    jobsChan := make(chan int, 5)


    done := false
    j := 0

    go jobs(jobsChan)
    for !done {
        j = <-jobsChan
        if j < 20 {
            fmt.Printf("job %d\n", j)
        } else {
            done = true
        }
    }
}