Created
November 10, 2022 10:16
-
-
Save vanleantking/0b78477f4270aaa8fd389aa2bea3a4ce to your computer and use it in GitHub Desktop.
workers use buffer channel
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 characters
https://play.golang.com/p/9uHR2jIDR9y | |
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
const ( | |
numberOfURLs = 10000 | |
numberOfWorkers = 5 | |
) | |
func crawlURL(queue <-chan int, name string) { | |
for v := range queue { | |
fmt.Printf("Worker %s is crawling URL %d\n", name, v) | |
time.Sleep(time.Second) | |
} | |
fmt.Printf("Worker %s done and exit\n", name) | |
} | |
func startQueue() <-chan int { | |
queue := make(chan int, 100) | |
go func() { | |
for i := 1; i <= numberOfURLs; i++ { | |
queue <- i | |
fmt.Printf("URL %d has been enqueued\n", i) | |
} | |
close(queue) | |
}() | |
return queue | |
} | |
func main() { | |
queue := startQueue() | |
for i := 1; i <= numberOfWorkers; i++ { | |
go crawlURL(queue, fmt.Sprintf("%d", i)) | |
} | |
time.Sleep(time.Minute * 5) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment