```
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()
}
```
这段代码运行一段时间后,内存回消耗光,然后停止运行。请问该如何优化呢
有疑问加站长微信联系(非本文作者)