进行例程并取决于功能

  • 本文关键字:取决于 功能 例程 go
  • 更新时间 :
  • 英文 :


在那里我很开心,对我想实现的目标感到非常好奇。我在这里有一个包裹,只是从reddit那里得到了一个供稿。当我收到父json文件时,我想检索子女数据。如果您看到下面的代码,我会启动一系列的goroutines,然后将其阻止,等待使用sync软件包完成。我想要的是曾经是使用先前结果的第一个goroutines系列goroutines。我在想一些循环和开关语句。但是,这样做的最好,最有效的方法

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 manditoryn")
    }
    // 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("%snn", commentFeed.Kind)
    }
    waitGroup.Wait()
    close(results)
    return nil, nil
}

如果要等待所有第一个系列完成,那么您可以将指针传递给WaitGroup,等待呼叫所有第一个系列功能(这将调用Done()在WaitGroup上(,然后开始第二系列。这是一个可运行的注释代码示例:

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 %dn", 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

最新更新