zimuyang
zimuyang

Reputation: 23

Why in Linux, golang run Http service will automatically create multiple child processes?

This is what I used to do the test code

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"))
    }
}

Here is the situation when the code runs

htop output showing a tree with nested apptest threads

System is centos 6.5

This normal? If not normal, how to limit golang automatically create a large number of child processes?

Upvotes: 0

Views: 330

Answers (1)

Zan Lynx
Zan Lynx

Reputation: 54353

Your htop program is displaying threads, not processes. What you are seeing is Go's natural creation of threads which are used to run goroutines and to run system calls without blocking in the kernel.

You can control the number of threads created with the GOMAXPROCS environment variable.

Upvotes: 3

Related Questions