Skip to content

Commit

Permalink
[enh] update async examples - gocolly#91
Browse files Browse the repository at this point in the history
  • Loading branch information
asciimoo committed Jan 20, 2018
1 parent 2103c5c commit 23cac86
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion _examples/parallel/parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func main() {
// MaxDepth is 2, so only the links on the scraped page
// and links on those pages are visited
colly.MaxDepth(2),
colly.Async(true),
)

// Limit the maximum parallelism to 5
Expand All @@ -28,7 +29,7 @@ func main() {
// Print link
fmt.Println(link)
// Visit link found on page on a new thread
go e.Request.Visit(link)
e.Request.Visit(link)
})

// Start scraping on https://en.wikipedia.org
Expand Down
3 changes: 2 additions & 1 deletion _examples/random_delay/random_delay.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func main() {
c := colly.NewCollector(
// Attach a debugger to the collector
colly.Debugger(&debug.LogDebugger{}),
colly.Async(true),
)

// Limit the number of threads started by colly to two
Expand All @@ -27,7 +28,7 @@ func main() {

// Start scraping in four threads on https://httpbin.org/delay/2
for i := 0; i < 4; i++ {
go c.Visit(fmt.Sprintf("%s?n=%d", url, i))
c.Visit(fmt.Sprintf("%s?n=%d", url, i))
}
// Start scraping on https://httpbin.org/delay/2
c.Visit(url)
Expand Down
10 changes: 5 additions & 5 deletions _examples/rate_limit/rate_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ func main() {

// Instantiate default collector
c := colly.NewCollector(
// Turn on asynchronous requests
colly.Async(true),
// Attach a debugger to the collector
colly.Debugger(&debug.LogDebugger{}),
)
Expand All @@ -24,12 +26,10 @@ func main() {
//Delay: 5 * time.Second,
})

// Start scraping in four threads on https://httpbin.org/delay/2
for i := 0; i < 4; i++ {
go c.Visit(fmt.Sprintf("%s?n=%d", url, i))
// Start scraping in five threads on https://httpbin.org/delay/2
for i := 0; i < 5; i++ {
c.Visit(fmt.Sprintf("%s?n=%d", url, i))
}
// Start scraping on https://httpbin.org/delay/2
c.Visit(url)
// Wait until threads are finished
c.Wait()
}

0 comments on commit 23cac86

Please sign in to comment.