我正在尝试与 Go 并行写入 100000 个文件。
我不知道为什么,但是当我使用 argv 参数“100000”调用它时,下面的代码在大约 30% 的时间内崩溃。
这是崩溃:
goroutine 3749 [chan send]:
main.CallShellCommand(0xc820016180, 0xea1)
.../parallel.go:13 +0x1bf
created by main.main
.../parallel.go:22 +0xbd
这是代码:
package main
import "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
}
}
弑天下
相关分类