自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在 Go 語(yǔ)言中管理 Concurrency 的三種方式

開(kāi)發(fā) 前端
本章會(huì)介紹幾種方式來(lái)帶大家認(rèn)識(shí)并發(fā),而這三種方式分別對(duì)應(yīng)到三個(gè)不同的名詞:WaitGroup,Channel,及 Context。下面用簡(jiǎn)單的范例帶大家了解。

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

[[337382]]

WaitGroup

先來(lái)了解有什么情境需要使用到 WaitGroup,假設(shè)您有兩臺(tái)機(jī)器需要同時(shí)上傳最新的代碼,兩臺(tái)機(jī)器分別上傳完成后,才能執(zhí)行最后的重啟步驟。就像是把一個(gè)工作同時(shí)拆成好幾份同時(shí)一起做,可以減少時(shí)間,但是最后需要等到全部做完,才能執(zhí)行下一步,這時(shí)候就需要用到 WaitGroup 才能做到。

  1. package main 
  2.  
  3. import ( 
  4.     "fmt" 
  5.     "sync" 
  6.  
  7. func main() { 
  8.     var wg sync.WaitGroup 
  9.     i := 0 
  10.     wg.Add(3) //task count wait to do 
  11.     go func() { 
  12.         defer wg.Done() // finish task1 
  13.         fmt.Println("goroutine 1 done"
  14.         i++ 
  15.     }() 
  16.     go func() { 
  17.         defer wg.Done() // finish task2 
  18.         fmt.Println("goroutine 2 done"
  19.         i++ 
  20.     }() 
  21.     go func() { 
  22.         defer wg.Done() // finish task3 
  23.         fmt.Println("goroutine 3 done"
  24.         i++ 
  25.     }() 
  26.     wg.Wait() // wait for tasks to be done 
  27.     fmt.Println("all goroutine done"
  28.     fmt.Println(i) 

Channel

另外一種實(shí)際的案例就是,我們需要主動(dòng)通知一個(gè) Goroutine 進(jìn)行停止的動(dòng)作。換句話說(shuō),當(dāng) App 啟動(dòng)時(shí),會(huì)在后臺(tái)跑一些監(jiān)控程序,而當(dāng)整個(gè) App 需要停止前,需要發(fā)個(gè) Notification 給后臺(tái)的監(jiān)控程序,將其先停止,這時(shí)候就需要用到 Channel 來(lái)通知??聪孪旅孢@個(gè)例子:

  1. package main 
  2.  
  3. import ( 
  4.     "fmt" 
  5.     "time" 
  6.  
  7. func main() { 
  8.     exit := make(chan bool) 
  9.     go func() { 
  10.         for { 
  11.             select { 
  12.             case <-exit: 
  13.                 fmt.Println("Exit"
  14.                 return 
  15.             case <-time.After(2 * time.Second): 
  16.                 fmt.Println("Monitoring"
  17.             } 
  18.         } 
  19.     }() 
  20.     time.Sleep(5 * time.Second
  21.     fmt.Println("Notify Exit"
  22.     exit <- true //keep main goroutine alive 
  23.     time.Sleep(5 * time.Second

上面的例子可以發(fā)現(xiàn),用了一個(gè) Gogourtine 和 Channel 來(lái)控制??梢韵胂癞?dāng)后臺(tái)有無(wú)數(shù)個(gè) Goroutine 的時(shí)候,我們就需要用多個(gè) Channel 才能進(jìn)行控制,也許 Goroutine 內(nèi)又會(huì)產(chǎn)生 Goroutine,開(kāi)發(fā)者這時(shí)候就會(huì)發(fā)現(xiàn)已經(jīng)無(wú)法單純使用 Channel 來(lái)控制多個(gè) Goroutine 了。這時(shí)候解決方式會(huì)是傳遞 Context。

Context

大家可以想像,今天有一個(gè)后臺(tái)任務(wù) A,A 任務(wù)又產(chǎn)生了 B 任務(wù),B 任務(wù)又產(chǎn)生了 C 任務(wù),也就是可以按照此模式一直產(chǎn)生下去,假設(shè)中途我們需要停止 A 任務(wù),而 A 又必須告訴 B 及 C 要一起停止,這時(shí)候通過(guò) context 方式是最快的了。

 

  1. package main 
  2.  
  3. import ( 
  4.     "context" 
  5.     "fmt" 
  6.     "time" 
  7.  
  8. func foo(ctx context.Context, name string) { 
  9.     go bar(ctx, name) // A calls B 
  10.     for { 
  11.         select { 
  12.         case <-ctx.Done(): 
  13.             fmt.Println(name"A Exit"
  14.             return 
  15.         case <-time.After(1 * time.Second): 
  16.             fmt.Println(name"A do something"
  17.         } 
  18.     } 
  19.  
  20. func bar(ctx context.Context, name string) { 
  21.     for { 
  22.         select { 
  23.         case <-ctx.Done(): 
  24.             fmt.Println(name"B Exit"
  25.             return 
  26.         case <-time.After(2 * time.Second): 
  27.             fmt.Println(name"B do something"
  28.         } 
  29.     } 
  30.  
  31. func main() { 
  32.     ctx, cancel := context.WithCancel(context.Background()) 
  33.     go foo(ctx, "FooBar"
  34.     fmt.Println("client release connection, need to notify A, B exit"
  35.     time.Sleep(5 * time.Second
  36.     cancel() //mock client exit, and pass the signal, ctx.Done() gets the signal  time.Sleep(3 * time.Second
  37.     time.Sleep(3 * time.Second
  38.  
  39. package main 
  40.  
  41. import ( 
  42.     "context" 
  43.     "fmt" 
  44.     "time" 
  45.  
  46. func foo(ctx context.Context, name string) { 
  47.     go bar(ctx, name) // A calls B 
  48.     for { 
  49.         select { 
  50.         case <-ctx.Done(): 
  51.             fmt.Println(name"A Exit"
  52.             return 
  53.         case <-time.After(1 * time.Second): 
  54.             fmt.Println(name"A do something"
  55.         } 
  56.     } 
  57.  
  58. func bar(ctx context.Context, name string) { 
  59.     for { 
  60.         select { 
  61.         case <-ctx.Done(): 
  62.             fmt.Println(name"B Exit"
  63.             return 
  64.         case <-time.After(2 * time.Second): 
  65.             fmt.Println(name"B do something"
  66.         } 
  67.     } 
  68.  
  69. func main() { 
  70.     ctx, cancel := context.WithCancel(context.Background()) 
  71.     go foo(ctx, "FooBar"
  72.     fmt.Println("client release connection, need to notify A, B exit"
  73.     time.Sleep(5 * time.Second
  74.     cancel() //mock client exit, and pass the signal, ctx.Done() gets the signal  time.Sleep(3 * time.Second
  75.     time.Sleep(3 * time.Second

大家可以把 context 想成是一個(gè) controller,可以隨時(shí)控制不確定個(gè)數(shù)的 Goroutine,由上往下,只要宣告context.WithCancel后,再任意時(shí)間點(diǎn)都可以通過(guò)cancel()來(lái)停止整個(gè)后臺(tái)服務(wù)。實(shí)際案例會(huì)用在當(dāng) App 需要重新啟動(dòng)時(shí),要先通知全部 goroutine 停止,正常停止后,才會(huì)重新啟動(dòng) App。

總結(jié)

根據(jù)不同的情境跟狀況來(lái)選擇不同的方式,做一個(gè)總結(jié):

  • WaitGroup:需要將單一個(gè)工作分解成多個(gè)子任務(wù),等到全部完成后,才能進(jìn)行下一步,這時(shí)候用 WaitGroup 最適合了
  • Channel + Select:Channel 只能用在比較單純的 Goroutine 情況下,如果要管理多個(gè) Goroutine,建議還是 走 context 會(huì)比較適合
  • Context:如果您想一次控制全部的 Goroutine,相信用 context 會(huì)是最適合不過(guò)的。

 

責(zé)任編輯:未麗燕 來(lái)源: 今日頭條
相關(guān)推薦

2023-08-15 08:01:07

Go 語(yǔ)言排序

2022-05-31 16:00:46

Go 編程語(yǔ)言復(fù)制文件Go 標(biāo)準(zhǔn)庫(kù)

2012-07-17 09:16:16

SpringSSH

2024-05-10 08:36:40

Go語(yǔ)言對(duì)象

2023-12-19 16:43:01

2019-08-28 09:04:02

Go語(yǔ)言Python操作系統(tǒng)

2019-02-27 07:45:25

物聯(lián)網(wǎng)健康管理IOT

2023-03-28 07:46:46

go語(yǔ)言kubernetes

2014-12-31 17:42:47

LBSAndroid地圖

2021-06-24 08:52:19

單點(diǎn)登錄代碼前端

2021-11-05 21:33:28

Redis數(shù)據(jù)高并發(fā)

2019-11-20 18:52:24

物聯(lián)網(wǎng)智能照明智能恒溫器

2020-11-01 17:10:46

異步事件開(kāi)發(fā)前端

2022-06-26 06:31:25

Linux電子游戲

2010-03-12 17:52:35

Python輸入方式

2014-04-09 09:32:24

Go并發(fā)

2024-07-08 09:03:31

2017-07-14 15:07:23

2010-09-13 12:19:03

2021-11-26 11:07:14

cowsay命令Linux
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)