Search code examples
windowsgoconcurrencygoroutine

Possibility to NOT hook all available CPU power?


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?


Solution

  • 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.