首頁 > 後端開發 > Golang > 在利用 goroutine 處理 URL 清單時,如何限制並發 Go 例程的數量?

在利用 goroutine 處理 URL 清單時,如何限制並發 Go 例程的數量?

Mary-Kate Olsen
發布: 2024-10-31 11:31:02
原創
552 人瀏覽過

How can you limit the number of concurrent Go routines when processing a list of URLs while utilizing goroutines?

限制並發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>
登入後複製

在此修改後的程式碼中:

  1. 建立一個緩衝通道urlsChan 來保存要處理的URL。緩衝區大小設定為 *parallel,有效限制了可以同時存取通道的 goroutine 數量。
  2. 一個單獨的 goroutine 專門用於使用 URL 填充 urlsChan 通道。
  3. worker goroutine持續使用 urlsChan 通道中的 URL,直到關閉。
  4. 一旦所有工作人員完成其任務,就會使用一個單獨的 Goroutine 來關閉結果通道。

透過利用這種修改後的架構,可以根據指定的並行度限制,有效調控並發執行的goroutine數量。

以上是在利用 goroutine 處理 URL 清單時,如何限制並發 Go 例程的數量?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板