alitto / pond

🔘 Minimalistic and High-performance goroutine worker pool written in Go
MIT License
1.43k stars 60 forks source link

run stop panic #49

Open liusl104 opened 10 months ago

liusl104 commented 10 months ago

test demo : package main

import ( "fmt" "github.com/alitto/pond" "time" )

func testPool(i int, p *pond.WorkerPool) { if i == 8 { var stop string // fmt.Printf("stop -- %d : ", i) // fmt.Scanln(&stop) stop = "yes" if stop == "yes" { fmt.Println("stop ok") p.Stop() return }

}
fmt.Printf("run %d \n", i)
time.Sleep(time.Duration(i) * time.Second)

} func main() { // Create a buffered (non-blocking) pool that can scale up to 100 workers // and has a buffer capacity of 1000 tasks pool := pond.New(5, 1000, pond.MinWorkers(5))

// Submit 1000 tasks
for i := 1; i < 1000; i++ {
    n := i
    pool.Submit(func() {
        testPool(n, pool)

    })
}
// pool.RunningWorkers()
// Stop the pool and wait for all submitted tasks to complete
fmt.Println("wait all task done ...")
pool.StopAndWait()

} package main

import ( "fmt" "github.com/alitto/pond" "time" )

func testPool(i int, p *pond.WorkerPool) { if i == 8 { var stop string // fmt.Printf("stop -- %d : ", i) // fmt.Scanln(&stop) stop = "yes" if stop == "yes" { fmt.Println("stop ok") p.Stop() return }

}
fmt.Printf("run %d \n", i)
time.Sleep(time.Duration(i) * time.Second)

} func main() { // Create a buffered (non-blocking) pool that can scale up to 100 workers // and has a buffer capacity of 1000 tasks pool := pond.New(5, 1000, pond.MinWorkers(5))

// Submit 1000 tasks
for i := 1; i < 1000; i++ {
    n := i
    pool.Submit(func() {
        testPool(n, pool)

    })
}
// pool.RunningWorkers()
// Stop the pool and wait for all submitted tasks to complete
fmt.Println("wait all task done ...")
pool.StopAndWait()

} output : run 1 run 3 run 2 wait all task done ... run 4 run 5 run 6 run 7 stop ok run 9 run 10 fatal error: all goroutines are asleep - deadlock!