在进行大量I/O操作的Go程序中崩溃

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

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 &quot;fmt&quot;
import &quot;io/ioutil&quot;
import &quot;strconv&quot;
import &quot;os&quot;
import &quot;runtime&quot;

func CallCommand(ch chan struct{}, id int) {
	ioutil.WriteFile(fmt.Sprintf(&quot;/tmp/my_prefix_%d&quot;, id), []byte(&quot;HELLO\n&quot;), 0644)
	ch &lt;- struct{}{}
}

func main() {
	runtime.GOMAXPROCS(4)
	n, _ := strconv.Atoi(os.Args[1])
	ch := make(chan struct{})
	for i := 0; i &lt; n; i++ {
		go CallCommand(ch, i+1)
	}
	for j := 0; j &lt; n; j++ {
		&lt;-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.

huangapple
  • 本文由 发表于 2016年2月26日 00:28:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/35632942.html
匿名

发表评论

匿名网友

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

确定