Last active
July 15, 2024 07:55
Revisions
-
harlow revised this gist
Mar 12, 2017 . 1 changed file with 8 additions and 8 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,29 +11,29 @@ const concurrency = 3 func main() { // put tasks on channel tasks := make(chan int, 100) go func() { for j := 1; j <= 9; j++ { tasks <- j } close(tasks) }() // waitgroup, and close results channel when work done results := make(chan int) wg := &sync.WaitGroup{} wg.Add(concurrency) go func() { wg.Wait() close(results) }() for i := 1; i <= concurrency; i++ { go func(id int) { defer wg.Done() for t := range tasks { fmt.Println("worker", id, "processing job", t) results <- t * 2 time.Sleep(time.Second) } }(i) @@ -43,4 +43,4 @@ func main() { for r := range results { fmt.Println("result", r) } } -
harlow revised this gist
Mar 12, 2017 . 1 changed file with 22 additions and 26 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,42 +9,38 @@ import ( const concurrency = 3 func main() { // put tasks on channel tasks := make(chan int, 100) go func(){ for j := 1; j <= 9; j++ { tasks <- j } close(jobs) }() // waitgroup, and close results channel when work done results := make(chan int) wg := &sync.WaitGroup{} wg.Add(concurrency) go func() { wg.Wait() close(results) }() for i := 1; i <= concurrency; i++ { go func(id int){ defer wg.Done() for t := range tasks { fmt.Println("worker", id, "processing job", t) results <- j * 2 time.Sleep(time.Second) } }(i) } // loop over results until closed (see above) for r := range results { fmt.Println("result", r) } } -
harlow revised this gist
Mar 3, 2017 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,8 +12,7 @@ func main() { jobs := make(chan int, 100) results := make(chan int) // workers to process jobs channel, puts output onto results channel wg := &sync.WaitGroup{} wg.Add(concurrency) for w := 1; w <= concurrency; w++ { -
harlow revised this gist
Mar 3, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,11 +9,11 @@ import ( const concurrency = 3 func main() { jobs := make(chan int, 100) results := make(chan int) // workers to process jobs channel, puts output // onto results channel wg := &sync.WaitGroup{} wg.Add(concurrency) for w := 1; w <= concurrency; w++ { -
harlow revised this gist
Mar 3, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,7 +32,7 @@ func main() { } close(jobs) // loop over results until closed (see above) for r := range results { fmt.Println("result", r) } -
harlow revised this gist
Mar 3, 2017 . 1 changed file with 12 additions and 12 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,18 +6,6 @@ import ( "time" ) const concurrency = 3 func main() { @@ -49,3 +37,15 @@ func main() { fmt.Println("result", r) } } // worker will receive jobs from `in` channel and send results // on the `out` channel. func worker(id int, wg *sync.WaitGroup, in <-chan int, out chan<- int) { defer wg.Done() for j := range jobs { fmt.Println("worker", id, "processing job", j) results <- j * 2 time.Sleep(time.Second) } } -
harlow revised this gist
Mar 3, 2017 . 1 changed file with 12 additions and 16 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,12 +6,9 @@ import ( "time" ) // worker will receive jobs from `in` channel and send results // on the `out` channel. func worker(id int, wg *sync.WaitGroup, in <-chan int, out chan<- int) { defer wg.Done() for j := range jobs { @@ -24,31 +21,30 @@ func worker(id int, wg *sync.WaitGroup, jobs <-chan int, results chan<- int) { const concurrency = 3 func main() { // channels for jobs and results jobs := make(chan int, 100) results := make(chan int) // add to waitgroup and creates workers wg := &sync.WaitGroup{} wg.Add(concurrency) for w := 1; w <= concurrency; w++ { go worker(w, wg, jobs, results) } // close results channel when work is done go func() { wg.Wait() close(results) }() // put jobs on channel for j := 1; j <= 9; j++ { jobs <- j } close(jobs) // loop over results until `results` channel is closed (see above) for r := range results { fmt.Println("result", r) } -
harlow created this gist
Oct 7, 2016 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ package main import ( "fmt" "sync" "time" ) // Here's the worker, of which we'll run several // concurrent instances. These workers will receive // work on the `jobs` channel and send the corresponding // results on `results`. We'll sleep a second per job to // simulate an expensive task. func worker(id int, wg *sync.WaitGroup, jobs <-chan int, results chan<- int) { defer wg.Done() for j := range jobs { fmt.Println("worker", id, "processing job", j) results <- j * 2 time.Sleep(time.Second) } } const concurrency = 3 func main() { // Create channels for our pool of work, and to collect the results jobs := make(chan int, 100) results := make(chan int) // results channel when all work is done. wg := &sync.WaitGroup{} wg.Add(concurrency) go func() { wg.Wait() close(results) }() // creates workers for w := 1; w <= concurrency; w++ { go worker(w, wg, jobs, results) } // Here we send 9 `jobs` and then `close` that // channel to indicate that's all the work we have. for j := 1; j <= 9; j++ { jobs <- j } close(jobs) // Collect the results and disply the work completed for r := range results { fmt.Println("result", r) } }