Open mylxy opened 1 year ago
Hey @mylxy!
The value reported by pool.RunningWorkers()
is the number of worker gorutines that is currently running (either executing a task or idle), so it won't necessarily match the total number of tasks that were submitted to the pool.
In this example, you are instantiating the pool with a limit of 10 workers (first argument passed to pond.New
), which means there cannot be more than 10 goroutines processing tasks at any given time.
Once all 20 tasks have completed, workers start to become idle since there are no more tasks in the queue. This is why the number of idle workers starts to increase. But at the same time, idle workers are gradually killed and that's why you see 9 instead of 10. That indicates one of the idle workers was killed. If you extend the time this example runs, you will see that the pool eventually reaches 0 running workers and 0 idle.
An idle thread is also running at the same time, can a thread have both states at the same time?
pool.RunningWorkers()
reflects the total number of worker goroutines that are running (either busy or idle), while pool.IdleWorkers()
only returns the number of worker goroutines that are not currently processing any task.
You could also calculate the number of "busy" worker goroutines by doing pool.RunningWorkers() - pool.IdleWorkers()
.
Cheers!
//test demo func TestTaskGroup(t *testing.T) {
}
// active result log mertics: running=10, ide=0, waiting=10 mertics: running=10, ide=0, waiting=10 mertics: running=10, ide=0, waiting=0 mertics: running=10, ide=0, waiting=0 mertics: running=10, ide=0, waiting=0 all tasks has complete mertics: running=9, ide=9, waiting=0 mertics: running=9, ide=9, waiting=0 mertics: running=9, ide=9, waiting=0 mertics: running=9, ide=9, waiting=0 mertics: running=9, ide=9, waiting=0
Puzzled : why all tasks has complete, running task more than zero ?