问题描述

GOMAXPROCS10
引用runtime的包文档:runtime
<块引用>

GOMAXPROCS 变量限制了可以同时执行用户级 Go 代码的操作系统线程数. 代表 Go 的系统调用中可以阻塞的线程数没有限制代码;这些不计入 GOMAXPROCS 限制.该包的 GOMAXPROCS 函数查询和更改限制.

因此,如果您的应用程序没有启动任何新的 goroutine,线程数将小于 10.

如果您的应用启动了许多没有阻塞的 goroutine (>10)(例如在系统调用中),则 10 个操作系统线程将同时执行您的 goroutine.

如果您的应用启动了许多 goroutine,其中许多 (>10) 个在系统调用中被阻塞,则会产生 10 个以上的操作系统线程(但最多只有 10 个将执行用户级 Go 代码).

编辑(响应您的编辑):

我相信 GOMAXPROCS 的默认值是逻辑 CPU 的数量,这是有原因的:因为通常它提供最高的性能.你可以把它留在那里.一般来说,如果您只有 1 个 goroutine 并且您确定您的代码不会产生更多,GOMAXPROCS=1 就足够了,但是您应该测试并且不要相信它.GOMAXPROCSGOMAXPROCS=1
GOMAXPROCS10

Edit:

GOMAXPROCSruntime.NumCPU()GOMAXPROCS

My real question is: If I have a single-goroutine program running in a Docker container that has a CPU quota, what is the minimum number of logical processors that I need in order to have maximum performance?

There is no direct correlation. Threads used by your app may be less than, equal to or more than 10.

runtime

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. This package's GOMAXPROCS function queries and changes the limit.

So if your application does not start any new goroutines, threads count will be less than 10.

If your app starts many goroutines (>10) where none is blocking (e.g. in system calls), 10 operating system threads will execute your goroutines simultaneously.

If your app starts many goroutines where many (>10) are blocked in system calls, more than 10 OS threads will be spawned (but only at most 10 will be executing user-level Go code).

Edit (in response to your edit):

GOMAXPROCSGOMAXPROCS=1

这篇关于Go 运行时使用的线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!