89
Go Worker Pools
Worker pools is a design in which a fixed number of m workers (Go goroutines) works on n tasks in a work queue (Go channel). The work resides in a queue until a worker finish its current task and pull a new one.
Let’s See it by an example
Output:
worker 2 processing job 1 worker 1 processing job 2 worker 2 processing job 3 worker 1 processing job 4 worker 1 processing job 5 worker 2 processing job 6 worker 1 processing job 7 worker 2 processing job 8
In this example, 2 workers are started and 9 work items are in put onto a job channel. Workers have a work loop with a time.Sleep so that each ends up working 2 jobs. close is used on the channel after all the work’s been put onto it, which signals to all 2 workers that they can exit their work loop by dropping them out of their loop on range.
Next TopicGo Time