```
package main
import (
"xiyu/models"
"runtime"
"sync"
"github.com/astaxie/beego/orm"
)
var limit = make(chan struct{}, 10)
func getInfos(url,proxy string,cat_id int,goods_thumb string,id int) {
limit <- struct{}{}
defer func() {
<-limit
}()
info :=models.Get_Goods_Info("http://www.ehsy.com/"+url,"not")
if len(info.Goods_name) > 5 {
//insert collect info to mysql
info.Cat_id = cat_id
info.Parent_id = id
info.Url = url
info.Old_thumb = goods_thumb
orm.NewOrm().Insert(&info)
//update the goods url status in mysql
detail_url_model := new(models.GoodsUrl)
detail_url_model.Id = id
detail_url_model.Status = 1
detail_url_model.Update("Status")
}
}
func main() {
NCPU := runtime.NumCPU()
runtime.GOMAXPROCS(NCPU)
models.Init()
goods_url := new(models.GoodsUrl)
var goods_urls []*models.GoodsUrl
count ,_ := goods_url.Query().Filter("status",0).Count() //get total url count
var wg sync.WaitGroup
for i:=0;i<=int(count)/100;i++ {
wg.Add(1)
goods_url.Query().Filter("status",0).Limit(100,i*100).All(&goods_urls,"Cat_id","Url","Id","Img") //get 100 urls
for _,v :=range goods_urls {
go func(url string,proxy string ,cat_id int,img string,id int) {
getInfos(url,proxy,cat_id,img,id) //execute collect action
}(v.Url,"",v.Cat_id,v.Img,v.Id)
}
wg.Done()
}
wg.Wait()
}
```
这段代码运行一段时间后,内存回消耗光,然后停止运行。请问该如何优化呢
wg.Add(1) 改到 for _,v :=range goods_urls { 下面。
wg.Done() 改成 defer wg.Done() 放在go func(url string,proxy string ,cat_id int,img string,id int) { 下。
wg.Wait() 改到原代码中 wg.Done()的位置。
limit应该可以去了。
#10
更多评论