英文:
Crash in go program doing a lot of I/O
问题
我正在尝试使用Go语言并行地向100,000个文件写入数据。
我不确定为什么,但是当我使用参数"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
}
}
补充信息:
- 我的计算机有4个核心
- 这是Go语言1.5.3版本的代码
英文:
I am trying to write to 100000 files in parallel with Go.
I am not sure why, but this code below crashes ~30% of the time when I call it with the argv parameter "100000".
Here is the crash:
goroutine 3749 [chan send]:
main.CallShellCommand(0xc820016180, 0xea1)
.../parallel.go:13 +0x1bf
created by main.main
.../parallel.go:22 +0xbd
Here is the code:
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
}
}
For the record:
- My computer has 4 cores
- This is go 1.5.3
答案1
得分: 1
@peterSO提到在Go中应该始终检查错误,我本应该这样做。
英文:
@peterSO mentioned that errors should always be checked in Go, I should had done that.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论