douren1891 2018-03-05 02:25
浏览 108

如何将time.Duration类型传递给go函数?

I am learning GOLANG, in particular for its concurrency capabilities.

Have tried to further develop one of the worker_pool examples so that each worker receives a job id and a job load, represented in a random duration of the job.

The duration is used by a time.sleep command to wait the alloted number of nano seconds, which are calculated randomly.

Code looks like this...

//worker_pool improved example

package main

import "fmt"
import "time"
import "math/rand"

// Here's the worker, of which we'll run several
// concurrent instances. These workers will receive
// work on the `jobs` channel and send the corresponding
// results on `results`. We'll sleep a random number of seconds between
// 1 and 5 to simulate an expensive task.
func worker(id int, jobs <-chan int, loads <-chan time.Duration, results chan<- int) {
   for j := range jobs {
        fmt.Println("worker", id, "started  job", j, time.Now())
        time.Sleep(loads*time.Second)  
        fmt.Println("worker", id, "finished job", j, time.Now())
        results <- j * 2
    }
}

func main() {

    // In order to use our pool of workers we need to send
    // them work and collect their results. We make 2
    // channels for this.
    jobs := make(chan int)
    loads := make(chan time.Duration)
    results := make(chan int)

    // This starts up 3 workers, initially blocked
    // because there are no jobs yet.
    for w := 1; w <= 3; w++ {
        go worker(w, jobs, results)
    }

    // Here we send 24 `jobs` and then `close` that
    // channel to indicate that's all the work we have.
    for j := 1; j <= 24; j++ {
        jobs <- j
        r := rand.New(rand.NewSource(99))
        load := r.Int63n(5000000)
        loads <- load
    }
    close(jobs)
    close(loads)


    // Finally we collect all the results of the work.
    for a := 1; a <= 24; a++ {
        <-results
    }
}

I keep getting this error messages...

prog.go:18:33: cannot convert loads (type <-chan int) to type time.Duration

prog.go:36:18: not enough arguments in call to worker have (int, chan int, chan int) want (int, <-chan int, <-chan int, chan<- int)

prog.go:45:15: cannot use load (type int64) as type int in send

What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • dsfsdf7852 2018-03-05 02:35
    关注

    Even if you fix your compile errors, you still have problems.

    //worker_pool improved example
    
    package main
    
    import "fmt"
    import "time"
    import "math/rand"
    
    // Here's the worker, of which we'll run several
    // concurrent instances. These workers will receive
    // work on the `jobs` channel and send the corresponding
    // results on `results`. We'll sleep a random number of seconds between
    // 1 and 5 to simulate an expensive task.
    func worker(id int, jobs <-chan int, loads <-chan time.Duration, results chan<- int) {
        for j := range jobs {
            fmt.Println("worker", id, "started  job", j, time.Now())
            time.Sleep(<-loads * time.Second)
            fmt.Println("worker", id, "finished job", j, time.Now())
            results <- j * 2
        }
    }
    
    func main() {
    
        // In order to use our pool of workers we need to send
        // them work and collect their results. We make 2
        // channels for this.
        jobs := make(chan int)
        loads := make(chan time.Duration)
        results := make(chan int)
    
        // This starts up 3 workers, initially blocked
        // because there are no jobs yet.
        for w := 1; w <= 3; w++ {
            go worker(w, jobs, loads, results)
        }
    
        // Here we send 24 `jobs` and then `close` that
        // channel to indicate that's all the work we have.
        for j := 1; j <= 24; j++ {
            jobs <- j
            r := rand.New(rand.NewSource(99))
            load := time.Duration(r.Int63n(5000000))
            loads <- load
        }
        close(jobs)
        close(loads)
    
        // Finally we collect all the results of the work.
        for a := 1; a <= 24; a++ {
            <-results
        }
    }
    

    Playground: https://play.golang.org/p/tVdlKFHunKN

    Output:

    worker 3 started  job 1 2009-11-10 23:00:00 +0000 UTC m=+0.000000001
    worker 1 started  job 2 2009-11-10 23:00:00 +0000 UTC m=+0.000000001
    worker 2 started  job 3 2009-11-10 23:00:00 +0000 UTC m=+0.000000001
    worker 1 finished job 2 2009-12-27 17:05:41 +0000 UTC m=+4039541.000000001
    worker 3 finished job 1 2009-12-27 17:05:41 +0000 UTC m=+4039541.000000001
    worker 2 finished job 3 2009-12-27 17:05:41 +0000 UTC m=+4039541.000000001
    fatal error: all goroutines are asleep - deadlock!
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并