drl47263 2012-10-22 20:05
浏览 107
已采纳

并发http请求没有响应

I am playing around with Go a bit and I've a problem that I am unable to solve.

The following code is the least possible code that reproduces my problem. The goal of the original code is to delegate http request to goroutines. Each goroutine does a bit of heavy image calculations and is supposed to respond.

package main

import (
    "fmt"
    "runtime"
    "net/http"
)

func main() {
    http.HandleFunc("/", handle)
    http.ListenAndServe(":8080", nil)
}

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

    // the idea is to be able to handle several requests
    // in parallel

    // the "go" is problematic
    go delegate(w)
}

func delegate(w http.ResponseWriter) {

    // do some heavy calculations first

    // present the result (in the original code, the image)
    fmt.Fprint(w, "hello")
}

In the case of a go delegate(w) I get no response, without the go it works out nicely.

Can anyone explain what's going on? Thanks a lot!

  • 写回答

3条回答 默认 最新

  • douling0053 2012-10-22 20:10
    关注

    ListenAndServe already launches goroutines to call your handler function, so you shouldn't do it yourself.

    Here's the code of the relevant functions from the package source :

    1089    func ListenAndServe(addr string, handler Handler) error {
    1090        server := &Server{Addr: addr, Handler: handler}
    1091        return server.ListenAndServe()
    1092    }
    
    
    1010    func (srv *Server) ListenAndServe() error {
    1011        addr := srv.Addr
    1012        if addr == "" {
    1013            addr = ":http"
    1014        }
    1015        l, e := net.Listen("tcp", addr)
    1016        if e != nil {
    1017            return e
    1018        }
    1019        return srv.Serve(l)
    1020    }
    
    
    1025    func (srv *Server) Serve(l net.Listener) error {
    1026        defer l.Close()
    1027        var tempDelay time.Duration // how long to sleep on accept failure
    1028        for {
    
    1057            go c.serve()
    1058        }
    1059        panic("not reached")
    1060    }
    
    
    579 // Serve a new connection.
    580 func (c *conn) serve() {
    581     defer func() {
    582         err := recover()
    
    669         handler.ServeHTTP(w, w.req)
    

    So your code should simply be

    func handle(w http.ResponseWriter, r *http.Request) {
        // the idea is to be able to handle several requests
        // in parallel
        // do some heavy calculations first
    
        // present the result (in the original code, the image)
        fmt.Fprint(w, "hello")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错