```
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()
}
```
这段代码运行一段时间后,内存回消耗光,然后停止运行。请问该如何优化呢
不懂怎么帖代码,上个图,也没几行代码。你分别尝试下两个main函数。如果第一个每过大约3秒的时间,输出100个数字。第二个运行之后崩溃的话。把你的功能代码初步添加到第一个main中。
![111.png](http://studygolang.qiniudn.com/150415/ba28aa56e03c3d13580bca1a12bd07e2.png)
#15
更多评论