淺析Go語言中的同步與異步處理
同步處理
在同步處理方式中,任務(wù)按順序一個接一個地執(zhí)行。每個任務(wù)必須在下一個任務(wù)開始之前完成。這意味著如果某個任務(wù)需要花費大量時間來完成,它可能會阻塞后續(xù)任務(wù)的執(zhí)行,導(dǎo)致潛在的性能瓶頸。
一個簡單的現(xiàn)實生活中的例子是兩個人在喝啤酒時進(jìn)行對話。一個人說一些話并提問,另一個人根據(jù)情況回應(yīng),然后反過來...
在下面的示例中,每個URL調(diào)用必須完成其整個請求-響應(yīng)周期并提供響應(yīng)或錯誤,以便可以進(jìn)行后續(xù)的URL調(diào)用。
package main import ( "fmt" "net/http" ) func makeUrlCall(url string) { _, err := http.Get(url) if err != nil { fmt.Println("Got error in connecting to url: ", url) } fmt.Println("Got the response from our url: ", url) } func main() { fmt.Println("Welcome here !!") fmt.Println() //slice of urls urlSlice := []string{ "https://www.baidu.com", "https://www.csdn.net", "https://www.runoob.com", } for idx, url := range urlSlice { fmt.Println("Calling url on index: ", idx) makeUrlCall(url) } fmt.Println() fmt.Println("End of sync processing !!") return }
輸出:
Welcome here !!
Calling url on index: 0
Got the response from our url: https://www.baidu.com
Calling url on index: 1
Got the response from our url: https://www.csdn.net
Calling url on index: 2
Got the response from our url: https://www.runoob.com
End of sync processing !!
異步處理
在異步處理方式中,任務(wù)獨立并同時執(zhí)行。這意味著程序在一個任務(wù)完成之前不會等待它繼續(xù)下一個任務(wù)。在Golang中,可以使用Goroutines和Go運行時來實現(xiàn)異步編程。
一個簡單的現(xiàn)實生活中的例子是去汽車修理店。一旦工程師處理完其他任務(wù),他們會處理你的任務(wù)。在此期間,你可以做其他重要的事情,直到你的汽車被取走并修好。
在下面的示例中,每個URL調(diào)用將通過goroutine在自己的線程中進(jìn)行,并根據(jù)需要處理響應(yīng)。
package main import ( "fmt" "net/http" "sync" ) func makeUrlCall(url string) { _, err := http.Get(url) if err != nil { fmt.Println("Got error in connecting to url: ", url) } fmt.Println("Got the response from our url: ", url) } func main() { fmt.Println("Welcome here !!") fmt.Println() //slice of urls urlSlice := []string{ "https://www.baidu.com", "https://www.csdn.net", "https://www.runoob.com", } var wg sync.WaitGroup for _, u := range urlSlice { wg.Add(1) //all the url's to get error/response are called in their own separate thread via goroutines go func(url string) { defer wg.Done() makeUrlCall(url) }(u) } wg.Wait() fmt.Println() fmt.Println("End of sync processing !!") return }
輸出:
Welcome here !!
Got the response from our url: https://www.baidu.com
Got the response from our url: https://www.runoob.com
Got the response from our url: https://www.csdn.net
End of sync processing !!
如果我們在切片中添加更多的URL并進(jìn)行更多的HTTP get請求,比較兩種方式的性能。
到此這篇關(guān)于淺析Go語言中的同步與異步處理的文章就介紹到這了,更多相關(guān)Go同步與異步處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
簡單聊聊Go語言中空結(jié)構(gòu)體和空字符串的特殊之處
在日常的編程過程中,大家應(yīng)該經(jīng)常能遇到各種”空“吧,比如空指針、空結(jié)構(gòu)體、空字符串等,本文就以?Go?語言為例,一起來看看空結(jié)構(gòu)體和空字符串在?Go?語言中的特殊之處吧2024-03-03Go語言學(xué)習(xí)之golang-jwt/jwt的教程分享
jwt是?json?web?token的簡稱。go使用jwt目前,主流使用的jwt庫是golang-jwt/jwt。本文就來和大家講講golang-jwt/jwt的具體使用,需要的可以參考一下2023-01-01