Last active
April 7, 2016 10:10
-
-
Save kaikousa/e1613e6626523a72a82d98d42e131eff to your computer and use it in GitHub Desktop.
This file contains hidden or 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 characters
/* | |
* A very simple example of having a pool of workers perform jobs | |
* from the same channel. Simulates a situation where we have a long running | |
* process such as web server that receives requests and then posts the payloads | |
* to workers. | |
*/ | |
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"strconv" | |
"time" | |
) | |
// worker-function for polling more jobs and performing them | |
// workerID: id to separate workers output from each other | |
// jobs: a channel of string, the worker polls this channel for more jobs | |
// using <- means that this channel is receive only, we can't post back to this channel | |
// from this function | |
func worker(workerID int, jobs <-chan string) { | |
// use loop to keep reading jobs from the channel | |
// until it is closed (the magic is in the use of ´range´) | |
for job := range jobs { | |
// the "job" is to print out values we receive | |
fmt.Println("\njob(" + strconv.Itoa(workerID) + "): " + job) | |
// simulate a long running job | |
if workerID == 3 { | |
time.Sleep(time.Second * 3) | |
} | |
} | |
} | |
func main() { | |
// channel to post jobs into | |
jobs := make(chan string) | |
// create a pool of workers (goroutines) | |
for w := 1; w <= 3; w++ { | |
go worker(w, jobs) | |
} | |
// read user input | |
reader := bufio.NewReader(os.Stdin) | |
for { | |
text, _ := reader.ReadString('\n') | |
// post a job into jobs-channel | |
jobs <- text | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment