taskQueue is an task queue for golang. it steal the idea even the code from blog Handling 1 Million Requests per Minute with Go.
The usage of taskQueue is as simple as four steps:
- Create your own job struct which implement Execute methods.
- Create the queue.
- Start the queue.
- Add job to the queue.
- Waiting for worker to be finished in the main method if it will exit (optional).
import (
"fmt"
"github.com/bubble501/taskQueue"
"testing"
)
type TestJob struct {
id int
}
func (job TestJob) Execute() {
total := 0
for i := 0; i < job.id; i++ {
total = total + i
}
// fmt.Printf("the total for %d is %d\n", job.id, total)
}
func TestTastQueue(*testing.T) {
queue := taskQueue.New(8, 2000)
queue.Start()
var job taskQueue.Job
for i := 0; i < 300000; i++ {
testjob := TestJob{i}
job = testjob
queue.AddJob(job)
}
queue.Wait()
fmt.Printf("Finished")
}