From 513eefcddc228b76b0bd4184c89bcba2e167477f Mon Sep 17 00:00:00 2001
From: Mark McGranaghan
Date: Tue, 27 Dec 2016 10:32:33 -0800
Subject: [PATCH] Clarify worker-pool example
Ref #131
---
examples/worker-pools/worker-pools.go | 9 +++---
examples/worker-pools/worker-pools.hash | 4 +--
examples/worker-pools/worker-pools.sh | 27 +++++++++---------
public/worker-pools | 38 +++++++++++++------------
4 files changed, 41 insertions(+), 37 deletions(-)
diff --git a/examples/worker-pools/worker-pools.go b/examples/worker-pools/worker-pools.go
index 4aa5d69..1114a22 100644
--- a/examples/worker-pools/worker-pools.go
+++ b/examples/worker-pools/worker-pools.go
@@ -13,8 +13,9 @@ import "time"
// simulate an expensive task.
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
- fmt.Println("worker", id, "processing job", j)
+ fmt.Println("worker", id, "started job", j)
time.Sleep(time.Second)
+ fmt.Println("worker", id, "finished job", j)
results <- j * 2
}
}
@@ -33,15 +34,15 @@ func main() {
go worker(w, jobs, results)
}
- // Here we send 9 `jobs` and then `close` that
+ // Here we send 5 `jobs` and then `close` that
// channel to indicate that's all the work we have.
- for j := 1; j <= 9; j++ {
+ for j := 1; j <= 5; j++ {
jobs <- j
}
close(jobs)
// Finally we collect all the results of the work.
- for a := 1; a <= 9; a++ {
+ for a := 1; a <= 5; a++ {
<-results
}
}
diff --git a/examples/worker-pools/worker-pools.hash b/examples/worker-pools/worker-pools.hash
index 09eb260..013b2de 100644
--- a/examples/worker-pools/worker-pools.hash
+++ b/examples/worker-pools/worker-pools.hash
@@ -1,2 +1,2 @@
-9d5b1d008d278df7db7697d56d491d982da86e7d
-y12QxBsKtT
+1f9acf1e50be05cad73e6b085ed3294892c67d42
+RTRcHA05vV
diff --git a/examples/worker-pools/worker-pools.sh b/examples/worker-pools/worker-pools.sh
index 90937e1..10139fb 100644
--- a/examples/worker-pools/worker-pools.sh
+++ b/examples/worker-pools/worker-pools.sh
@@ -1,16 +1,17 @@
-# Our running program shows the 9 jobs being executed by
-# various workers. The program only takes about 3 seconds
-# despite doing about 9 seconds of total work because
+# 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 processing job 1
-worker 2 processing job 2
-worker 3 processing job 3
-worker 1 processing job 4
-worker 2 processing job 5
-worker 3 processing job 6
-worker 1 processing job 7
-worker 2 processing job 8
-worker 3 processing job 9
+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 0m3.149s
+real 0m2.358s
diff --git a/public/worker-pools b/public/worker-pools
index 87cc85d..ba84dfe 100644
--- a/public/worker-pools
+++ b/public/worker-pools
@@ -40,7 +40,7 @@ a worker pool using goroutines and channels.
-
+
@@ -73,8 +73,9 @@ simulate an expensive task.
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
- fmt.Println("worker", id, "processing job", j)
+ fmt.Println("worker", id, "started job", j)
time.Sleep(time.Second)
+ fmt.Println("worker", id, "finished job", j)
results <- j * 2
}
}
@@ -129,13 +130,13 @@ because there are no jobs yet.
- Here we send 9 jobs and then close that
+ Here we send 5 jobs and then close that
channel to indicate that’s all the work we have.
|
- for j := 1; j <= 9; j++ {
+ for j := 1; j <= 5; j++ {
jobs <- j
}
close(jobs)
@@ -151,7 +152,7 @@ channel to indicate that’s all the work we have.
|
- for a := 1; a <= 9; a++ {
+ for a := 1; a <= 5; a++ {
<-results
}
}
@@ -166,24 +167,25 @@ channel to indicate that’s all the work we have.
- Our running program shows the 9 jobs being executed by
-various workers. The program only takes about 3 seconds
-despite doing about 9 seconds of total work because
+ 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 processing job 1
-worker 2 processing job 2
-worker 3 processing job 3
-worker 1 processing job 4
-worker 2 processing job 5
-worker 3 processing job 6
-worker 1 processing job 7
-worker 2 processing job 8
-worker 3 processing job 9
+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
|
@@ -195,7 +197,7 @@ there are 3 workers operating concurrently.
- |
| |