golang协程问题

ecshop528 2014-12-09 09:12:13
我需要采集一些URL
这样写。我能采集我需要的完整的内容。
写法1:

func main() {
models.Init()
for j:=390;j>0;j-- {
content ,err := models.Get_cate_urls("http://bbs.ecshop.com/forum-96-"+strconv.Itoa(j)+".html")
if (err !=nil) {
fmt.Println(err)
}
url_model := new(models.Urls)
for _,j :=range content {
url_model.Url = j
if url_model.Read("url") !=nil {
url_model.Status = 1
url_model.Insert()
} else {
fmt.Println(" have")
}

}
}

}


写法2
但是我修改成以下协程方式,采集的数据,只是上面的一半。采集的内容不完整
func geturls(j int ,c chan bool) {
//获取分类的里面的所有详细页面url
content ,err := models.Get_cate_urls("http://bbs.ecshop.com/forum-96-"+strconv.Itoa(j)+".html")
if (err !=nil) {
fmt.Println(err)
}
//数据库orm初始化
url_model := new(models.Urls)
for _,j :=range content {//循环插入每个分类采集的详细页面的url
url_model.Url = j
if url_model.Read("url") !=nil {
//如果采集的URL在数据库中不存在。就写入
url_model.Status = 1
url_model.Insert()
} else {
//如果数据库中存在,就跳过这条记录
fmt.Println(" have")
}
}
c<-true
}

func main() {
models.Init()
c := make(chan bool,390)
for j:=390;j>0;j-- {
//循环采集390个分类页面的详细url
go geturls(j,c)
}
for j:=390;j>0;j-- {
u:= <-c
fmt.Println(u)
}
close(c)
}


请问是怎么造成呢,我用php写了一次,采集的结果和方法1是一样的。只有方法2采集的数据。少了一部分
...全文
1671 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2016-01-07
  • 打赏
  • 举报
回复
写入的时候没有考虑多个协程的并发执行。
晴空闲雲 2014-12-12
  • 打赏
  • 举报
回复
从程序来看没啥大问题,估计是你并发去抓取内容被对方限制掉了。

2,190

社区成员

发帖
与我相关
我的任务
社区描述
go语言学习与交流版
社区管理员
  • go语言社区
  • Freeman Z
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧