package main
import (
"fmt"
"time"
"github.com/Jeffail/tunny"
)
func main() {
const numTasks = 50
// Create pool with 10 workers
pool := tunny.NewFunc(10, func(payload interface{}) interface{} {
taskID := payload.(int)
// Simulate work
time.Sleep(time.Millisecond * time.Duration(100+taskID*10))
return fmt.Sprintf("Task %d completed", taskID)
})
defer pool.Close()
// Submit tasks
results := make([]interface{}, numTasks)
for i := 1; i <= numTasks; i++ {
go func(taskID int) {
results[taskID-1] = pool.Process(taskID)
}(i)
}
// Wait for tasks (simple wait for demo)
time.Sleep(3 * time.Second)
// Count completed tasks
completed := 0
for _, result := range results {
if result != nil {
completed++
fmt.Println(result.(string))
}
}
fmt.Printf("\nSummary: %d out of %d tasks completed\n", completed, numTasks)
}
No comments:
Post a Comment