Golang Goroutine 与 channel 同步

标签 go

我有以下程序,其中 HTTP 服务器是使用 gorilla mux 创建的。 当任何请求到来时,它会启动 goroutine 1。在处理过程中,我正在启动另一个 goroutine 2。 我想在 goroutine 1 中等待 goroutine 2 的响应?我该怎么做? 如何保证只有goroutine 2 会响应goroutine 1?

GR3可以创建GR4,GR3只能等待GR4。

GR = 协程

enter image description here

服务器

    package main

import (
    "encoding/json"
    "fmt"
    "net/http"
    "strconv"
    "time"

    "github.com/gorilla/mux"
)

type Post struct {
    ID    string `json:"id"`
    Title string `json:"title"`
    Body  string `json:"body"`
}

var posts []Post

var i = 0

func getPosts(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    i++
    fmt.Println(i)
    ch := make(chan int)
    go getTitle(ch, i)

    p := Post{
        ID: "123",
    }
    // Wait for getTitle result and update variable P with title

    s := <-ch
    //

    p.Title = strconv.Itoa(s) + strconv.Itoa(i)
    json.NewEncoder(w).Encode(p)
}

func main() {

    router := mux.NewRouter()
    posts = append(posts, Post{ID: "1", Title: "My first post", Body: "This is the content of my first post"})
    router.HandleFunc("/posts", getPosts).Methods("GET")
    http.ListenAndServe(":9999", router)
}

func getTitle(resultCh chan int, m int) {
    time.Sleep(2 * time.Second)
    resultCh <- m
}

客户

package main

import (
  "fmt"
  "net/http"
  "io/ioutil"
    "time"
)


func main(){

for i :=0;i <100 ;i++ {

go main2()

}

    time.Sleep(200 * time.Second)

}

func main2() {

  url := "http://localhost:9999/posts"
  method := "GET"

  client := &http.Client {
  }
  req, err := http.NewRequest(method, url, nil)

  if err != nil {
    fmt.Println(err)
  }
  res, err := client.Do(req)
  defer res.Body.Close()
  body, err := ioutil.ReadAll(res.Body)

  fmt.Println(string(body))
}

实际结果

{"id":"123","title":"25115","body":""}

{"id":"123","title":"23115","body":""}

{"id":"123","title":"31115","body":""}

{"id":"123","title":"44115","body":""}

{"id":"123","title":"105115","body":""}

{"id":"123","title":"109115","body":""}

{"id":"123","title":"103115","body":""}

{"id":"123","title":"115115","body":""}

{"id":"123","title":"115115","body":""}

{"id":"123","title":"115115","body":""}

预期结果

 {"id":"123","title":"112112","body":""}
 {"id":"123","title":"113113","body":""}
 {"id":"123","title":"115115","body":""}
 {"id":"123","title":"116116","body":""}
 {"id":"123","title":"117117","body":""}

最佳答案

有几种方法可以做到这一点,一个简单的方法是使用 channel

将 getTitle 函数更改为此

func getTitle(resultCh chan string)  {
   time.Sleep(2 * time.Second)
   resultCh <- "Game Of Thrones"
}

getPosts 会像这样使用它

func getPosts(w http.ResponseWriter, r *http.Request) {
   w.Header().Set("Content-Type", "application/json")

   ch := make(chan string)
   go getTitle(ch)


   s := <-ch // this will wait until getTile inserts data to channel 
   p := Post{
       ID: s,
   }

   json.NewEncoder(w).Encode(p)
}

我怀疑你是新来的,这是一个基本的 channel 使用,在这里查看更多细节 Channels

关于Golang Goroutine 与 channel 同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60691968/

相关文章:

go - 使用 Go Reflect 调用函数

go - 无法从 Stripe 获取卡片品牌

java - Golang/Java 无法从中间件中解码 JSON

go - 如何使用 protobuf 二进制文件在 PUB/SUB ( ZeroMQ ) 中进行过滤?

go - ast.Inspect 不走 *ast.UnaryExpr

curl - NTLM 和 Golang

go - 将结构接口(interface)转换为相同的结构

go - 为什么 goroutine 饿死了?

戈朗 : Is there a way to modify one of the multi-value return parameters in one line?

go - 错误: cannot load values. yaml:将YAML转换为JSON时发生错误:yaml:第**行:找不到预期的 key