我正在嘗試與 Go 并行寫入 100000 個(gè)文件。我不知道為什么,但是當(dāng)我使用 argv 參數(shù)“100000”調(diào)用它時(shí),下面的代碼在大約 30% 的時(shí)間內(nèi)崩潰。這是崩潰:goroutine 3749 [chan send]:main.CallShellCommand(0xc820016180, 0xea1) .../parallel.go:13 +0x1bfcreated by main.main .../parallel.go:22 +0xbd這是代碼:package mainimport "fmt"import "io/ioutil"import "strconv"import "os"import "runtime"func CallCommand(ch chan struct{}, id int) { ioutil.WriteFile(fmt.Sprintf("/tmp/my_prefix_%d", id), []byte("HELLO\n"), 0644) ch <- struct{}{}}func main() { runtime.GOMAXPROCS(4) n, _ := strconv.Atoi(os.Args[1]) ch := make(chan struct{}) for i := 0; i < n; i++ { go CallCommand(ch, i+1) } for j := 0; j < n; j++ { <-ch }}
執(zhí)行大量 I/O 的 go 程序崩潰
30秒到達(dá)戰(zhàn)場
2021-12-20 09:54:15