淺析Go語(yǔ)言中的同步與異步處理
同步處理
在同步處理方式中,任務(wù)按順序一個(gè)接一個(gè)地執(zhí)行。每個(gè)任務(wù)必須在下一個(gè)任務(wù)開(kāi)始之前完成。這意味著如果某個(gè)任務(wù)需要花費(fèi)大量時(shí)間來(lái)完成,它可能會(huì)阻塞后續(xù)任務(wù)的執(zhí)行,導(dǎo)致潛在的性能瓶頸。
一個(gè)簡(jiǎn)單的現(xiàn)實(shí)生活中的例子是兩個(gè)人在喝啤酒時(shí)進(jìn)行對(duì)話(huà)。一個(gè)人說(shuō)一些話(huà)并提問(wèn),另一個(gè)人根據(jù)情況回應(yīng),然后反過(guò)來(lái)...
在下面的示例中,每個(gè)URL調(diào)用必須完成其整個(gè)請(qǐng)求-響應(yīng)周期并提供響應(yīng)或錯(cuò)誤,以便可以進(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ù)獨(dú)立并同時(shí)執(zhí)行。這意味著程序在一個(gè)任務(wù)完成之前不會(huì)等待它繼續(xù)下一個(gè)任務(wù)。在Golang中,可以使用Goroutines和Go運(yùn)行時(shí)來(lái)實(shí)現(xiàn)異步編程。
一個(gè)簡(jiǎn)單的現(xiàn)實(shí)生活中的例子是去汽車(chē)修理店。一旦工程師處理完其他任務(wù),他們會(huì)處理你的任務(wù)。在此期間,你可以做其他重要的事情,直到你的汽車(chē)被取走并修好。
在下面的示例中,每個(gè)URL調(diào)用將通過(guò)goroutine在自己的線(xiàn)程中進(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 !!
如果我們?cè)谇衅刑砑痈嗟腢RL并進(jìn)行更多的HTTP get請(qǐng)求,比較兩種方式的性能。
到此這篇關(guān)于淺析Go語(yǔ)言中的同步與異步處理的文章就介紹到這了,更多相關(guān)Go同步與異步處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go集成swagger實(shí)現(xiàn)在線(xiàn)接口文檔的教程指南
wagger是一個(gè)用于設(shè)計(jì),構(gòu)建和文檔化API的開(kāi)源框架,在Go語(yǔ)言中,Swagger可以幫助后端開(kāi)發(fā)人員快速創(chuàng)建和定義RESTful API,并提供自動(dòng)生成接口文檔的功能,所以本文給大家介紹了Go集成swagger實(shí)現(xiàn)在線(xiàn)接口文檔的方法,需要的朋友可以參考下2024-11-11
prometheus?client_go為應(yīng)用程序自定義監(jiān)控指標(biāo)
這篇文章主要為大家介紹了prometheus?client_go為應(yīng)用程序自定義監(jiān)控指標(biāo)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Go語(yǔ)言Elasticsearch數(shù)據(jù)清理工具思路詳解
這篇文章主要介紹了Go語(yǔ)言Elasticsearch數(shù)據(jù)清理工具思路詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10
Golang實(shí)現(xiàn)自定義recovery中間件
在?Golang?的?Web?項(xiàng)目中,自定義?recovery?中間件是一種常見(jiàn)的做法,用于捕獲并處理應(yīng)用程序的運(yùn)行時(shí)錯(cuò)誤,下面我們就來(lái)看看具體如何實(shí)現(xiàn)吧2023-09-09
Golang查詢(xún)MongoDB的實(shí)現(xiàn)步驟
本文介紹了如何使用Golang操作MongoDB數(shù)據(jù)庫(kù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-12-12
gorm RowsAffected()返回0的問(wèn)題及解決
在gorm中,`RowsAffected()`方法用于獲取更新操作的受影響行數(shù),如果在執(zhí)行更新操作后立即調(diào)用`RowsAffected()`,可能會(huì)得到0,因?yàn)樵摲椒ㄔ趦?nèi)部已經(jīng)執(zhí)行了數(shù)據(jù)庫(kù)操作并更新了數(shù)據(jù),正確的使用方法是在執(zhí)行更新操作后,通過(guò)返回的`db`對(duì)象來(lái)獲取受影響的行數(shù)2025-12-12

