Skip to content

Instantly share code, notes, and snippets.

@Rayer
Created May 27, 2020 15:59
Show Gist options
  • Save Rayer/6740296cbbdda01d9ea3073807e179b3 to your computer and use it in GitHub Desktop.
Save Rayer/6740296cbbdda01d9ea3073807e179b3 to your computer and use it in GitHub Desktop.
GO Routine loop
func ParseRangeDocumentAsync(board string, start int, end int) (ret []*PDocRaw) {
parseChannel := make(chan *PDocRaw, 2)
var count int
docUrlList := make([]string, 0)
_ = IterateDocuments(board, start, end, func(docUrl string) {
docUrlList = append(docUrlList, docUrl)
})
for _, docUrl := range docUrlList {
count++
docUrl := docUrl
go func() {
workerChannel := make(chan *PDocRaw, 1)
timer := time.NewTimer(5 * time.Second)
go func() {
p, _ := ParseSingleRawDocument(docUrl)
timer.Stop()
workerChannel <- p
}()
select {
case res := <-workerChannel:
parseChannel <- res
case <-timer.C:
parseChannel <- &PDocRaw{
Title: "Time out....",
}
}
}()
}
ret = make([]*PDocRaw, 0, 0)
for i := 0; i < count; i++ {
select {
case p := <-parseChannel:
fmt.Printf("(%d/%d)Completed parsed : %s with committer count %d\n", i+1, count, p.Title, len(p.CommitterInfoList))
ret = append(ret, p)
}
}
return ret
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment