doujiabing1228 2015-01-08 13:18
浏览 34
已采纳

何时何地检查通道是否不再有任何数据?

I'm trying to solve Exercise: Web Crawler

In this exercise you'll use Go's concurrency features to parallelize a web crawler.

Modify the Crawl function to fetch URLs in parallel without fetching the same URL twice.

When should I check if all urls already been crawled? (or how could I know if there will be no more data queued?)

package main

import (
    "fmt"
)

type Result struct {
    Url string
    Depth int
}

type Stor struct {
    Queue  chan Result
    Visited map[string]int
}    

func NewStor() *Stor {
    return &Stor{
        Queue:  make(chan Result,1000),
        Visited: map[string]int{},
    }
}

type Fetcher interface {
    // Fetch returns the body of URL and
    // a slice of URLs found on that page.
    Fetch(url string) (body string, urls []string, err error)
}

// Crawl uses fetcher to recursively crawl
// pages starting with url, to a maximum of depth.
func Crawl(res Result, fetcher Fetcher, stor *Stor) {
    defer func() {          
        /*
        if len(stor.Queue) == 0 {
            close(stor.Queue)
        }   
        */  // this is wrong, it makes the channel closes too early
    }()
    if res.Depth <= 0 {
        return
    }
    // TODO: Don't fetch the same URL twice.
    url := res.Url
    stor.Visited[url]++
    if stor.Visited[url] > 1 {
        fmt.Println("skip:",stor.Visited[url],url)
        return
    }
    body, urls, err := fetcher.Fetch(url)
    if err != nil {
        fmt.Println(err)
        return
    }   
    fmt.Printf("found: %s %q
", url, body)
    for _, u := range urls {
        stor.Queue <- Result{u,res.Depth-1}
    }
    return
}

func main() {
    stor := NewStor()   
    Crawl(Result{"http://golang.org/", 4}, fetcher, stor)
    for res := range stor.Queue {
        // TODO: Fetch URLs in parallel.
        go Crawl(res,fetcher,stor)
    }
}

// fakeFetcher is Fetcher that returns canned results.
type fakeFetcher map[string]*fakeResult

type fakeResult struct {
    body string
    urls []string
}

func (f fakeFetcher) Fetch(url string) (string, []string, error) {
    if res, ok := f[url]; ok {
        return res.body, res.urls, nil
    }
    return "", nil, fmt.Errorf("not found: %s", url)
}

// fetcher is a populated fakeFetcher.
var fetcher = fakeFetcher{
    "http://golang.org/": &fakeResult{
        "The Go Programming Language",
        []string{
            "http://golang.org/pkg/",
            "http://golang.org/cmd/",
        },
    },
    "http://golang.org/pkg/": &fakeResult{
        "Packages",
        []string{
            "http://golang.org/",
            "http://golang.org/cmd/",
            "http://golang.org/pkg/fmt/",
            "http://golang.org/pkg/os/",
        },
    },
    "http://golang.org/pkg/fmt/": &fakeResult{
        "Package fmt",
        []string{
            "http://golang.org/",
            "http://golang.org/pkg/",
        },
    },
    "http://golang.org/pkg/os/": &fakeResult{
        "Package os",
        []string{
            "http://golang.org/",
            "http://golang.org/pkg/",
        },
    },
}

The output was a deadlock since the stor.Queue channel never closed.

  • 写回答

2条回答 默认 最新

  • duanji5746 2015-01-09 13:55
    关注

    Simplest way to wait until all goroutings are done is sync.WaitGroup in sync package

    package main
    import "sync"
    var wg sync.WaitGroup
    //then you do
    func Crawl(res Result, fetcher Fetcher) { //what for you pass stor *Stor as arg? It just visible for all goroutings
        defer wg.Done()
    ...
    //why not to spawn new routing just inside Crowl?
        for res := range urls {
            wg.Add(1)
            go Crawl(res,fetcher)
        }
    ...
    }
    ...
    //And in main.main()
    func main() {
        wg.Add(1) 
        Crawl(Result{"http://golang.org/", 4}, fetcher)
        ...
        wg.Wait() //Will block until all routings Done
    }
    

    Complete solution will be:

    package main
    
    import (
        "fmt"
        "sync"
    )
    var wg sync.WaitGroup
    var visited map[string]int = map[string]int{}
    
    type Result struct {
        Url string
        Depth int
    }
    
    type Fetcher interface {
        // Fetch returns the body of URL and
        // a slice of URLs found on that page.
        Fetch(url string) (body string, urls []string, err error)
    }
    
    // Crawl uses fetcher to recursively crawl
    // pages starting with url, to a maximum of depth.
    func Crawl(res Result, fetcher Fetcher) {
        defer wg.Done()
        if res.Depth <= 0 {
            return
        }
        // TODO: Don't fetch the same URL twice.
        url := res.Url
        visited[url]++
        if visited[url] > 1 {
            fmt.Println("skip:",visited[url],url)
            return
        }
        body, urls, err := fetcher.Fetch(url)
        if err != nil {
            fmt.Println(err)
            return
        }   
        fmt.Printf("found: %s %q
    ", url, body)
        for _, u := range urls {
            wg.Add(1)
            go Crawl( Result{u,res.Depth-1},fetcher)
            //stor.Queue <- Result{u,res.Depth-1}
        }
        return
    }
    
    func main() {
        wg.Add(1) 
        Crawl(Result{"http://golang.org/", 4}, fetcher)
        wg.Wait()
    }
    
    // fakeFetcher is Fetcher that returns canned results.
    type fakeFetcher map[string]*fakeResult
    
    type fakeResult struct {
        body string
        urls []string
    }
    
    func (f fakeFetcher) Fetch(url string) (string, []string, error) {
        if res, ok := f[url]; ok {
            return res.body, res.urls, nil
        }
        return "", nil, fmt.Errorf("not found: %s", url)
    }
    
    // fetcher is a populated fakeFetcher.
    var fetcher = fakeFetcher{
        "http://golang.org/": &fakeResult{
            "The Go Programming Language",
            []string{
                "http://golang.org/pkg/",
                "http://golang.org/cmd/",
            },
        },
        "http://golang.org/pkg/": &fakeResult{
            "Packages",
            []string{
                "http://golang.org/",
                "http://golang.org/cmd/",
                "http://golang.org/pkg/fmt/",
                "http://golang.org/pkg/os/",
            },
        },
        "http://golang.org/pkg/fmt/": &fakeResult{
            "Package fmt",
            []string{
                "http://golang.org/",
                "http://golang.org/pkg/",
            },
        },
        "http://golang.org/pkg/os/": &fakeResult{
            "Package os",
            []string{
                "http://golang.org/",
                "http://golang.org/pkg/",
            },
        },
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序