去常规和依赖功能

时间:2018-02-17 16:11:42

标签: go

在那里我和GO有一些乐趣,我对我想要达到的目标非常好奇。我这里有一个包,只是从Reddit得到一个特殊的饲料。当我收到父JSON文件时,我想要检索子数据。如果您看到下面的代码,我会启动一系列goroutines然后阻止,等待它们使用sync包完成。我想要的是,一旦第一系列goroutines使用之前的结果完成第二系列goroutines。有一些我正在考虑如loop和switch语句。但是,最好和最有效的方法是什么

func (m redditMatcher) retrieve(dataPoint *collect.DataPoint) (*redditCommentsDocument, error) {
    if dataPoint.URI == "" {
        return nil, errors.New("No datapoint uri provided")
    }

    // Get options data -> returns empty struct
    // if no options are present
    options := m.options(dataPoint.Options)
    if len(options.subreddit) <= 0 {
        return nil, fmt.Errorf("Matcher fail: Reddit - Subreddit option manditory\n")
    }

    // Create an buffered channel to receive match results to display.
    results := make(chan *redditCommentsDocument, len(options.subreddit))

    // Generte requests for each subreddit produced using
    // goroutines concurency model
    for _, s := range options.subreddit {
        // Set the number of goroutines we need to wait for while
        // they process the individual subreddit.
        waitGroup.Add(1)
        go retrieveComment(s.(string), dataPoint.URI, results)
    }

    // Launch a goroutine to monitor when all the work is done.
    waitGroup.Wait()

    // HERE I WOULD TO CALL ANOTHER SERIES OFF GOROUTINES
    for commentFeed := range results {
        // HERE I WOULD LIKE TO CALL GO ROUTINES USING THE RESULTS
        // PROVIDED FROM THE PREVIOUS FUNCTIONS
        waitGroup.Add(1)
        log.Printf("%s\n\n", commentFeed.Kind)
    }

    waitGroup.Wait()
    close(results)

    return nil, nil
}

1 个答案:

答案 0 :(得分:1)

如果你想等待所有第一个系列完成,那么你可以直接传入一个指向你的waitgroup的指针,在调用所有第一个系列函数之后等待(在waitgroup上调用Done()) ,然后开始第二个系列。这是一个可运行的带注释的代码示例:

package main

import(
    "fmt"
    "sync"
    "time"
)

func first(wg *sync.WaitGroup) {
    defer wg.Done()
    fmt.Println("Starting a first")
    // do some stuff... here's a sleep to make some time pass
    time.Sleep(250 * time.Millisecond)
    fmt.Println("Done with a first")
}

func second(wg *sync.WaitGroup) {
    defer wg.Done()
    fmt.Println("Starting a second")
    // do some followup stuff
    time.Sleep(50 * time.Millisecond)
    fmt.Println("Done with a second")
}

func main() {
    wg := new(sync.WaitGroup) // you'll need a pointer to avoid a copy when passing as parameter to goroutine function

    // let's start 5 firsts and then wait for them to finish
    wg.Add(5)
    go first(wg)
    go first(wg)
    go first(wg)
    go first(wg)
    go first(wg)
    wg.Wait()

    // now that we're done with all the firsts, let's do the seconds
    // how about two of these
    wg.Add(2)
    go second(wg)
    go second(wg)
    wg.Wait()

    fmt.Println("All done")
}

输出:

Starting a first
Starting a first
Starting a first
Starting a first
Starting a first
Done with a first
Done with a first
Done with a first
Done with a first
Done with a first
Starting a second
Starting a second
Done with a second
Done with a second
All done

但是如果你想在“第一次”完成后立即启动“秒”,那么在第一次运行时,只需要在通道上执行阻止接收运算符的秒数:

package main

import(
    "fmt"
    "math/rand"
    "sync"
    "time"
)

func first(res chan int, wg *sync.WaitGroup) {
    defer wg.Done()
    fmt.Println("Starting a first")
    // do some stuff... here's a sleep to make some time pass
    time.Sleep(250 * time.Millisecond)
    fmt.Println("Done with a first")
    res <- rand.Int() // this will block until a second is ready
}

func second(res chan int, wg *sync.WaitGroup) {
    defer wg.Done()
    fmt.Println("Wait for a value from first")
    val := <-res // this will block until a first is ready
    fmt.Printf("Starting a second with val %d\n", val)
    // do some followup stuff
    time.Sleep(50 * time.Millisecond)
    fmt.Println("Done with a second")
}

func main() {
    wg := new(sync.WaitGroup) // you'll need a pointer to avoid a copy when passing as parameter to goroutine function
    ch := make(chan int)

    // lets run first twice, and second once for each first result, for a total of four workers:
    wg.Add(4)
    go first(ch, wg)
    go first(ch, wg)
    // don't wait before starting the seconds
    go second(ch, wg)
    go second(ch, wg)
    wg.Wait()

    fmt.Println("All done")
}

哪个输出:

Wait for a value from first
Starting a first
Starting a first
Wait for a value from first
Done with a first
Starting a second with val 5577006791947779410
Done with a first
Starting a second with val 8674665223082153551
Done with a second
Done with a second
All done