use*_*460 6 block go httpserver
我在Golang中写了一个httpserver,但是我发现当来自Web浏览器的多个请求时,http.HandleFunc将被阻塞.如何让服务器同时处理多个请求?谢谢.
我的代码是:
func DoQuery(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
fmt.Printf("%d path %s\n", time.Now().Unix(), r.URL.Path)
time.Sleep(10 * time.Second)
fmt.Fprintf(w, "hello...")
//why this function block when multi request ?
}
func main() {
fmt.Printf("server start working...\n")
http.HandleFunc("/query", DoQuery)
s := &http.Server{
Addr: ":9090",
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
//MaxHeaderBytes: 1 << 20,
}
log.Fatal(s.ListenAndServe())
fmt.Printf("server stop...")
}
Run Code Online (Sandbox Code Playgroud)
我运行了你的代码,一切都按预期工作.我同时做了两个请求(卷曲localhost:9090 /查询),他们都在10秒后完成了.也许问题出在其他地方?这是我使用的命令:time curl -s localhost:9090/query | echo $(curl -s localhost:9090/query) - tjameson
thakns
真奇怪.当我从chrome请求相同的url时,同时发送两个请求不处理,但是使用cur test可以同时处理.但是当我发送两个请求使用不同的url时,它可以在同一时间处理.
[root @ localhost httpserver]#./ httppServer
服务器开始工作......
1374301593路径/查询?form = chrome
1374301612路径/查询?from = cur2
1374301614路径/查询?from = cur1
1374301618路径/查询?form = chrome
1374301640路径/查询?form = chrome2
1374301643路径/查询?form = chrome1
*1374301715路径/查询?form = chrome
1374301725路径/查询?form = chrome*
**1374301761路径/查询?form = chrome1
1374301763路径/查询?form = chrome2**
| 归档时间: |
|
| 查看次数: |
6174 次 |
| 最近记录: |