2016-09-22 112 views
0

我有以下的代码实现了一个工作队列:Goroutines被for循环封锁?

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语句(或里面什么都没有),刮板不打印收到的消息。使用net/http中的ListenAndServe函数进行阻止,它将打印接收到的消息。阻塞使用睡眠1毫秒,我收到消息。并在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") 
} 
+2

空'为{}'环路使用一个CPU核心的100%。这完全不是好的做法。 – 2016-09-22 14:49:14

+1

繁忙的循环最终会阻止你的整个程序。 (当你只有一个案例时,你也不需要选择) – JimB

+0

@Amd我需要停止程序退出,直到完成所有工作。没有for循环可以做到这一点。 for循环将不断检查,直到工作完成。 –

回答

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.") 
}