Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metric problem #47

Open
mylxy opened this issue Aug 4, 2023 · 3 comments
Open

metric problem #47

mylxy opened this issue Aug 4, 2023 · 3 comments

Comments

@mylxy
Copy link

mylxy commented Aug 4, 2023

//test demo
func TestTaskGroup(t *testing.T) {

// Create a pool
pool := pond.New(10, 1000)
defer pool.StopAndWait()

go func() {
	for {
		println(fmt.Sprintf("mertics: running=%v, ide=%v, waiting=%v", pool.RunningWorkers(), pool.IdleWorkers(), pool.WaitingTasks()))
		time.Sleep(1 * time.Second)
	}
}()

// Create a task group
group := pool.Group()

// Submit a group of tasks
for i := 0; i < 20; i++ {
	//n := i
	group.Submit(func() {
		//fmt.Printf("Running group task #%d\n", n)
		time.Sleep(2 * time.Second)
	})
}

// Wait for all tasks in the group to complete
group.Wait()
println("all tasks has complete")

time.Sleep(2 * time.Hour)

}

// 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 ?

@alitto
Copy link
Owner

alitto commented Aug 4, 2023

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.

@mylxy
Copy link
Author

mylxy commented Aug 16, 2023

An idle thread is also running at the same time, can a thread have both states at the same time?

@alitto
Copy link
Owner

alitto commented Aug 17, 2023

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants