When Go Scheduler Creates New M and P in the GMP Model
In Go's GMP (Goroutine, Machine, Processor) model, the scheduler manages the creation of M (Machine) and P (Processor) based on specific conditions.
M Creation
M is created in response to specific events, such as:
P Creation
P is created at program startup based on the GOMAXPROCS environment variable, which specifies the maximum number of available P. The default value is the number of logical CPUs on the system.
Example Analysis
In your example code, you have two batches of goroutines running database operations. Each goroutine performs a blocking I/O operation.
Therefore, in your case, the scheduler will create more than 8 M for the second batch of goroutines because the operations are blocking. The P will be limited to 1 based on the GOMAXPROCS value, but M will be created dynamically as needed.
Additional Resources
For further understanding, refer to the following resources:
The above is the detailed content of How Does the Go Scheduler Create New M and P When Goroutines Perform Blocking Operations?. For more information, please follow other related articles on the PHP Chinese website!