gpt4 book ai didi

Golang Goroutine 与 channel 同步

转载 作者:行者123 更新时间:2023-12-03 14:49:07 24 4
gpt4 key购买 nike

我有以下程序,其中 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com