一个Go程序能同时启动8k+个Goroutine吗?

huangapple go评论96阅读模式
英文:

Can a Go program start 8k+ Goroutines simultaneously?

问题

我需要为发送HTTP请求启动多个goroutine。以下是我的代码:

func InteractWithCheck(clusterId int, tableName string, key string, jobs <-chan int, results chan<- objRes) error {
    // the host only test
    host := "http://xxx.yyy.com/a/b?method=check&"
    for _ = range jobs {
        url := fmt.Sprintf("%scluster=%d&table=%s&key=%s", host, clusterId, tableName, key)
        for intRetry := 0; intRetry < 3; intRetry++ {
            resp, err := http.Get(url)
            if err != nil {
                continue
            }
            if resp.StatusCode >= 500 {
                continue
            }
            defer resp.Body.Close()
            body, err := ioutil.ReadAll(resp.Body)
            if err != nil {
                return err
            }
            // ...
            return nil
        }
    }
    return nil
}

// DataCheck将使用go关键字调用InteractWithCheck,代码如下:

func DataCheck(clusterId int, tableName string, keyList []string) error {
    // ...

    for _, sliceKey := range keyList {
        // 当len(keyList) >= 8000时,程序会挂起
        go InteractWithCheck(clusterId, tableName, sliceKey, jobs, results)
    }

    for j := 0; j < num; j++ {
        jobs <- j
    }
    close(jobs)

    for k := 0; k < num; k++ {
        select {
        case res := <-results:
            // 检查结果
            // ...
        }
    }
    return nil
}

`len(keylist) >= 8000`程序会挂起所以我应该对其进行优化吗

<details>
<summary>英文:</summary>

I need to start multiple goroutines for sending http requests. The following is my code:
    
    func InteractWithCheck(clusterId int, tableName string, key string, jobs &lt;-chan int, results chan&lt;- objRes) error {
        // the host only test
        host := &quot;http://xxx.yyy.com/a/b?method=check&amp;&quot;
        for _ = range jobs {
            url := fmt.Sprintf(&quot;%scluster=%d&amp;table=%s&amp;key=%s&quot;, host, clusterId, tableName, key)
            for intRetry := 0; intRetry &lt; 3; intRetry++ {
                resp, err := http.Get(url)
                if err != nil {
                    continue
                }
                if resp.StatusCode &gt;= 500 {
                    continue
                }
                defer resp.Body.Close()
                body, err := ioutil.ReadAll(resp.Body)
                if err != nil {
                    return err
                }
                // ...
                return nil
            }
        }
        return nil
    }

`DataCheck` will call `InteractWithCheck` with `go`, the following code:

    func DataCheck(clusterId int, tableName string, keyList []string) error {
        // ...

        for _, sliceKey := range keyList {
            // when len(keyList) &gt; 8000, it will be hang
            go InteractWithCheck(clusterId, tableName, sliceKey, jobs, results)
        }

        for j := 0; j &lt; num; j++ {
            jobs &lt;- j
        }
        close(jobs)

        for k := 0; k &lt; num; k++ {
            select {
            case res := &lt;-results:
                // check result
                // ...
            }
        }
        return nil
    }


When `len(keylist) &gt;= 8000`, the program will be hang, so I should optimize it?

</details>


# 答案1
**得分**: 1

可用于生成的goroutine数量取决于您计算机的RAM和实时内存因此是的您可以启动8k+个goroutine但如果您的计算机无法处理则不能这样做

<details>
<summary>英文:</summary>

The amount of available goroutines to spawn depends on your RAM, live memory of your computer. So yes, you can start 8k+, but not if your computer can&#39;t handle it.

</details>



huangapple
  • 本文由 发表于 2017年5月2日 22:56:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/43741109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定