为什么在Linux、golang 运行 Http服务会自动创建多个子进程?
Why in Linux, golang run Http service will automatically create multiple child processes?
这是我用来做测试的代码
package main
import (
"net/http"
"time"
)
func main() {
s := &http.Server{
Addr: ":9301",
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
http.HandleFunc("/line/getList", Response)
s.ListenAndServe()
}
func Response(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Connection", "keep-alive")
w.WriteHeader(200)
for i:= 0; i < 10000; i++ {
w.Write([]byte("hello world"))
}
}
下面是代码运行时的情况
系统是centos 6.5
这正常吗?如果不正常,如何限制golang自动创建大量子进程?
您的 htop
程序显示的是线程,而不是进程。您所看到的是 Go 自然创建的线程,这些线程用于 运行 goroutines 和 运行 系统调用而不会在内核中阻塞。
You can control the number of threads created with the GOMAXPROCS environment variable.
这是我用来做测试的代码
package main
import (
"net/http"
"time"
)
func main() {
s := &http.Server{
Addr: ":9301",
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
http.HandleFunc("/line/getList", Response)
s.ListenAndServe()
}
func Response(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Connection", "keep-alive")
w.WriteHeader(200)
for i:= 0; i < 10000; i++ {
w.Write([]byte("hello world"))
}
}
下面是代码运行时的情况
系统是centos 6.5
这正常吗?如果不正常,如何限制golang自动创建大量子进程?
您的 htop
程序显示的是线程,而不是进程。您所看到的是 Go 自然创建的线程,这些线程用于 运行 goroutines 和 运行 系统调用而不会在内核中阻塞。
You can control the number of threads created with the GOMAXPROCS environment variable.