[英]Go's sync.WaitGroup lost the one of the responses
我正在嘗試通過添加time.Sleep
內部goroutine來發送http請求。
但是, sync.WaitGroup
總是丟失一個響應,例如,此后客戶端向我的Web服務器發送了5個請求,但只有5個響應中有4個響應:
Sending http://localhost:9001/?id=1, at 2018-06-11 17:11:56.424086867 +0800 CST m=+0.000949479
Sending http://localhost:9001/?id=2, at 2018-06-11 17:11:57.426178028 +0800 CST m=+1.003040640
GOT id: 2 sleeping .... 0.347917120258, at: 2018-06-11 17:11:57.776187964 +0800 CST m=+1.353050576
GOT id: 1 sleeping .... 1.63133622383, at: 2018-06-11 17:11:58.059441646 +0800 CST m=+1.636304258
Sending http://localhost:9001/?id=3, at 2018-06-11 17:11:58.42641506 +0800 CST m=+2.003277672
GOT id: 3 sleeping .... 0.959551004983, at: 2018-06-11 17:11:59.392013618 +0800 CST m=+2.968876230
Sending http://localhost:9001/?id=4, at 2018-06-11 17:11:59.428900219 +0800 CST m=+3.005762831
GOT id: 4 sleeping .... 0.0479890727854, at: 2018-06-11 17:11:59.479683953 +0800 CST m=+3.056546565
Sending http://localhost:9001/?id=5, at 2018-06-11 17:12:00.428293512 +0800 CST m=+4.005156124
這是Go客戶端代碼
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"sync"
"time"
)
func main() {
urls := []string{
"http://localhost:9001/?id=1",
"http://localhost:9001/?id=2",
"http://localhost:9001/?id=3",
"http://localhost:9001/?id=4",
"http://localhost:9001/?id=5",
}
jsonResponses := make(chan string)
var wg sync.WaitGroup
wg.Add(len(urls))
for i, url := range urls {
tsleep := i
go func(url string) {
defer wg.Done()
time.Sleep(time.Duration(tsleep) * time.Second)
fmt.Println("Sending " + url + ", at " + time.Now().String())
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
} else {
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
} else {
t := time.Now()
jsonResponses <- string("GOT id: " + string(body) + ", at: " + t.String())
}
}
}(url)
}
go func() {
for response := range jsonResponses {
fmt.Println(response)
}
}()
wg.Wait()
}
用我的測試龍卷風python web服務器代碼
import tornado.ioloop
import tornado.web
import random
import tornado.gen
class DefaultHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def get(self):
id = self.get_query_argument("id", "1")
sleepy = 2.0 * (random.random())
self.write(id + " sleeping .... " + str(sleepy))
yield tornado.gen.sleep(sleepy)
self.finish()
def make_app():
return tornado.web.Application([
(r"/", DefaultHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(9001)
tornado.ioloop.IOLoop.current().start()
wg.Wait()
只會等到所有進行HTTP調用的goroutine完成,它不會等待打印結果的goroutine完成。 當所有HTTP調用完成(並且它們的結果在通道上發送)時, wg.Wait()
可能會返回,並且main()
函數結束。 有了它,你的應用程序也會結束。 它不會等待獨立的,並發的goroutine打印結果。
要使您的應用程序也等待,請使用第二個WaitGroup
或其他方法進行同步。 並且不要忘記在完成所有HTTP調用后關閉jsonResponses
通道,因為這將使打印goroutine結束(一旦所有值在關閉之前收到):
var wg2 sync.WaitGroup
wg2.Add(1)
go func() {
defer wg2.Done()
for response := range jsonResponses {
fmt.Println(response)
}
}()
wg.Wait()
// At this point HTTP calls are done.
// Close jsonResponses, signalling no more data will come:
close(jsonResponses)
wg2.Wait()
這里發生的是,一旦wg.Wait()
重新出現,我們就知道所有HTTP調用並且已經完成了他們的結果。 我們可以在這里關閉jsonResponses
。 一旦收到通道關閉之前發送的所有值,打印goroutine中的for range
循環將正確終止。 最后它將調用wg2.Done()
,因此wg2.Wait()
調用可以返回並且程序結束。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.