duanlie7447 2016-07-11 03:58
浏览 29
已采纳

Golang在尝试使方法并发时没有任何回报

I have a method that returns Json from the database and it works correctly, however when I try to make it concurrent it does not return anything and does not give any errors. For example this is the method working correctly

func Listing_Expiration(w http.ResponseWriter, r *http.Request)  {

        db,err := sql.Open("DB_Connect")

        if err != nil {
            log.Fatal(err)
            println(err)
        }



        var result string

        errr := db.QueryRow("select json_build_object('Expiration', array_to_json(array_agg(t))) from (select fullname,ad_end from profiles where id=32)t").Scan(&result)


        defer db.Close()
        switch {
        case errr != nil:
            log.Fatal(errr)


        }


        fmt.Fprintf(w,result)

}

The above method works correctly and the data is returned to the browser then I try to make this method async

 func Listing_Expiration(w http.ResponseWriter, r *http.Request)  {
        go func(){

            db,err := sql.Open("DB_Connect")

            if err != nil {
                log.Fatal(err)
                println(err)
            }



            var result string

            errr := db.QueryRow("select json_build_object('Expiration', array_to_json(array_agg(t))) from (select fullname,ad_end from profiles where id=32)t").Scan(&result)


            defer db.Close()
            switch {
            case errr != nil:
                log.Fatal(errr)


            }


            fmt.Fprintf(w,result)

        }()
    }

The async above returns nothing the only thing I have changed is that I added the Go func() inside the method so that it is async and everything else is the same . I checked and the database is returning the same content back just have no idea why the async is not printing the results back to the browser.

  • 写回答

1条回答 默认 最新

  • dpdjv9559 2016-07-11 04:06
    关注

    The net/http server completes the response when the handler returns. Anything written to the response after the handler returns is ignored.

    The handler function Listing_Expiration is returning before the anonymous function executes fmt.Fprintf(w,result).

    To fix this, use a sync.WaitGroup:

    func Listing_Expiration(w http.ResponseWriter, r *http.Request)  {
      var wg sync.WaitGroup
      wg.Add(1)
      go func(){
        defer wg.Done()
        // same code here as in question
      }()
      wg.Wait()
    }
    

    The net/http server starts a goroutine for each connection and runs handlers in those goroutines. If the code in the question is the complete handler, then there's no reason to start yet another goroutine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于python拓展屏使用pyautogui.screenshot无法截图的问题
  • ¥15 有偿求答 无导出权限怎么快速导出企业微信微文档
  • ¥15 求解答,怎么数码管中这么加入闹钟或者传感器,这应该怎么加入相应的代码
  • ¥15 scottplot5
  • ¥30 想问问这个建模怎么编程没有思路
  • ¥15 关于imageENview(ImageEN)中新建图层并根据鼠标位置添加图标
  • ¥100 用两台电脑局域联网进行MT5的EA参数优化,但是连接不上,日志提示:
  • ¥15 FastAPI报错: AsyncSession不是有效Pydantic类型
  • ¥50 这Mac系统提示虚拟内存不足,怎么解决
  • ¥15 Rs232电路无法收发数据,求帮助