- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在研究 Go 的一些并发模式。我查看了使用 goroutine 和输入/输出 channel 实现后台工作程序,并注意到当我将新作业发送到接收 channel (本质上是将新作业排队)时,我必须在 goroutine 中进行,否则调度会被搞砸。含义:
for _, jobData := range(dataSet) {
input <- jobData
}
go func() {
for _, jobData := range(dataSet) {
input <- jobData
}
}()
为了更具体一些,我玩了一些无意义的代码(here it is in go playground):
package main
import (
"log"
"runtime"
)
func doWork(data int) (result int) {
// ... some 'heavy' computation
result = data * data
return
}
// do the processing of the input and return
// results on the output channel
func Worker(input, output chan int) {
for data := range input {
output <- doWork(data)
}
}
func ScheduleWorkers() {
input, output := make(chan int), make(chan int)
for i := 0 ; i < runtime.NumCPU() ; i++ {
go Worker(input, output)
}
numJobs := 20
// THIS DOESN'T WORK
// and crashes the program
/*
for i := 0 ; i < numJobs ; i++ {
input <- i
}
*/
// THIS DOES
go func() {
for i := 0 ; i < numJobs ; i++ {
input <- i
}
}()
results := []int{}
for i := 0 ; i < numJobs ; i++ {
// read off results
result := <-output
results = append(results, result)
// do stuff...
}
log.Printf("Result: %#v\n", results)
}
func main() {
ScheduleWorkers()
}
我正在努力解决这个细微差别 - 感谢您的帮助。谢谢。
最佳答案
你的 ScheduleWorks
函数在主 goroutine(即运行 main()
函数的 goroutine,程序在其中启动)中通过 input
发送一个值。 . Worker
接收它,并通过 output
发送另一个值.但是没有人收到来自 output
的消息那时,程序无法继续,主 goroutine 将下一个值发送给另一个 Worker
.
对每个 worker 重复这个推理。你有 runtime.NumCPU()
worker ,这可能小于 numJobs
.假设runtime.NumCPU() == 4
,所以你有 4 个 worker 。最后,您成功发送了 4 个值,每个值都是一对一 Worker
.因为没有人在阅读 output
,所有 Worker 都忙于尝试发送,因此他们无法通过 input
接受更多数据, 所以第五个 input <- i
会挂。此时每个 goroutine 都在等待;这就是僵局。
您会注意到,如果您启动 20 个或更多 Worker 而不是 runtime.NumCPU()
,程序有效。那是因为主 goroutine 可以通过 input
发送它想要的一切。 ,因为有足够的 worker 来接收它们。
如果您不使用所有这些,而是将 input <- i
在另一个 goroutine 中循环,就像在您的成功示例中一样,main
goroutine(ScheduleWorks
在其中运行)可以继续并从 output
开始读取.因此,每次这个新的 goroutine 发送一个值时,worker 都会通过 output
发送另一个值。 ,主 goroutine 得到这个输出,worker 可以接收另一个值。没有人等待,程序成功。
关于design-patterns - Go - 为什么调度 goroutine background workers 也需要自己的 goroutine?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22606887/
发件人:http://blog.nindalf.com/how-goroutines-work/ As the goroutines are scheduled cooperatively, a go
很多时候在用 Go 开发 http 服务器时,我都会遇到这种困境。 假设我想尽快用http statuscode 200响应客户端(然后在后面执行工作),这就是我通常这样做的原因: 我让我的主要 ht
这是代码: import "fmt" func main() { messages := make(chan string, 1) go func(c chan string) {
我正在学习 Golang,但遇到了一些困难。我已经研究过 Google,但没有任何进展。 我编写了一个代码,通过多台服务器的 ICMP 检查 RTT。 它有这样的结构: type Server str
我想运行多个 goroutine,进行一些处理,将结果放入 channel ,当至少有一个 goroutine 完成时,完成所有其他 goroutine 并从 channel 返回结果。 所以,我尝试
我有两个(但以后我会是三个)go 例程来处理来自远程服务器(来自 ampq channel )的传入消息。但是因为它们正在处理相同的数据/状态,所以我想阻止所有其他 go 例程,除了正在运行的例程。
我有一个案例,我从 2 个不同的位置(ES 和 REDIS)读取数据,我需要从最快的源读取一个值,因此我触发了 2 个 goroutines,一个从 ES 获取数据,其他从REDIS获取。 一旦从其中
像这里一样,我创建了一个 go playground 示例:sGgxEh40ev ,但无法正常工作。 quit := make(chan bool) res := make(chan int) go
我是golang的新手,正在研究goroutine。 我写了一个简单的代码,故意使用 goroutine 来划分数字。 首先,我给出基数并继续除它的数,直到它不能被整除 但是,我改变了go split
Main { go routine_1(carryout a time consuming task and return output) go routine_2(wait for output f
我想知道从另一个 goroutine 返回时调用的 goroutine 会发生什么。他们是继续运行还是被终止?这是一个示例代码来说明我的意思: func func() { // Doing s
更具体地说,在我的例子中,我有一个网络服务器和一个全局可访问的结构,网络服务器使用它来生成页面。我有另一个 Goroutine,它总是定期用新值更新该结构。这会引起问题吗?我是否需要实现一种机制来确保
来自 this file ,我不明白为什么函数startWorker会这样写: func (p *WorkerPool) dispatch() { for i := 0; i < p.maxW
我正在学习围棋,但在使用 goroutines 时遇到了问题。这是我的代码 package main import ( "fmt" "sync" "time" ) var co
我收到以下错误,我不明白为什么: 发送:查询 Herefatal 错误:所有 goroutines 都睡着了 - 死锁! 您可以看到我正在调用我使用 goroutine 创建的函数 routine。我
大家好,我正在从 Python3 过渡到 Go,所以我正在尝试重写我创建的库以获得更好的性能。 我面临一个问题,因为我是 Golang XD 中的新手,我使用有限的 API 下载数百个 json,我想
我有以下格式的脚本部分: func main() { for i=0;i<1000000;i++ { go test() } } func test() { a := test
package main func main() { c:=make(chan int) for i:=0; i<=100;i++ {
我正在学习 Go,我的第一个项目是一个简单的 ping 脚本。本质上,我想 ping 一堆 url,并在每个响应时等待 XXX 秒,然后再次 ping。这是删减的代码: func mai
这个问题在这里已经有了答案: Go all goroutines are asleep deadlock (2 个回答) fatal error: all goroutines are asleep
我是一名优秀的程序员,十分优秀!