我在 Golang 中編寫了一個(gè) httpserver,但是我發(fā)現(xiàn)當(dāng)來自 Web 瀏覽器的多個(gè)請(qǐng)求時(shí) http.HandleFunc 將被阻止。我怎樣才能讓服務(wù)器同時(shí)處理多個(gè)請(qǐng)求?謝謝。我的代碼是: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...")}我運(yùn)行了你的代碼,一切都按預(yù)期工作。我同時(shí)做了兩個(gè)請(qǐng)求(curl localhost:9090/query),它們都在 10 秒后完成。也許問題出在其他地方?這是我使用的命令: time curl -s localhost:9090/query | echo $(curl -s localhost:9090/query) – tjameson塔肯斯那很奇怪。當(dāng)我從 chrome 請(qǐng)求相同的 url 時(shí),發(fā)送兩個(gè)請(qǐng)求不是同時(shí)處理,但使用 cur 測(cè)試可以同時(shí)處理。但是當(dāng)我發(fā)送兩個(gè)請(qǐng)求使用不同的 url 時(shí),它可以同時(shí)處理。[root@localhost httpserver]# ./httpServer服務(wù)器開始工作...1374301593 路徑 /query?form=chrome1374301612 路徑 /query?from=cur21374301614 路徑 /query?from=cur11374301618 路徑 /query?form=chrome1374301640 路徑 /query?form=chrome21374301643 路徑 /query?form=chrome1*1374301715 路徑 /query?form=chrome1374301725 路徑 /query?form=chrome***1374301761 路徑 /query?form=chrome11374301763 路徑 /query?form=chrome2**
- 1 回答
- 0 關(guān)注
- 185 瀏覽
添加回答
舉報(bào)
0/150
提交
取消