1. 首页
  2. 主题
  3. Go问与答

go并发的问题

netaxcess · · 3465 次点击
package main import ( "yangsheng/models" //"fmt" "runtime" //"time" //"os" ) func getinfo(id int,url string,a,b chan bool) { a<-true isok:=false defer func() { b <-isok }() info:=models.GetInfo(url) if len(info["Title"]) > 0 { var inmodel models.Info var durls models.Deurls inmodel.Url = info["Url"] if inmodel.Read("title") !=nil { inmodel.Title = info["Title"] inmodel.Content = info["Content"] _,err :=inmodel.Insert() if err == nil { durls.Id = id durls.Status = 1 durls.Update("Status") } else { } } } else { } isok=true } func main() { NCPU := runtime.NumCPU() runtime.GOMAXPROCS(NCPU) models.Init() details :=new(models.Deurls) var list []*models.Deurls a :=make(chan bool,10) b :=make(chan bool,1) for i:=0;i<10;i++ { details.Query().OrderBy("-id").Limit(i*2000,2000).All(&list) for _,v := range list { go getinfo(v.Id,v.Url,a,b) } } for j:=0;j<20000;j++ { <-b <-a } close(a) close(b) } 我这段代码,是用来采集数据的。总共有2万条数据。运行一会,他就提示以下错误。
感觉有点乱啊,循环的次数20000是怎么出来的,如果并发碰到问题的话,最简单的办法就是改成不并发的。
#8
更多评论
polaris
社区,需要你我一同完善!
错误信息呢?
#1
created by main.main E:/golang/src/yangsheng/info.go:48 +0x2b7 goroutine 3032 [chan send, 5 minutes]: main.getinfo(0x1d198, 0x1151e1c0, 0x35, 0x11030140, 0x11038000) E:/golang/src/yangsheng/info.go:11 +0x3d created by main.main E:/golang/src/yangsheng/info.go:48 +0x2b7 goroutine 3033 [chan send, 5 minutes]: main.getinfo(0x1d197, 0x1151e2c0, 0x37, 0x11030140, 0x11038000) E:/golang/src/yangsheng/info.go:11 +0x3d created by main.main E:/golang/src/yangsheng/info.go:48 +0x2b7 goroutine 3034 [chan send, 5 minutes]: main.getinfo(0x1d196, 0x1151e3c0, 0x36, 0x11030140, 0x11038000) E:/golang/src/yangsheng/info.go:11 +0x3d created by main.main E:/golang/src/yangsheng/info.go:48 +0x2b7 goroutine 3035 [chan send, 5 minutes]: main.getinfo(0x1d195, 0x1151e4c0, 0x34, 0x11030140, 0x11038000) E:/golang/src/yangsheng/info.go:11 +0x3d created by main.main 以下是错误信息
#2

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中