英文:
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 <-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` will call `InteractWithCheck` with `go`, the following code:
func DataCheck(clusterId int, tableName string, keyList []string) error {
// ...
for _, sliceKey := range keyList {
// when len(keyList) > 8000, it will be hang
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:
// check result
// ...
}
}
return nil
}
When `len(keylist) >= 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't handle it.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论