我有以下代码实现了一个工作队列:
package main
import (
"fmt"
"net/http"
"io"
"time"
)
var (
linkQueue chan Link
scraperQueue chan chan Link
)
func CycleDirectory(page int) {
linkQueue <- Link{Name: "asd"}
}
type Link struct {
Name string
}
func (s Scraper) Start() {
fmt.Println("Started")
go func() {
for {
s.ScraperQueue <- s.Link
select {
case link := <-s.Link:
fmt.Println(fmt.Sprintf("%v", s.Id) + ": Received " + link.Name)
case <-s.QuitChan:
fmt.Println("Closed")
return
}
}
}()
}
func (s Scraper) Stop() {
go func() {
s.QuitChan <- true
}()
}
type Scraper struct {
Id int
Link chan Link
ScraperQueue chan chan Link
QuitChan chan bool
}
func InitScraper(id int, scraperQueue chan chan Link) Scraper {
return Scraper {
Id: id,
Link: make(chan Link),
ScraperQueue: scraperQueue,
QuitChan: make(chan bool),
}
}
func HelloServer(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "hello, world!\n")
}
func main() {
linkQueue = make(chan Link, 2000)
numScrapers := 2
scraperQueue = make(chan chan Link, numScrapers)
for i := 0; i < numScrapers; i++ {
s := InitScraper(i+1, scraperQueue)
s.Start()
}
go func() {
for {
select {
case link := <-linkQueue:
go func() {
scraper := <-scraperQueue
scraper <- link
}()
}
}
}()
CycleDirectory(1)
// time.Sleep(1 * time.Millisecond)
for {
// select {
// }
}
// http.HandleFunc("/hello", HelloServer)
// http.ListenAndServe(":12345", nil)
}
使用包含if语句的for循环(或内部没有任何内容)运行此代码,scraper不会打印收到的消息。使用net / http中的ListenAndServe函数进行阻止,它会打印收到的消息。使用睡眠阻止1 ms,我收到消息。在for循环中放入一个select语句,我也收到了消息。
为什么没有select语句的for循环不允许执行在工作队列中发送的消息,我将如何处理它。我需要在for循环中使用if语句来检查是否已完成所有工作,以便我可以退出循环并结束程序。
更新
Amd的建议是解决这个问题的方法。这是我使用sync.WaitGroup的更新代码 包主要
import (
"fmt"
"sync"
)
var (
linkQueue chan Link
scraperQueue chan chan Link
wg sync.WaitGroup
)
func CycleDirectory(page int) {
wg.Add(1)
linkQueue <- Link{Name: "asd"}
}
type Link struct {
Name string
}
func (s Scraper) Start() {
fmt.Println("Started")
go func() {
for {
s.ScraperQueue <- s.Link
select {
case link := <-s.Link:
Scrape(s.Id, link.Name)
s.Stop()
case <-s.QuitChan:
fmt.Println("Closed")
wg.Done()
return
}
}
}()
}
func (s Scraper) Stop() {
go func() {
s.QuitChan <- true
}()
}
type Scraper struct {
Id int
Link chan Link
ScraperQueue chan chan Link
QuitChan chan bool
}
func Scrape(id int, name string) {
fmt.Println(fmt.Sprintf("%v", id) + ": Received " + name)
}
func InitScraper(id int, scraperQueue chan chan Link) Scraper {
return Scraper {
Id: id,
Link: make(chan Link),
ScraperQueue: scraperQueue,
QuitChan: make(chan bool),
}
}
func main() {
linkQueue = make(chan Link, 2000)
numScrapers := 2
scraperQueue = make(chan chan Link, numScrapers)
for i := 0; i < numScrapers; i++ {
s := InitScraper(i+1, scraperQueue)
s.Start()
}
go func() {
for {
select {
case link := <-linkQueue:
go func() {
scraper := <-scraperQueue
scraper <- link
}()
}
}
}()
CycleDirectory(1)
wg.Wait()
fmt.Println("Done")
}
答案 0 :(得分:0)
您可以使用sync.WaitGroup
停止程序退出,直到完成所有工作
试试The Go Playground:
package main
import (
"fmt"
"sync"
"time"
)
var (
wg sync.WaitGroup
)
func main() {
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(2 * time.Second)
}()
fmt.Println("Wait...")
wg.Wait()
fmt.Println("Done.")
}