限制並發Go 程式的數量
問題:
您打算處理URL 清單同時使用,但同時執行的goroutine 數量是預先定義的。在這種情況下,儘管有 30 個 URL,但您希望同時將並行化限制為 10 個 goroutine。
解決方案:
解決此問題的關鍵在於修改您的程式碼的架構。不要為每個 URL 產生單獨的 goroutine,而是產生有限數量的工作 goroutine 來使用來自共享通道的 URL。此通道的緩衝性質將調節並發性。
程式碼修改:
以下是包含此方法的程式碼的更新版本:
<code class="go">package main import ( "flag" "fmt" "os" "sync" "time" ) func main() { parallel := flag.Int("parallel", 10, "max parallel requests allowed") flag.Parse() urls := flag.Args() // Create a buffered channel to buffer URLs urlsChan := make(chan string, *parallel) // Create a separate goroutine to feed URLs into the channel go func() { for _, u := range urls { urlsChan <- u } // Close the channel to indicate that there are no more URLs to process close(urlsChan) }() var wg sync.WaitGroup client := rest.Client{} results := make(chan string) // Start the specified number of worker goroutines for i := 0; i < *parallel; i++ { wg.Add(1) go func() { defer wg.Done() // Continuously retrieve URLs from the channel until it is closed for url := range urlsChan { worker(url, client, results) } }() } // Launch a separate goroutine to close the results channel when all workers are finished go func() { // Wait for all workers to finish processing URLs wg.Wait() // Close the results channel to signal that there are no more results close(results) }() // Read results from the channel until it is closed for res := range results { fmt.Println(res) } os.Exit(0) }</code>
在此修改後的程式碼中:
透過利用這種修改後的架構,可以根據指定的並行度限制,有效調控並發執行的goroutine數量。
以上是在利用 goroutine 處理 URL 清單時,如何限制並發 Go 例程的數量?的詳細內容。更多資訊請關注PHP中文網其他相關文章!