如何等待回调函数返回?

我有以下代码:


http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

  player.Exec(command, func(response map[string]interface{}){

    if response["statusCode"]==float64(0) { // ok

      w.Write([]byte(response["statusMessage"].(string)))

      player.SendMessage("<b>"+response["statusMessage"].(string)+"</b>")

      fmt.Println("playerExec: "+time.Now().Format("20060102150405"))

    } else { // failed to process

      w.WriteHeader(http.StatusBadRequest) // 400

      w.Write([]byte(response["statusMessage"].(string)))

      player.SendMessage(response["statusMessage"].(string))

    }

  })

  // Time.Sleep(Time.Seconds*2)

  fmt.Println("cmd: "+time.Now().Format("20060102150405"))   

})

的处理player.Exec()需要一些时间(因为它启动了 WebSocket 连接),所以在一段时间后调用回调函数(见下面的证明)。太晚了,所以我看到以下错误:


http:来自 main.main.func1.1 的多余 response.WriteHeader 调用


此外,当我在浏览器中打开“/”页面时,我看不到任何内容。如果我添加Time.Sleep()到我的代码中(参见注释行),那么我会看到内容。Exec() 代码在这里。


日志显示以下内容 -


cmd: 20200612192659

playerExec: 20200612192659

有什么方法可以等待回调函数返回吗?


BIG阳
浏览 143回答 2
2回答

慕的地8271018

是的,您可以sync.WaitGroup按如下方式使用:wg := sync.WaitGroup{}http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {&nbsp; wg.Add(1)&nbsp; // add a waitgroup before calling the async function&nbsp; player.Exec(command, func(response map[string]interface{}){&nbsp; &nbsp; defer wg.Done()&nbsp; // release when this function returns&nbsp; &nbsp; if response["statusCode"]==float64(0) { // ok&nbsp; &nbsp; &nbsp; w.Write([]byte(response["statusMessage"].(string)))&nbsp; &nbsp; &nbsp; player.SendMessage("<b>"+response["statusMessage"].(string)+"</b>")&nbsp; &nbsp; &nbsp; fmt.Println("playerExec: "+time.Now().Format("20060102150405"))&nbsp; &nbsp; } else { // failed to process&nbsp; &nbsp; &nbsp; w.WriteHeader(http.StatusBadRequest) // 400&nbsp; &nbsp; &nbsp; w.Write([]byte(response["statusMessage"].(string)))&nbsp; &nbsp; &nbsp; player.SendMessage(response["statusMessage"].(string))&nbsp; &nbsp; }&nbsp; })&nbsp; wg.Wait()&nbsp; // this will block until all the resources are released&nbsp; fmt.Println("cmd: "+time.Now().Format("20060102150405"))&nbsp; &nbsp;})

偶然的你

http:来自 main.main.func1.1 的多余 response.WriteHeader 调用如果您WriteHeader在通过Write. 这就是为什么 WriteHeader 是额外的或superfluous.在等待你的 exec 完成时,你可以在你的闭包中使用 waitgroup 。wg.Wait()将等到您的 exec 回调返回http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {&nbsp; var wg sync.WaitGroup&nbsp; wg.Add(1)&nbsp; player.Exec(command, func(response map[string]interface{}){&nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; if response["statusCode"]==float64(0) { // ok&nbsp; &nbsp; &nbsp; w.Write([]byte(response["statusMessage"].(string)))&nbsp; &nbsp; &nbsp; player.SendMessage("<b>"+response["statusMessage"].(string)+"</b>")&nbsp; &nbsp; &nbsp; fmt.Println("playerExec: "+time.Now().Format("20060102150405"))&nbsp; &nbsp; } else { // failed to process&nbsp; &nbsp; &nbsp; w.WriteHeader(http.StatusBadRequest) // 400&nbsp; &nbsp; &nbsp; w.Write([]byte(response["statusMessage"].(string)))&nbsp; &nbsp; &nbsp; player.SendMessage(response["statusMessage"].(string))&nbsp; &nbsp; }&nbsp; })&nbsp; wg.Wait()&nbsp; // Time.Sleep(Time.Seconds*2)&nbsp; fmt.Println("cmd: "+time.Now().Format("20060102150405"))&nbsp; &nbsp;})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go