防止过度 CPU 使用 Golang 后台进程

Prevent excessive CPU usage of Golang background process

我正在编写一个监视文件的 Go 程序,并且 运行 如果其中一个文件已更改则执行系统命令。到目前为止,它运作良好。我对我的主 "infinite" 循环有疑问,因为我不希望它占用所有系统 CPU 资源:

runtime.GOMAXPROCS(1)
for {
    updatedFiles, _ := GetWatchMap(config)
    if !reflect.DeepEqual(filesToWatch, updatedFiles) {
        start := time.Now()
        _, _ = colorstring.Println(fmt.Sprintf(" [yellow] ⬇ Update detected[white] at [green]%s[white] > updating...", start.Format("15:04:05")))
        _, _ = Update(config)
        end := time.Now()
        elapsed := end.Sub(start)
        _, _ = colorstring.Println(fmt.Sprintf("  [green]✅  Done![white] in [yellow]%.2f[white] second(s).", elapsed.Seconds()))
        filesToWatch = updatedFiles
    } else {
        time.Sleep(config.SleepTime)
    }
}

所以,我所做的是设置 GOMAXPROCS,所以它只使用“1 CPU/Core”,并且我在 else 分支中添加了一个可配置的休眠时间。

没有休眠时间,htop 显示该进程占用 CPU 时间的 100%(我猜它是一个核心的 100%?)无论我是否调用 runtime.GOMAXPROCS(1)

如果我在我的机器(MacMini i7、12 Core)上使用 30 毫秒的休眠时间,htop 报告进程利用率为 20% CPU,这似乎没问题,但我想这会因计算机 运行正在运行程序。

这里的最佳做法是什么?

GOMAXPROCS 并不像您想象的那样。来自包裹 运行time docu:

The GOMAXPROCS variable limits the number of operating system threads that can execute user-level Go code simultaneously. There is no limit to the number of threads that can be blocked in system calls on behalf of Go code; those do not count against the GOMAXPROCS limit.

它限制了 OS 个线程。如果您的代码不使用 可以 调度到 OS 个线程的 goroutine,那么限制线程的数量实际上是 没有 。 只需删除 GOMAXPROCS 东西,它什么都不做。 (如果你将 GOMAXPROCS 设置为 12,你将最多有 12 OS 个线程实际执行 goroutine 代码;如果你只有一个 goroutine 限制 OS 线程的数量,它可以 运行不。)

您所能做的就是不像使用 time.Sleep() 那样忙于循环。根据 您的 要求,您可以定期调用实际代码,例如通过 time.Ticker。没有单个 "best practice"(除了不摆弄 GOMAXPROCS)。