Introduction
This challenge demonstrates how to implement a worker pool using goroutines and channels.
This tutorial is from open-source community. Access the source code
This challenge demonstrates how to implement a worker pool using goroutines and channels.
Implement a worker pool that receives work on the jobs
channel and sends the corresponding results on the results
channel. The worker pool should have multiple concurrent instances, and each worker should sleep for a second per job to simulate an expensive task.
jobs
channel and send the corresponding results on the results
channel.## Our running program shows the 5 jobs being executed by
## various workers. The program only takes about 2 seconds
## despite doing about 5 seconds of total work because
## there are 3 workers operating concurrently.
$ time go run worker-pools.go
worker 1 started job 1
worker 2 started job 2
worker 3 started job 3
worker 1 finished job 1
worker 1 started job 4
worker 2 finished job 2
worker 2 started job 5
worker 3 finished job 3
worker 1 finished job 4
worker 2 finished job 5
real 0m2.358s
This challenge demonstrated how to implement a worker pool using goroutines and channels. The worker pool receives work on the jobs
channel and sends the corresponding results on the results
channel. Each worker sleeps for a second per job to simulate an expensive task.