Created
September 6, 2012 01:25
-
-
Save border/3649617 to your computer and use it in GitHub Desktop.
启动100个goroutines去抓取URL,这100个抓取完后再继续下一组, v2版本更简洁明了,
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
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
const MAX = 100 | |
func watching() { | |
fmt.Printf("NumGoroutine: %d\n", runtime.NumGoroutine()) | |
} | |
func doSomething(url int, wg *sync.WaitGroup) { | |
// Fetch URL | |
// TODO something... | |
time.Sleep(100 * time.Microsecond) | |
wg.Done() | |
} | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
urls := make(chan int, MAX) | |
total := 10000000 | |
count := 0 | |
var wg sync.WaitGroup | |
// Get urls | |
go func() { | |
for i := 0; i < total; i++ { | |
urls <- i | |
} | |
}() | |
fmt.Printf("Looking for fetch\n") | |
t := time.Tick(1 * time.Second) | |
for { | |
select { | |
case url := <-urls: | |
wg.Add(1) | |
go func(u int, wg *sync.WaitGroup) { | |
doSomething(u, wg) | |
}(url, &wg) | |
count++ | |
case <-t: | |
watching() | |
} | |
if count%MAX == 0 { | |
wg.Wait() | |
//fmt.Printf("N: %d ", count/MAX) | |
} | |
if count == total { | |
break | |
} | |
} | |
fmt.Printf("%d Done\n", count) | |
} |
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
package main | |
import ( | |
"fmt" | |
"runtime" | |
"time" | |
) | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
c := make(chan bool, 100) | |
t := time.Tick(time.Second) | |
go func() { | |
for { | |
select { | |
case <-t: | |
watching() | |
} | |
} | |
}() | |
for i := 0; i < 10000000; i++ { | |
c <- true | |
go worker(i, c) | |
} | |
fmt.Println("Done") | |
} | |
func watching() { | |
fmt.Printf("NumGoroutine: %d\n", runtime.NumGoroutine()) | |
} | |
func worker(i int, c chan bool) { | |
//fmt.Println("worker", i) | |
time.Sleep(100 * time.Microsecond) | |
<-c | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment