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

Go并發(fā)模式:管道和顯式取消

開發(fā) 開發(fā)工具
Go并發(fā)原語使得構(gòu)建流式數(shù)據(jù)管道,高效利用I/O和多核變得簡單。這篇文章介紹了幾個管道例子,重點指出在操作失敗時的細(xì)微差別,并介紹了優(yōu)雅處理失敗的技術(shù)。

引言

Go并發(fā)原語使得構(gòu)建流式數(shù)據(jù)管道,高效利用I/O和多核變得簡單。這篇文章介紹了幾個管道例子,重點指出在操作失敗時的細(xì)微差別,并介紹了優(yōu)雅處理失敗的技術(shù)。

什么是管道?

Go沒有正式的管道定義。管道只是眾多并發(fā)程序的一類。一般的,一個管道就是一些列的由channel連接起來的階段。每個階段都有執(zhí)行相同邏輯的goroutine。在每個階段中,goroutine

  • 從channel讀取上游數(shù)據(jù)
  • 在數(shù)據(jù)上執(zhí)行一些操作,通常會產(chǎn)生新的數(shù)據(jù)
  • 通過channel將數(shù)據(jù)發(fā)往下游

每個階段都可以有任意個輸入channel和輸出channel,除了第一個和最有一個channel(只有輸入channel或只有輸出channel)。第一個步驟通常叫數(shù)據(jù)源或者生產(chǎn)者,最后一個叫做存儲池或者消費者。

我們先從一個簡單的管道例子來解釋這些概念和技術(shù),稍后我們會介紹一個更為復(fù)雜的例子。

數(shù)字的平方

假設(shè)管道有三個階段。

第一步,gen函數(shù),是一個將數(shù)字列表轉(zhuǎn)換到一個channel中的函數(shù)。Gen函數(shù)啟動了一個goroutine,將數(shù)字發(fā)送到channel,并在所有數(shù)字都發(fā)送完后關(guān)閉channel。

  1. func gen(nums ...int) <-chan int {  
  2.     out := make(chan int)  
  3.     go func() {  
  4.         for _, n := range nums {  
  5.             out <- n  
  6.         }  
  7.         close(out)  
  8.     }()  
  9.     return out  

第二個階段,sq,從上面的channel接收數(shù)字,并返回一個包含所有收到數(shù)字的平方的channel。在上游channel關(guān)閉后,這個階段已經(jīng)往下游發(fā)送完所有的結(jié)果,然后關(guān)閉輸出channel:

  1. func sq(in <-chan int) <-chan int {  
  2.     out := make(chan int)  
  3.     go func() {  
  4.         for n := range in {  
  5.             out <- n * n  
  6.         }  
  7.         close(out)  
  8.     }()  
  9.     return out  

main函數(shù)建立這個管道,并執(zhí)行第一個階段,從第二個階段接收結(jié)果并逐個打印,直到channel被關(guān)閉。

  1. func main() {  
  2.     // Set up the pipeline.  
  3.     c := gen(23)  
  4.     out := sq(c)  
  5.    
  6.     // Consume the output.  
  7.     fmt.Println(<-out) // 4  
  8.     fmt.Println(<-out) // 9  

因為sq對輸入channel和輸出channel擁有相同的類型,我們可以任意次的組合他們。我們也可以像其他階段一樣,將main函數(shù)重寫成一個循環(huán)遍歷。

  1. func main() {  
  2.     // Set up the pipeline and consume the output.  
  3.     for n := range sq(sq(gen(23))) {  
  4.         fmt.Println(n) // 16 then 81  
  5.     }  

扇出扇入(Fan-out, fan-in)

多個函數(shù)可以從同一個channel讀取數(shù)據(jù),直到這個channel關(guān)閉,這叫扇出。這是一種多個工作實例分布式地協(xié)作以并行利用CPU和I/O的方式。

一個函數(shù)可以從多個輸入讀取并處理數(shù)據(jù),直到所有的輸入channel都被關(guān)閉。這個函數(shù)會將所有輸入channel導(dǎo)入一個單一的channel。這個單一的channel在所有輸入channel都關(guān)閉后才會關(guān)閉。這叫做扇入。

我們可以設(shè)置我們的管道執(zhí)行兩個sq實例,每一個實例都從相同的輸入channel讀取數(shù)據(jù)。我們引入了一個新的函數(shù),merge,來扇入結(jié)果:

  1. func main() {  
  2.     in := gen(23)  
  3.    
  4.     // Distribute the sq work across two goroutines that both read from in.  
  5.     c1 := sq(in)  
  6.     c2 := sq(in)  
  7.    
  8.     // Consume the merged output from c1 and c2.  
  9.     for n := range merge(c1, c2) {  
  10.         fmt.Println(n) // 4 then 9, or 9 then 4  
  11.     }  

merge函數(shù)為每一個輸入channel啟動一個goroutine,goroutine將數(shù)據(jù)拷貝到同一個輸出channel。這樣就將多個channel轉(zhuǎn)換成一個channel。一旦所有的output goroutine啟動起來,merge就啟動另一個goroutine,在所有輸入拷貝完畢后關(guān)閉輸出channel。
向一個關(guān)閉了的channel發(fā)送數(shù)據(jù)會觸發(fā)異常,所以在調(diào)用close之前確認(rèn)所有的發(fā)送動作都執(zhí)行完畢很重要。sync.WaitGroup類型為這種同步提供了一種簡便的方法:

  1. func merge(cs ...<-chan int) <-chan int {  
  2.     var wg sync.WaitGroup  
  3.     out := make(chan int)  
  4.    
  5.     // Start an output goroutine for each input channel in cs.  output  
  6.     // copies values from c to out until c is closed, then calls wg.Done.  
  7.     output := func(c <-chan int) {  
  8.         for n := range c {  
  9.             out <- n  
  10.         }  
  11.         wg.Done()  
  12.     }  
  13.     wg.Add(len(cs))  
  14.     for _, c := range cs {  
  15.         go output(c)  
  16.     }  
  17.    
  18.     // Start a goroutine to close out once all the output goroutines are  
  19.     // done.  This must start after the wg.Add call.  
  20.     go func() {  
  21.         wg.Wait()  
  22.         close(out)  
  23.     }()  
  24.     return out  

停止的藝術(shù)

我們所有的管道函數(shù)都遵循一種模式:

  • 發(fā)送者在發(fā)送完畢時關(guān)閉其輸出channel。
  • 接收者持續(xù)從輸入管道接收數(shù)據(jù)直到輸入管道關(guān)閉。

這種模式使得每一個接收函數(shù)都能寫成一個range循環(huán),保證所有的goroutine在數(shù)據(jù)成功發(fā)送到下游后就關(guān)閉。

但是在真實的案例中,并不是所有的輸入數(shù)據(jù)都需要被接收處理。有些時候是故意這么設(shè)計的:接收者可能只需要數(shù)據(jù)的子集就夠了;或者更一般的,因為輸入數(shù)據(jù)有錯誤而導(dǎo)致接收函數(shù)提早退出。上面任何一種情況下,接收者都不應(yīng)該繼續(xù)等待后續(xù)的數(shù)據(jù)到來,并且我們希望上游函數(shù)停止生成后續(xù)步驟已經(jīng)不需要的數(shù)據(jù)。

在我們的管道例子中,如果一個階段無法消費所有的輸入數(shù)據(jù),那些發(fā)送這些數(shù)據(jù)的goroutine就會一直阻塞下去:

  1.     // Consume the first value from output.  
  2.     out := merge(c1, c2)  
  3.     fmt.Println(<-out) // 4 or 9  
  4.     return 
  5.     // Since we didn't receive the second value from out,  
  6.     // one of the output goroutines is hung attempting to send it.  

這是一種資源泄漏:goroutine會占用內(nèi)存和運行時資源。goroutine棧持有的堆引用會阻止GC回收資源。而且goroutine不能被垃圾回收,必須主動退出。

我們必須重新設(shè)計管道中的上游函數(shù),在下游函數(shù)無法接收所有輸入數(shù)據(jù)時退出。一種方法就是讓輸出channel擁有一定的緩存。緩存可以存儲一定數(shù)量的數(shù)據(jù)。如果緩存空間足夠,發(fā)送操作就會馬上返回:

  1. c := make(chan int2// buffer size 2  
  2. c <- 1  // succeeds immediately  
  3. c <- 2  // succeeds immediately  
  4. c <- 3  // blocks until another goroutine does <-c and receives 1 

如果在channel創(chuàng)建時就知道需要發(fā)送數(shù)據(jù)的數(shù)量,帶緩存的channel會簡化代碼。例如,我們可以重寫gen函數(shù),拷貝一系列的整數(shù)到一個帶緩存的channel而不是創(chuàng)建一個新的goroutine:

  1. func gen(nums ...int) <-chan int {  
  2.     out := make(chan int, len(nums))  
  3.     for _, n := range nums {  
  4.         out <- n  
  5.     }  
  6.     close(out)  
  7.     return out  

反過來我們看管道中被阻塞的goroutine,我們可以考慮為merge函數(shù)返回的輸出channel增加一個緩存:

  1. func merge(cs ...<-chan int) <-chan int {  
  2.     var wg sync.WaitGroup  
  3.     out := make(chan int1// enough space for the unread inputs  
  4.     // ... the rest is unchanged ... 

雖然這樣可以避免了程序中g(shù)oroutine的阻塞,但這是很爛的代碼。選擇緩存大小為1取決于知道m(xù)erge函數(shù)接收數(shù)字的數(shù)量和下游函數(shù)消費數(shù)字的數(shù)量。這是很不穩(wěn)定的:如果我們向gen多發(fā)送了一個數(shù)據(jù),或者下游函數(shù)少消費了數(shù)據(jù),我們就又一次阻塞了goroutine。

然而,我們需要提供一種方式,下游函數(shù)可以通知上游發(fā)送者下游要停止接收數(shù)據(jù)。

#p#

顯式取消

當(dāng)main函數(shù)決定在沒有從out接收所有的數(shù)據(jù)而要退出時,它需要通知上游的goroutine取消即將發(fā)送的數(shù)據(jù)??梢酝ㄟ^向一個叫做done的channel發(fā)送數(shù)據(jù)來實現(xiàn)。因為有兩個潛在阻塞的goroutine,main函數(shù)會發(fā)送兩個數(shù)據(jù):

  1. func main() {  
  2.     in := gen(23)  
  3.    
  4.     // Distribute the sq work across two goroutines that both read from in.  
  5.     c1 := sq(in)  
  6.     c2 := sq(in)  
  7.    
  8.     // Consume the first value from output.  
  9.     done := make(chan struct{}, 2)  
  10.     out := merge(done, c1, c2)  
  11.     fmt.Println(<-out) // 4 or 9  
  12.    
  13.     // Tell the remaining senders we're leaving.  
  14.     done <- struct{}{}  
  15.     done <- struct{}{}  

對發(fā)送goroutine而言,需要將發(fā)送操作替換為一個select語句,要么out發(fā)生發(fā)送操作,要么從done接收數(shù)據(jù)。done的數(shù)據(jù)類型是空的struct,因為其值無關(guān)緊要:僅僅表示out需要取消發(fā)送操作。output 繼續(xù)在輸入channel循環(huán)執(zhí)行,因此上游函數(shù)是不會阻塞的。(接下來我們會討論如何讓循環(huán)提早退出)

  1. func merge(done <-chan struct{}, cs ...<-chan int) <-chan int {  
  2.     var wg sync.WaitGroup  
  3.     out := make(chan int)  
  4.    
  5.     // Start an output goroutine for each input channel in cs.  output  
  6.     // copies values from c to out until c is closed or it receives a value  
  7.     // from done, then output calls wg.Done.  
  8.     output := func(c <-chan int) {  
  9.         for n := range c {  
  10.             select {  
  11.             case out <- n:  
  12.             case <-done:  
  13.             }  
  14.         }  
  15.         wg.Done()  
  16.     }  
  17.     // ... the rest is unchanged ... 

這種方法有一個問題:每一個下游函數(shù)需要知道潛在可能阻塞的上游發(fā)送者的數(shù)量,以發(fā)送響應(yīng)的信號讓其提早退出。跟蹤這些數(shù)量是無趣的而且很容易出錯。

我們需要一種能夠讓未知或無界數(shù)量的goroutine都能夠停止向下游發(fā)送數(shù)據(jù)的方法。在Go中,我們可以通過關(guān)閉一個channel實現(xiàn)。因為從一個關(guān)閉了的channel執(zhí)行接收操作總能馬上成功,并返回相應(yīng)數(shù)據(jù)類型的零值。

這意味著main函數(shù)僅通過關(guān)閉done就能實現(xiàn)將所有的發(fā)送者解除阻塞。關(guān)閉操作是一個高效的對發(fā)送者的廣播信號。我們擴展管道中所有的函數(shù)接受done作為一個參數(shù),并通過defer來實現(xiàn)相應(yīng)channel的關(guān)閉操作。因此,無論main函數(shù)在哪一行退出都會通知上游退出。

  1. func main() {  
  2.     // Set up a done channel that's shared by the whole pipeline,  
  3.     // and close that channel when this pipeline exits, as a signal  
  4.     // for all the goroutines we started to exit.  
  5.     done := make(chan struct{})  
  6.     defer close(done)  
  7.    
  8.     in := gen(done, 23)  
  9.    
  10.     // Distribute the sq work across two goroutines that both read from in.  
  11.     c1 := sq(done, in)  
  12.     c2 := sq(done, in)  
  13.    
  14.     // Consume the first value from output.  
  15.     out := merge(done, c1, c2)  
  16.     fmt.Println(<-out) // 4 or 9  
  17.    
  18.     // done will be closed by the deferred call.  

現(xiàn)在每一個管道函數(shù)在done被關(guān)閉后就可以馬上返回了。merge函數(shù)中的output可以在接收管道的數(shù)據(jù)消費完之前返回,因為output函數(shù)知道上游發(fā)送者sq會在done關(guān)閉后停止產(chǎn)生數(shù)據(jù)。同時,output通過defer語句保證wq.Done會在所有退出路徑上調(diào)用。

  1. func merge(done <-chan struct{}, cs ...<-chan int) <-chan int {  
  2.     var wg sync.WaitGroup  
  3.     out := make(chan int)  
  4.    
  5.     // Start an output goroutine for each input channel in cs.  output  
  6.     // copies values from c to out until c or done is closed, then calls  
  7.     // wg.Done.  
  8.     output := func(c <-chan int) {  
  9.         defer wg.Done()  
  10.         for n := range c {  
  11.             select {  
  12.             case out <- n:  
  13.             case <-done:  
  14.                 return 
  15.             }  
  16.         }  
  17.     }  
  18.     // ... the rest is unchanged ... 

類似的,sq也可以在done關(guān)閉后馬上返回。sq通過defer語句使得任何退出路徑都能關(guān)閉其輸出channel out。

  1. func sq(done <-chan struct{}, in <-chan int) <-chan int {  
  2.     out := make(chan int)  
  3.     go func() {  
  4.         defer close(out)  
  5.         for n := range in {  
  6.             select {  
  7.             case out <- n * n:  
  8.             case <-done:  
  9.                 return 
  10.             }  
  11.         }  
  12.     }()  
  13.     return out  

管道構(gòu)建的指導(dǎo)思想如下:

  • 每一個階段在所有發(fā)送操作完成后關(guān)閉輸出channel。
  • 每一個階段持續(xù)從輸入channel接收數(shù)據(jù)直到輸入channel被關(guān)閉或者生產(chǎn)者被解除阻塞(譯者:生產(chǎn)者退出)。

管道解除生產(chǎn)者阻塞有兩種方法:要么保證有足夠的緩存空間存儲將要被生產(chǎn)的數(shù)據(jù),要么顯式的通知生產(chǎn)者消費者要取消接收數(shù)據(jù)。

樹形摘要

讓我們來看一個更為實際的管道。

MD5是一個信息摘要算法,對于文件校驗非常有用。命令行工具md5sum很有用,可以打印一系列文件的摘要值。

  1. % md5sum *.go  
  2. d47c2bbc28298ca9befdfbc5d3aa4e65  bounded.go  
  3. ee869afd31f83cbb2d10ee81b2b831dc  parallel.go  
  4. b88175e65fdcbc01ac08aaf1fd9b5e96  serial.go 

我們的例子程序和md5sum類似,但是接受一個單一的文件夾作為參數(shù),打印該文件夾下每一個普通文件的摘要值,并按路徑名稱排序。

  1. % go run serial.go .  
  2. d47c2bbc28298ca9befdfbc5d3aa4e65  bounded.go  
  3. ee869afd31f83cbb2d10ee81b2b831dc  parallel.go  
  4. b88175e65fdcbc01ac08aaf1fd9b5e96  serial.go 

我們程序的main函數(shù)調(diào)用一個工具函數(shù)MD5ALL,該函數(shù)返回一個從路徑名稱到摘要值的哈希表,然后排序并輸出結(jié)果:

  1. func main() {  
  2.     // Calculate the MD5 sum of all files under the specified directory,  
  3.     // then print the results sorted by path name.  
  4.     m, err := MD5All(os.Args[1])  
  5.     if err != nil {  
  6.         fmt.Println(err)  
  7.         return 
  8.     }  
  9.     var paths []string  
  10.     for path := range m {  
  11.         paths = append(paths, path)  
  12.     }  
  13.     sort.Strings(paths)  
  14.     for _, path := range paths {  
  15.         fmt.Printf("%x  %s\n", m[path], path)  
  16.     }  

MD5ALL是我們討論的核心。在 serial.go中,沒有采用任何并發(fā),僅僅遍歷文件夾,讀取文件并求出摘要值。

  1. // MD5All reads all the files in the file tree rooted at root and returns a map  
  2. // from file path to the MD5 sum of the file's contents.  If the directory walk  
  3. // fails or any read operation fails, MD5All returns an error.  
  4. func MD5All(root string) (map[string][md5.Size]byte, error) {  
  5.     m := make(map[string][md5.Size]byte)  
  6.     err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {  
  7.         if err != nil {  
  8.             return err  
  9.         }  
  10.         if info.IsDir() {  
  11.             return nil  
  12.         }  
  13.         data, err := ioutil.ReadFile(path)  
  14.         if err != nil {  
  15.             return err  
  16.         }  
  17.         m[path] = md5.Sum(data)  
  18.         return nil  
  19.     })  
  20.     if err != nil {  
  21.         return nil, err  
  22.     }  
  23.     return m, nil  

#p#

并行摘要求值

在parallel.go中,我們將MD5ALL分成兩階段的管道。第一個階段,sumFiles,遍歷文件夾,每個文件一個goroutine進行求摘要值,然后將結(jié)果發(fā)送一個數(shù)據(jù)類型為result的channel中:

  1. type result struct {  
  2.     path string  
  3.     sum  [md5.Size]byte 
  4.     err  error  

sumFiles 返回兩個channel:一個用于生成結(jié)果,一個用于filepath.Walk返回錯誤。Walk函數(shù)為每一個普通文件啟動一個goroutine,然后檢查done,如果done被關(guān)閉,walk馬上就會退出。

  1. func sumFiles(done <-chan struct{}, root string) (<-chan result, <-chan error) {  
  2.     // For each regular file, start a goroutine that sums the file and sends  
  3.     // the result on c.  Send the result of the walk on errc.  
  4.     c := make(chan result)  
  5.     errc := make(chan error, 1)  
  6.     go func() {  
  7.         var wg sync.WaitGroup  
  8.         err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {  
  9.             if err != nil {  
  10.                 return err  
  11.             }  
  12.             if info.IsDir() {  
  13.                 return nil  
  14.             }  
  15.             wg.Add(1)  
  16.             go func() {  
  17.                 data, err := ioutil.ReadFile(path)  
  18.                 select {  
  19.                 case c <- result{path, md5.Sum(data), err}:  
  20.                 case <-done:  
  21.                 }  
  22.                 wg.Done()  
  23.             }()  
  24.             // Abort the walk if done is closed.  
  25.             select {  
  26.             case <-done:  
  27.                 return errors.New("walk canceled")  
  28.             default:  
  29.                 return nil  
  30.             }  
  31.         })  
  32.         // Walk has returned, so all calls to wg.Add are done.  Start a  
  33.         // goroutine to close c once all the sends are done.  
  34.         go func() {  
  35.             wg.Wait()  
  36.             close(c)  
  37.         }()  
  38.         // No select needed here, since errc is buffered.  
  39.         errc <- err  
  40.     }()  
  41.     return c, errc  

MD5All 從c中接收摘要值。MD5All 在遇到錯誤時提前退出,通過defer關(guān)閉done。

  1. func MD5All(root string) (map[string][md5.Size]byte, error) {  
  2.     // MD5All closes the done channel when it returns; it may do so before  
  3.     // receiving all the values from c and errc.  
  4.     done := make(chan struct{})  
  5.     defer close(done)  
  6.    
  7.     c, errc := sumFiles(done, root)  
  8.    
  9.     m := make(map[string][md5.Size]byte)  
  10.     for r := range c {  
  11.         if r.err != nil {  
  12.             return nil, r.err  
  13.         }  
  14.         m[r.path] = r.sum  
  15.     }  
  16.     if err := <-errc; err != nil {  
  17.         return nil, err  
  18.     }  
  19.     return m, nil  

有界并行

parallel.go中實現(xiàn)的MD5ALL,對每一個文件啟動了一個goroutine。在一個包含大量大文件的文件夾中,這會導(dǎo)致超過機器可用內(nèi)存的內(nèi)存分配。(譯者注:即發(fā)生OOM)

我們可以通過限制讀取文件的并發(fā)度來避免這種情況發(fā)生。在bounded.go中,我們通過創(chuàng)建一定數(shù)量的goroutine讀取文件?,F(xiàn)在我們的管道現(xiàn)在有三個階段:遍歷文件夾,讀取文件并計算摘要值,收集摘要值。

第一個階段,walkFiles,輸出文件夾中普通文件的文件路徑:

  1. func walkFiles(done <-chan struct{}, root string) (<-chan string, <-chan error) {  
  2.     paths := make(chan string)  
  3.     errc := make(chan error, 1)  
  4.     go func() {  
  5.         // Close the paths channel after Walk returns.  
  6.         defer close(paths)  
  7.         // No select needed for this send, since errc is buffered.  
  8.         errc <- filepath.Walk(root, func(path string, info os.FileInfo, err error) error {  
  9.             if err != nil {  
  10.                 return err  
  11.             }  
  12.             if info.IsDir() {  
  13.                 return nil  
  14.             }  
  15.             select {  
  16.             case paths <- path:  
  17.             case <-done:  
  18.                 return errors.New("walk canceled")  
  19.             }  
  20.             return nil  
  21.         })  
  22.     }()  
  23.     return paths, errc  

中間的階段啟動一定數(shù)量的digester goroutine,從paths接收文件名稱,并向c發(fā)送result結(jié)構(gòu):

  1. func digester(done <-chan struct{}, paths <-chan string, c chan<- result) {  
  2.     for path := range paths {  
  3.         data, err := ioutil.ReadFile(path)  
  4.         select {  
  5.         case c <- result{path, md5.Sum(data), err}:  
  6.         case <-done:  
  7.             return 
  8.         }  
  9.     }  

和前一個例子不同,digester并不關(guān)閉其輸出channel,因為輸出channel是共享的,多個goroutine會向同一個channel發(fā)送數(shù)據(jù)。MD5All 會在所有的digesters 結(jié)束后關(guān)閉響應(yīng)的channel。

  1. // Start a fixed number of goroutines to read and digest files.  
  2. c := make(chan result)  
  3. var wg sync.WaitGroup  
  4. const numDigesters = 20 
  5. wg.Add(numDigesters)  
  6. for i := 0; i < numDigesters; i++ {  
  7.     go func() {  
  8.         digester(done, paths, c)  
  9.         wg.Done()  
  10.     }()  
  11. }  
  12. go func() {  
  13.     wg.Wait()  
  14.     close(c)  
  15. }() 

我們也可以讓每一個digester創(chuàng)建并返回自己的輸出channel,但如果這樣的話,我們需要額外的goroutine來扇入這些結(jié)果。
最后一個階段從c中接收所有的result數(shù)據(jù),并從errc中檢查錯誤。這種檢查不能在之前的階段做,因為在這之前,walkFiles 可能被阻塞不能往下游發(fā)送數(shù)據(jù):

  1.     m := make(map[string][md5.Size]byte)  
  2.     for r := range c {  
  3.         if r.err != nil {  
  4.             return nil, r.err  
  5.         }  
  6.         m[r.path] = r.sum  
  7.     }  
  8.     // Check whether the Walk failed.  
  9.     if err := <-errc; err != nil {  
  10.         return nil, err  
  11.     }  
  12.     return m, nil  

結(jié)論

這篇文章介紹了如果用Go構(gòu)建流式數(shù)據(jù)管道的技術(shù)。在這樣的管道中處理錯誤有點取巧,因為管道中每一個階段可能被阻塞不能往下游發(fā)送數(shù)據(jù),下游階段可能已經(jīng)不關(guān)心輸入數(shù)據(jù)。我們展示了關(guān)閉channel如何向所有管道啟動的goroutine廣播一個done信號,并且定義了正確構(gòu)建管道的指導(dǎo)思想。

深入閱讀:

• Go并發(fā)模式(視頻)展示了Go并發(fā)原語的基本概念和幾個實現(xiàn)的方法

• 高級Go并發(fā)模式(視頻)包含幾個更為復(fù)雜的Go并發(fā)原語的使用,尤其是select

• Douglas McIlroy的Squinting at Power Series論文展示了類似Go的并發(fā)模式如何為復(fù)雜的計算提供優(yōu)雅的支持。

原文鏈接: Golang - Sameer Ajmani   翻譯: 伯樂在線 - Codefor

譯文鏈接: http://blog.jobbole.com/65552/

責(zé)任編輯:林師授 來源: 伯樂在線
相關(guān)推薦

2023-12-29 08:10:41

Go并發(fā)開發(fā)

2013-05-28 09:43:38

GoGo語言并發(fā)模式

2022-04-24 15:29:17

微服務(wù)go

2024-01-29 00:35:00

Go并發(fā)開發(fā)

2021-11-28 22:33:01

Go選項模式

2024-12-03 12:02:05

2025-03-24 00:25:00

Go語言并發(fā)編程

2023-08-08 07:18:17

協(xié)程管道函數(shù)

2019-12-24 16:52:22

Go語言騰訊TM函數(shù)

2014-07-21 09:22:40

GoAPI

2022-10-17 08:07:13

Go 語言并發(fā)編程

2024-06-17 08:40:16

2021-11-10 15:18:16

JavaGo命令

2023-05-04 08:47:31

命令模式抽象接口

2023-04-10 09:20:13

設(shè)計模式訪客模式

2018-09-10 08:45:04

Linux管道命令

2021-02-20 20:36:56

Linux無名管道

2025-03-27 00:45:00

2020-09-16 12:18:28

GoJava模式

2021-09-30 09:21:28

Go語言并發(fā)編程
點贊
收藏

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