Goroutines - 将关键数据发送到单个 goroutine 并等待结果

我的应用程序中运行着许多 goroutine,并且我有另一个 goroutine 必须在同一时间段内只处理一个请求,然后将结果发送回调用方。

这意味着其他 goroutine 应该等到必要的(单操作)goroutine 忙。

[goroutine 1] <-
                 -
                   -
                     -
[goroutine 2]<- - - -  -> [Process some data in a single goroutine and send the result back to caller
                     -
                   -
                 -
[goroutine 3] <-

这是它应该是什么样子的图表

我对 Go 非常陌生,而且我对如何正确实施它知之甚少。

有人可以为我提供一些工作示例,以便我可以在 go playground 上运行它吗?


慕无忌1623718
浏览 94回答 2
2回答

元芳怎么了

这里有一个代码片段,其中包含一些 worker-goroutine 和一个 processor-goroutine。只有一个 worker-goroutine 可以向处理器发送一些东西,因为processorChannel它只允许一个条目。处理器完成后,他将响应发回给从中获得工作的工人。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time")type WorkPackage struct {&nbsp; &nbsp; value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int&nbsp; &nbsp; responseChannel chan int}func main() {&nbsp; &nbsp; processorChannel := make(chan *WorkPackage)&nbsp; &nbsp; for i := 0; i < 3; i++ {&nbsp; &nbsp; &nbsp; &nbsp; go runWorker(processorChannel)&nbsp; &nbsp; }&nbsp; &nbsp; go runProcessor(processorChannel)&nbsp; &nbsp; // Do some clever waiting here like with wait groups&nbsp; &nbsp; time.Sleep(5 * time.Second)}func runWorker(processorChannel chan *WorkPackage) {&nbsp; &nbsp; responseChannel := make(chan int)&nbsp; &nbsp; for i := 0; i < 10; i++ {&nbsp; &nbsp; &nbsp; &nbsp; processorChannel <- &WorkPackage{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; responseChannel: responseChannel,&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("** Sent %d\n", i)&nbsp; &nbsp; &nbsp; &nbsp; response := <-responseChannel&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("** Received the response %d\n", response)&nbsp; &nbsp; &nbsp; &nbsp; // Do some work&nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(300 * time.Millisecond)&nbsp; &nbsp; }}func runProcessor(processorChannel chan *WorkPackage) {&nbsp; &nbsp; for workPackage := range processorChannel {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("## Received %d\n", workPackage.value)&nbsp; &nbsp; &nbsp; &nbsp; // Do some processing work&nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(100 * time.Millisecond)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; workPackage.responseChannel <- workPackage.value * 100&nbsp; &nbsp; }}

斯蒂芬大帝

我将使用添加两个数字的 goroutine 来描述该方法。声明 goroutine 的请求和响应类型。在请求中包含一个响应值通道:type request struct {&nbsp; &nbsp; a, b&nbsp; int&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add these two numbers&nbsp; &nbsp; ch chan response}type response struct {&nbsp; &nbsp; n int&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the result of adding the numbers}启动接收请求的 goroutine,执行操作并将响应发送到请求中的通道:func startAdder() chan request {&nbsp; &nbsp; ch := make(chan request)&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; for req := range ch {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; req.ch <- response{req.a + req.b}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }()&nbsp; &nbsp; return ch}要添加数字,请使用响应通道向 goroutine 发送请求。在响应通道上接收。返回响应值。func add(ch chan request, a, b int) int {&nbsp; &nbsp; req := request{ch: make(chan response), a: a, b: b}&nbsp; &nbsp; ch <- req&nbsp; &nbsp; return (<-req.ch).n}像这样使用它:ch := startAdder()fmt.Println(add(ch, 1, 2))在 GoLang PlayGround 上运行它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go