2018-10-24 13:58 阅读 50


I know, most of the beginners of go ask how to have performative go-routines / concurrency, this point I passed a few weeks ago. :-)

I have a real fast trans-coder that uses every cycle available of my 4+4 (i7 HT) CPU. It reads a file into a slice of pointers to structs, does calculations on these and writes the result back to disk. I am using bufio. I am coming from VB so the performance of Go is unbelievable.

I tried to add minimal sleeps (via time.Sleep()), but that drastically decreased performance.

While my trans-coder is working the whole system is lagging. I must change the go task's priority to low or idle to be able to work again.

How could I implement something that keeps the system responsive?

Right now I start thousands of go-routines (loop over a slice of pointers). Should I limit the number of routines?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongshen3352 dongshen3352 2018-10-24 14:00

    Lowering the process priority is arguably the correct way to do this. Use your OS's scheduler. That's what it's for. Per this question you can start your process with a specified priority like so:

    start "MyApp" /low "C:\myapp.exe"

    You may also be able to set process priority from within the application per this question:

    err := syscall.Setpriority(syscall.Getpid(), -1, -1)

    Lastly, you can use GOMAXPROCS to configure how many CPUs the process is allowed to use. You can pass it in as an environment variable at runtime, or call runtime.GOMAXPROCS() within your code to override it.

    点赞 评论 复制链接分享
  • dongshi2141 dongshi2141 2018-10-24 23:11

    Probably the simplest solution is to limit the number of concurrent goroutines using a semaphore, for example:

    sem := make(chan int, 10) // limited at go routines 
    for {
        sem <- 1
        go doThis()
    func doThis() {
        //do this
        <- sem

    The "sem <- 1" inside the for loop blocks until a goroutine "slot" is freed up by doThis extracting something from the sem channel/

    点赞 评论 复制链接分享