英文:
fatal error: all goroutines are asleep - deadlock! error GO
问题
我是你的中文翻译助手,以下是你提供的代码的翻译:
我对Go语言中的并发编程还不熟悉,所以我尝试了一个使用通道和goroutine的示例。我想要实现生产者-消费者模式。生产者函数会不断生成随机字符串,而消费者函数会将它们转换为大写。我希望它在有限的时间内运行(2秒)。
package main
import (
"fmt"
"math/rand"
"strings"
"time"
)
func producer(x []string, c chan string) {
i := 1
for i > 0 {
randomIndex := rand.Intn(len(x))
pick := x[randomIndex]
c <- pick
}
}
func consumer(x string, c chan string) {
x1 := strings.ToUpper(x)
c <- x1
}
func main() {
s := []string{"one", "two", "three", "four"}
c1 := make(chan string)
d1 := make(chan string)
go producer(s, c1)
go consumer(<-c1, d1)
stop := time.After(2000 * time.Millisecond)
for {
select {
case <-stop:
fmt.Println("STOP AFTER 2 SEC!")
return
default:
fmt.Println(<-d1)
time.Sleep(50 * time.Millisecond)
}
}
}
我只得到了一个数组元素和一些错误。需要做哪些更改才能使这个示例正常工作?
输出:
TWO
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
goroutine 6 [chan send]:
main.producer({0xc00004e040, 0x4, 0x0?}, 0x0?)
created by main.main
exit status 2
英文:
I'm new to concurrency in Go so I tried an example with channels and goroutines. I want to have the producer-consumer pattern. The producer function forever gives random strings and the consumer modifies them by making them uppercase. I want to run it for a limited time(2 sec).
package main
import (
"fmt"
"math/rand"
"strings"
"time"
)
func producer(x []string, c chan string) {
i := 1
for i > 0 {
randomIndex := rand.Intn(len(x))
pick := x[randomIndex]
c <- pick
}
}
func consumer(x string, c chan string) {
x1 := strings.ToUpper(x)
c <- x1
}
func main() {
s := []string{"one", "two", "three", "four"}
c1 := make(chan string)
d1 := make(chan string)
go producer(s, c1)
go consumer(<-c1, d1)
stop := time.After(2000 * time.Millisecond)
for {
select {
case <-stop:
fmt.Println("STOP AFTER 2 SEC!")
return
default:
fmt.Println(<-d1)
time.Sleep(50 * time.Millisecond)
}
}
}
I'm getting only one array element and some errors. What changes need to be made so this example would work?
OUTPUT:
TWO
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
goroutine 6 [chan send]:
main.producer({0xc00004e040, 0x4, 0x0?}, 0x0?)
created by main. main
exit status 2
答案1
得分: 1
你的消费者应该在一个循环中运行,这已经提到过了。
将消费者的第一个参数更改为chan string
,而不是一个字符串。这样,生产者可以持续向该通道写入数据,供消费者在另一个通道上发布,直到时间限制到期。
func consumer(consumeChan chan string, outCh chan string) {
for {
select {
case s := <-consumeChan:
s = strings.ToUpper(s)
outCh <- s
}
}
}
现在,在main
函数中,在调用go consumer()
之前,你正在等待来自生产者的c1
通道的第一个响应。而不是这样做,将c1
通道作为第一个参数传递。
func main() {
s := []string{"one", "two", "three", "four"}
c1 := make(chan string)
d1 := make(chan string)
go producer(s, c1)
go consumer(c1, d1)
stop := time.After(2000 * time.Millisecond)
for {
select {
case <-stop:
fmt.Println("STOP AFTER 2 SEC!")
return
case response := <-d1:
fmt.Println(response)
time.Sleep(50 * time.Millisecond)
}
}
}
这样应该会在c1
通道上连续写入随机数字,消费者会将文本转换为大写并在d1
通道上连续写入,直到2秒时间到。
英文:
Your consumer should run in a loop, this has been mentioned already.
Change the first parameter of the consumer so that it is a chan string
instead of a string. That way the producer can continuously write to this channel for the consumer to publish on another channel until the time limit expires.
func consumer(consumeChan chan string, outCh chan string) {
for {
select {
case s := <- consumeChan:
s = strings.ToUpper(s)
outCh <- s
}
}
}
Right now in the main func before go consumer()
is called you are waiting for the first response to the c1
channel from the producer. Instead of doing that pass in the c1
channel as the first parameter.
func main() {
s := []string{"one", "two", "three", "four"}
c1 := make(chan string)
d1 := make(chan string)
go producer(s, c1)
go consumer(c1, d1)
stop := time.After(2000 * time.Millisecond)
for {
select {
case <-stop:
fmt.Println("STOP AFTER 2 SEC!")
return
case response := <- d1:
fmt.Println(response)
time.Sleep(50 * time.Millisecond)
}
}
}
This should show you continuous writes of random numbers by the producer on the c1
channel, and continuous writes on the d1 channel by the consumer with the text in all uppercase until the 2 seconds is up.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论