Program snippet is as follows:
runtime.GOMAXPROCS(runtime.NumCPU())
//maxPage := getMaxPage()
maxPage := 10
if maxPage > 0 {
jobs := make(chan int, maxPage)
results := make(chan []map[string]string, maxPage)
// jobsresults
for w := 1; w <= runtime.NumCPU(); wPP {
go worker(w, jobs, results)
}
db, err := mysqlUtil.CreateReadDb()
if err != nil {
log.Fatal("connect db err ", err)
fmt.Println("connect db err ", err)
}
//
for j := 1; j <= maxPage; jPP {
jobs <- j
}
close(jobs)
for data := range results {
//
}
close(results)
fmt.Println("all done...")
}
The woker function is as follows:
func worker(id int, jobs <-chan int, results chan<- []map[string]string) {
for j := range jobs {
url := host + "/type/1/" + strconv.Itoa(j) + ".html"
fmt.Println()
data := getData(url)
fmt.Printf("worker %d finished job %d", id, j)
fmt.Println()
results <- data
}
}
the problem now is that the program is in
for data := range results {
//
}
this place is blocked, the job is completed, the data after the job is completed are put into the result, and the operation of writing the library is completed above, so why is it blocked there and not executed further?