Skip to content

Instantly share code, notes, and snippets.

@hauxe
Last active May 27, 2018 05:33
Show Gist options
  • Save hauxe/21bc9efbb9b082224d9f66f63cd6572b to your computer and use it in GitHub Desktop.
Save hauxe/21bc9efbb9b082224d9f66f63cd6572b to your computer and use it in GitHub Desktop.
Worker pool
package main
// Job define job interface
type Job interface {
Execute()
}
// JobQueue queue of jobs
var JobQueue chan Job
// WorkerNum number of workers
var WorkerNum int
func main() {
WorkerNum = 100
JobQueue := make(chan Job, WorkerNum)
for i := 0; i < WorkerNum; i++ {
// start a worker
go func() {
for {
job := <-JobQueue
// do work
job.Execute()
}
}()
}
// wait for jobs
for {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment