新聞中心
相信大家踏入 Go 語言的世界,肯定是被強(qiáng)大的并發(fā)(Concurrency)所吸引,Go 語言用最簡單的關(guān)鍵字go就可以將任務(wù)丟到后臺處理,但是開發(fā)者怎么有效率的控制并發(fā),這是入門 Go 語言必學(xué)的技能,本章會介紹幾種方式來帶大家認(rèn)識并發(fā),而這三種方式分別對應(yīng)到三個(gè)不同的名詞:WaitGroup,Channel,及 Context。下面用簡單的范例帶大家了解。

10年的達(dá)坂城網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營銷的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整達(dá)坂城建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)從事“達(dá)坂城網(wǎng)站設(shè)計(jì)”,“達(dá)坂城網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
WaitGroup
先來了解有什么情境需要使用到 WaitGroup,假設(shè)您有兩臺機(jī)器需要同時(shí)上傳最新的代碼,兩臺機(jī)器分別上傳完成后,才能執(zhí)行最后的重啟步驟。就像是把一個(gè)工作同時(shí)拆成好幾份同時(shí)一起做,可以減少時(shí)間,但是最后需要等到全部做完,才能執(zhí)行下一步,這時(shí)候就需要用到 WaitGroup 才能做到。
- package main
- import (
- "fmt"
- "sync"
- )
- func main() {
- var wg sync.WaitGroup
- i := 0
- wg.Add(3) //task count wait to do
- go func() {
- defer wg.Done() // finish task1
- fmt.Println("goroutine 1 done")
- i++
- }()
- go func() {
- defer wg.Done() // finish task2
- fmt.Println("goroutine 2 done")
- i++
- }()
- go func() {
- defer wg.Done() // finish task3
- fmt.Println("goroutine 3 done")
- i++
- }()
- wg.Wait() // wait for tasks to be done
- fmt.Println("all goroutine done")
- fmt.Println(i)
- }
Channel
另外一種實(shí)際的案例就是,我們需要主動通知一個(gè) Goroutine 進(jìn)行停止的動作。換句話說,當(dāng) App 啟動時(shí),會在后臺跑一些監(jiān)控程序,而當(dāng)整個(gè) App 需要停止前,需要發(fā)個(gè) Notification 給后臺的監(jiān)控程序,將其先停止,這時(shí)候就需要用到 Channel 來通知??聪孪旅孢@個(gè)例子:
- package main
- import (
- "fmt"
- "time"
- )
- func main() {
- exit := make(chan bool)
- go func() {
- for {
- select {
- case <-exit:
- fmt.Println("Exit")
- return
- case <-time.After(2 * time.Second):
- fmt.Println("Monitoring")
- }
- }
- }()
- time.Sleep(5 * time.Second)
- fmt.Println("Notify Exit")
- exit <- true //keep main goroutine alive
- time.Sleep(5 * time.Second)
- }
上面的例子可以發(fā)現(xiàn),用了一個(gè) Gogourtine 和 Channel 來控制??梢韵胂癞?dāng)后臺有無數(shù)個(gè) Goroutine 的時(shí)候,我們就需要用多個(gè) Channel 才能進(jìn)行控制,也許 Goroutine 內(nèi)又會產(chǎn)生 Goroutine,開發(fā)者這時(shí)候就會發(fā)現(xiàn)已經(jīng)無法單純使用 Channel 來控制多個(gè) Goroutine 了。這時(shí)候解決方式會是傳遞 Context。
Context
大家可以想像,今天有一個(gè)后臺任務(wù) A,A 任務(wù)又產(chǎn)生了 B 任務(wù),B 任務(wù)又產(chǎn)生了 C 任務(wù),也就是可以按照此模式一直產(chǎn)生下去,假設(shè)中途我們需要停止 A 任務(wù),而 A 又必須告訴 B 及 C 要一起停止,這時(shí)候通過 context 方式是最快的了。
- package main
- import (
- "context"
- "fmt"
- "time"
- )
- func foo(ctx context.Context, name string) {
- go bar(ctx, name) // A calls B
- for {
- select {
- case <-ctx.Done():
- fmt.Println(name, "A Exit")
- return
- case <-time.After(1 * time.Second):
- fmt.Println(name, "A do something")
- }
- }
- }
- func bar(ctx context.Context, name string) {
- for {
- select {
- case <-ctx.Done():
- fmt.Println(name, "B Exit")
- return
- case <-time.After(2 * time.Second):
- fmt.Println(name, "B do something")
- }
- }
- }
- func main() {
- ctx, cancel := context.WithCancel(context.Background())
- go foo(ctx, "FooBar")
- fmt.Println("client release connection, need to notify A, B exit")
- time.Sleep(5 * time.Second)
- cancel() //mock client exit, and pass the signal, ctx.Done() gets the signal time.Sleep(3 * time.Second)
- time.Sleep(3 * time.Second)
- }
- package main
- import (
- "context"
- "fmt"
- "time"
- )
- func foo(ctx context.Context, name string) {
- go bar(ctx, name) // A calls B
- for {
- select {
- case <-ctx.Done():
- fmt.Println(name, "A Exit")
- return
- case <-time.After(1 * time.Second):
- fmt.Println(name, "A do something")
- }
- }
- }
- func bar(ctx context.Context, name string) {
- for {
- select {
- case <-ctx.Done():
- fmt.Println(name, "B Exit")
- return
- case <-time.After(2 * time.Second):
- fmt.Println(name, "B do something")
- }
- }
- }
- func main() {
- ctx, cancel := context.WithCancel(context.Background())
- go foo(ctx, "FooBar")
- fmt.Println("client release connection, need to notify A, B exit")
- time.Sleep(5 * time.Second)
- cancel() //mock client exit, and pass the signal, ctx.Done() gets the signal time.Sleep(3 * time.Second)
- time.Sleep(3 * time.Second)
- }
大家可以把 context 想成是一個(gè) controller,可以隨時(shí)控制不確定個(gè)數(shù)的 Goroutine,由上往下,只要宣告context.WithCancel后,再任意時(shí)間點(diǎn)都可以通過cancel()來停止整個(gè)后臺服務(wù)。實(shí)際案例會用在當(dāng) App 需要重新啟動時(shí),要先通知全部 goroutine 停止,正常停止后,才會重新啟動 App。
總結(jié)
根據(jù)不同的情境跟狀況來選擇不同的方式,做一個(gè)總結(jié):
- WaitGroup:需要將單一個(gè)工作分解成多個(gè)子任務(wù),等到全部完成后,才能進(jìn)行下一步,這時(shí)候用 WaitGroup 最適合了
- Channel + Select:Channel 只能用在比較單純的 Goroutine 情況下,如果要管理多個(gè) Goroutine,建議還是 走 context 會比較適合
- Context:如果您想一次控制全部的 Goroutine,相信用 context 會是最適合不過的。
文章標(biāo)題:在 Go 語言中管理 Concurrency 的三種方式
標(biāo)題路徑:http://fisionsoft.com.cn/article/dhdoopd.html


咨詢
建站咨詢
