大佬们go并发有个问题想请教一下-关于1秒钟并发请求N个api的问题

xiaolaodi · · 1580 次点击
10楼 <a href="/user/didadi" title="@didadi">@didadi</a> 大哥 这样你能理解不,我需要生成100个数据,这100个数据中的每一个 又能生成1万条数据,我需要每秒秒并发请求这1万条数据中的10个。 ``` for i:=0;i&lt;10;i++{ //这里有1万条数据 zips := make([]string, 10000) //数据存入过程省略 //处理数据 for _, v := range zips { tmp[l] = v if (l == max-1) { //loop: for { select { case &lt;-tk.C: for i := 0; i &lt; 50; i++ { tmp_i := i go run(tmp[tmp_i], info) } quit &lt;- struct{}{} //break loop case &lt;-quit: // to receive error and quit return } } l = 0 tmp = make([] string, max) tmp[l] = v continue } l++ } } ``` 上面这个代码 会出现报错,数据竞争的问题? ``` ================== WARNING: DATA RACE Write at 0x00c4200ac018 by goroutine 17: flag.(*FlagSet).Parse() /usr/local/go/src/flag/flag.go:910 +0x40 flag.Parse() /usr/local/go/src/flag/flag.go:941 +0xbd lubi_data/common.Println() /Users/jack_lee/project/common/common.go:74 +0x4e main.run() /Users/jack_lee/project/singapore/main.go:158 +0x193 Previous write at 0x00c4200ac018 by goroutine 47: flag.(*FlagSet).Parse() /usr/local/go/src/flag/flag.go:910 +0x40 flag.Parse() /usr/local/go/src/flag/flag.go:941 +0xbd lubi_data/common.Println() /Users/jack_lee/project/common/common.go:74 +0x4e main.run() /Users/jack_lee/project/singapore/main.go:158 +0x193 Goroutine 17 (running) created at: main.gen_math() /Users/jack_lee/project/singapore/main.go:112 +0x6f6 main.main() /Users/jack_lee/project/singapore/main.go:217 +0x178 Goroutine 47 (finished) created at: main.gen_math() /Users/jack_lee/project/singapore/main.go:112 +0x6f6 main.main() /Users/jack_lee/project/singapore/main.go:217 +0x178 ================== ================== WARNING: DATA RACE Write at 0x00c4200ac030 by goroutine 17: flag.(*FlagSet).Parse() /usr/local/go/src/flag/flag.go:911 +0x5e flag.Parse() /usr/local/go/src/flag/flag.go:941 +0xbd lubi_data/common.Println() /Users/jack_lee/project/common/common.go:74 +0x4e main.run() /Users/jack_lee/project/singapore/main.go:158 +0x193 Previous write at 0x00c4200ac030 by goroutine 47: flag.(*FlagSet).Parse() /usr/local/go/src/flag/flag.go:911 +0x5e flag.Parse() /usr/local/go/src/flag/flag.go:941 +0xbd lubi_data/common.Println() /Users/jack_lee/project/common/common.go:74 +0x4e main.run() /Users/jack_lee/project/singapore/main.go:158 +0x193 Goroutine 17 (running) created at: main.gen_math() /Users/jack_lee/project/singapore/main.go:112 +0x6f6 main.main() /Users/jack_lee/project/singapore/main.go:217 +0x178 Goroutine 47 (finished) created at: main.gen_math() /Users/jack_lee/project/singapore/main.go:112 +0x6f6 main.main() /Users/jack_lee/project/singapore/main.go:217 +0x178 ================== ^C [21:05:42] jack_lee:singapore git:(master*) $ go build -race -o main main.go &amp;&amp; ./main ================== WARNING: DATA RACE Write at 0x00c4200a8018 by goroutine 29: flag.(*FlagSet).Parse() /usr/local/go/src/flag/flag.go:910 +0x40 flag.Parse() /usr/local/go/src/flag/flag.go:941 +0xbd lubi_data/common.Println() /Users/jack_lee/project/common/common.go:74 +0x4e main.run() /Users/jack_lee/project/singapore/main.go:159 +0x193 Previous write at 0x00c4200a8018 by goroutine 56: flag.(*FlagSet).Parse() /usr/local/go/src/flag/flag.go:910 +0x40 flag.Parse() /usr/local/go/src/flag/flag.go:941 +0xbd lubi_data/common.Println() /Users/jack_lee/project/common/common.go:74 +0x4e main.run() /Users/jack_lee/project/singapore/main.go:159 +0x193 Goroutine 29 (running) created at: main.gen_math() /Users/jack_lee/project/singapore/main.go:112 +0x5ce main.main() /Users/jack_lee/project/singapore/main.go:218 +0x178 Goroutine 56 (running) created at: main.gen_math() /Users/jack_lee/project/singapore/main.go:112 +0x5ce main.main() /Users/jack_lee/project/singapore/main.go:218 +0x178 ================== ================== WARNING: DATA RACE Write at 0x00c4200a8030 by goroutine 29: flag.(*FlagSet).Parse() /usr/local/go/src/flag/flag.go:911 +0x5e flag.Parse() /usr/local/go/src/flag/flag.go:941 +0xbd lubi_data/common.Println() /Users/jack_lee/project/common/common.go:74 +0x4e main.run() /Users/jack_lee/project/singapore/main.go:159 +0x193 Previous write at 0x00c4200a8030 by goroutine 56: flag.(*FlagSet).Parse() /usr/local/go/src/flag/flag.go:911 +0x5e flag.Parse() /usr/local/go/src/flag/flag.go:941 +0xbd lubi_data/common.Println() /Users/jack_lee/project/common/common.go:74 +0x4e main.run() /Users/jack_lee/project/singapore/main.go:159 +0x193 Goroutine 29 (running) created at: main.gen_math() /Users/jack_lee/project/singapore/main.go:112 +0x5ce main.main() /Users/jack_lee/project/singapore/main.go:218 +0x178 Goroutine 56 (running) created at: main.gen_math() /Users/jack_lee/project/singapore/main.go:112 +0x5ce main.main() /Users/jack_lee/project/singapore/main.go:218 +0x178 ================== ================== WARNING: DATA RACE Write at 0x00c4200a8018 by goroutine 21: flag.(*FlagSet).Parse() /usr/local/go/src/flag/flag.go:910 +0x40 flag.Parse() /usr/local/go/src/flag/flag.go:941 +0xbd lubi_data/common.Println() /Users/jack_lee/project/common/common.go:74 +0x4e main.run() /Users/jack_lee/project/singapore/main.go:159 +0x193 Previous write at 0x00c4200a8018 by goroutine 56: flag.(*FlagSet).Parse() /usr/local/go/src/flag/flag.go:910 +0x40 flag.Parse() /usr/local/go/src/flag/flag.go:941 +0xbd lubi_data/common.Println() /Users/jack_lee/project/common/common.go:74 +0x4e main.run() /Users/jack_lee/project/singapore/main.go:159 +0x193 Goroutine 21 (running) created at: main.gen_math() /Users/jack_lee/project/singapore/main.go:112 +0x5ce main.main() /Users/jack_lee/project/singapore/main.go:218 +0x178 Goroutine 56 (finished) created at: main.gen_math() /Users/jack_lee/project/singapore/main.go:112 +0x5ce main.main() /Users/jack_lee/project/singapore/main.go:218 +0x178 ================== ```
#11
更多评论
用go 协程去请求api
#1
哥们 我也这么想的,但是似乎不对。
#2