【发布时间】:2018-08-12 15:29:45
【问题描述】:
我正在学习 GOLANG,尤其是它的并发能力。
已尝试进一步开发其中一个 worker_pool 示例,以便每个工作人员接收一个作业 ID 和一个作业负载,以作业的随机持续时间表示。
time.sleep 命令使用持续时间来等待分配的纳秒数,这是随机计算的。
代码看起来像这样...
//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
}
}
我不断收到此错误消息...
prog.go:18:33: 无法将负载(类型
prog.go:36:18: 调用 worker 时参数不足 有 (int, chan int, chan int) 想要 (int,
prog.go:45:15: 不能在发送中使用负载(int64 类型)作为 int 类型
我做错了什么?
【问题讨论】:
-
您正在使用来自
loads通道的值,就好像它们是整数一样,但它们实际上是time.Duration类型。使用Nanoseconds()方法获取一个float64 的duration,然后将其转换为整数。您也没有在go worker(w, jobs, results)调用中传递足够的参数。 -
@teolandon:类型
time.Duration是一个整数:type Duration int64。您的评论没有意义。 -
@peterSO 你是对的,它的 int64 整数表示只是纳秒计数,所以我所说的都是多余的。第 18 行的真正问题是通道被当作整数使用。
标签: function go concurrency duration channels