亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何讓 Go channel worker 有不同的結果長度?

如何讓 Go channel worker 有不同的結果長度?

Go
撒科打諢 2023-01-03 15:47:04
我從gobyexample做了一些編輯:import (    "fmt"    "math/rand"    "time")type DemoResult struct {    Name string    Rate int}func random(min, max int) int {    rand.Seed(time.Now().UTC().UnixNano())    return rand.Intn(max-min) + min}func worker(id int, jobs <-chan int, results chan<- DemoResult) {    for j := range jobs {        fmt.Println("worker", id, "started  job", j)        time.Sleep(time.Second)        fmt.Println("worker", id, "finished job", j)        myrand := random(1, 4)        if myrand == 2 {            results <- DemoResult{Name: "succ", Rate: j}        }        //  else {        //  results <- DemoResult{Name: "failed", Rate: 999}        // }    }}func main() {    const numJobs = 5    jobs := make(chan int, numJobs)    results := make(chan DemoResult)    for w := 1; w <= 3; w++ {        go worker(w, jobs, results)    }    for j := 1; j <= numJobs; j++ {        jobs <- j    }    close(jobs)    for a := 1; a <= numJobs; a++ {        out := <-results        if out.Name == "succ" {            fmt.Printf("%v\n", out)        }    }}我評論了以下代碼,故意讓它永遠卡?。?nbsp;       //  else {        //  results <- DemoResult{Name: "failed", Rate: 999}        // }看起來我們應該使結果的長度與作業的長度相同。我想知道我們是否可以讓它有不同的長度?
查看完整描述

2 回答

?
牧羊人nacy

TA貢獻1862條經驗 獲得超7個贊

使用等待組來檢測工作人員何時完成。工作人員完成后關閉結果通道。接收結果直到通道關閉。

func worker(wg *sync.WaitGroup, id int, 

            jobs <-chan int, 

            results chan<- DemoResult) {

    // Decrement wait group counter on return from

    // function.

    defer wg.Done()

    ? 

}



func main() {

    ?

    // Declare wait group and increment counter for

    // each worker.

    var wg sync.WaitGroup

    for w := 1; w <= 3; w++ {

        wg.Add(1)

        go worker(&wg, w, jobs, results)

    }

    ?

    // Wait for workers to decrement wait group

    // counter to zero and close channel.

    // Execute in goroutine so we can continue on 

    // to receiving values from results in main. 

    go func() {

        wg.Wait()

        close(results)

    }()

    ?

    // Loop until results is closed.

    for out := range results {

       ?

    }

}

https://go.dev/play/p/FOQwybMl7tM


查看完整回答
反對 回復 2023-01-03
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

我想知道我們是否可以讓它有不同的長度?

絕對可以,但是您需要某種方法來確定何時到達結果的末尾。這就是您的示例失敗的原因 - 目前該函數假設會有numJobs(每個作業一個結果)結果并等待那么多。

另一種方法是使用通道關閉來指示這一點,即(游樂場

package main


import (

    "fmt"

    "math/rand"

    "sync"

    "time"

)


type DemoResult struct {

    Name string

    Rate int

}


func random(min, max int) int {

    rand.Seed(time.Now().UTC().UnixNano())

    return rand.Intn(max-min) + min

}


func worker(id int, jobs <-chan int, results chan<- DemoResult) {

    for j := range jobs {

        fmt.Println("worker", id, "started  job", j)

        time.Sleep(time.Second)

        fmt.Println("worker", id, "finished job", j)

        myrand := random(1, 4)

        if myrand == 2 {

            results <- DemoResult{Name: "succ", Rate: j}

        } // else {

        //  results <- DemoResult{Name: "failed", Rate: 999}

        //}

    }

}


func main() {

    const numWorkers = 3

    const numJobs = 5

    jobs := make(chan int, numJobs)

    results := make(chan DemoResult)


    var wg sync.WaitGroup

    wg.Add(numWorkers)

    for w := 1; w <= numWorkers; w++ {

        go func() {

            worker(w, jobs, results)

            wg.Done()

        }()

    }

    go func() {

        wg.Wait() // Wait for go routines to complete then close results channel

        close(results)

    }()


    for j := 1; j <= numJobs; j++ {

        jobs <- j

    }

    close(jobs)


    for out := range results {

        if out.Name == "succ" {

            fmt.Printf("%v\n", out)

        }

    }

}


查看完整回答
反對 回復 2023-01-03
  • 2 回答
  • 0 關注
  • 90 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號