英文:
Go Worker Pool doesn't seem to be processing Concurrently
问题
你好,我是你的中文翻译助手。以下是你要翻译的内容:
你好,我是全新接触Go语言(以及并发编程)的初学者,正在尝试将一个耗时的计算分发给一个工作池。
http://play.golang.org/p/lTv4Tm75A4
func main() {
test := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
answer := getSmallestMultiple(test)
fmt.Println(answer)
}
我试图找到一个能够被test
中所有数字整除的最小数。
我创建了一个工作池,并将值发送给它们,直到其中一个goroutine找到一个能够被test
中所有数字整除的数。
for w := 0; w < 100; w++ {
go divisibleByAllNumbers(&numbers, jobs, answer)
}
go func() {
for i := max; ; i += max {
fmt.Printf("Sending # %d\n", i)
jobs <- i
}
}()
尽管我启动了多少个工作线程,程序似乎运行速度都是一样的。我尝试了许多不同数量的工作线程,但运行所需的时间总是相同的,这似乎意味着工作根本没有并发执行。
每个工作线程都使用range
从队列中获取任务:
for j := range jobs {}
我希望越多的进程从jobs
通道中获取任务,程序执行得越快。
我还尝试了不同的jobs := make(chan int)
缓冲区值。
我整天都在盯着这个问题,希望有人能看出问题所在。我本来期望添加更多的工作线程会加快计算速度,但实际上并没有这样的效果。我肯定是漏掉了一些关键概念。
谢谢!
英文:
Hello I'm brand new to go (and concurrent programming in general :() and trying to distribute a slow computation to a pool of workers.
http://play.golang.org/p/lTv4Tm75A4
func main() {
test := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
answer := getSmallestMultiple(test)
fmt.Println(answer)
}
I am trying to find the smallest number that is evenly divisible by all the numbers in test
.
I have created a pool of workers and am sending them values until one of the goroutines finds a number that can be evenly divided by all the numbers in test
for w := 0; w < 100; w++ {
go divisibleByAllNumbers(&numbers, jobs, answer)
}
go func() {
for i := max; ; i += max {
fmt.Printf("Sending # %d\n", i)
jobs <- i
}
}()
The program seems to be running at the same speed despite how many workers I start. I have tried many number of workers and it always takes the same number of seconds to run, which seems like the work is not being done concurrently at all.
Each worker is consuming work from the queue using range
:
for j := range jobs {}
And i was hoping the more processes consuming off the jobs
channel the faster the program would execute.
I have also tried different values for the jobs := make(chan int)
buffer value
I have stared at this all day and was hoping someone could see what the issue is. I would expect the more workers I add the faster the computation takes but am not experiencing that. I'm sure I"m missing some key concepts,
Thank you
答案1
得分: 1
当前的Go运行时实现默认不会并行化这段代码。它只为用户级处理分配一个单独的核心。任意数量的goroutine可以在系统调用中被阻塞,但默认情况下,只能同时执行一个用户级代码。它应该更加智能,将来也会更加智能,但在它变得更智能之前,如果你想要CPU并行性,你必须告诉运行时你想要同时执行多少个goroutine的代码。有两种相关的方法可以做到这一点。要么在运行作业时将环境变量GOMAXPROCS设置为要使用的核心数,要么导入runtime包并调用runtime.GOMAXPROCS(NCPU)。一个有用的值可能是runtime.NumCPU(),它报告了本地机器上的逻辑CPU数量。再次强调,随着调度和运行时的改进,这个要求有望被废除。
英文:
http://golang.org/doc/effective_go.html#parallel
The current implementation of the Go runtime will not parallelize this code by default. It dedicates only a single core to user-level processing. An arbitrary number of goroutines can be blocked in system calls, but by default only one can be executing user-level code at any time. It should be smarter and one day it will be smarter, but until it is if you want CPU parallelism you must tell the run-time how many goroutines you want executing code simultaneously. There are two related ways to do this. Either run your job with environment variable GOMAXPROCS set to the number of cores to use or import the runtime package and call runtime.GOMAXPROCS(NCPU). A helpful value might be runtime.NumCPU(), which reports the number of logical CPUs on the local machine. Again, this requirement is expected to be retired as the scheduling and run-time improve.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论