簡體   English   中英

如何使用 goroutine 池

[英]How to use a goroutine pool

我想使用 Go 從雅虎財經下載股票價格電子表格。 我將在自己的 goroutine 中為每只股票發出 http 請求。 我有一個大約 2500 個符號的列表,但與其並行發出 2500 個請求,我更喜歡一次發出 250 個請求。 在 Java 中,我會創建一個線程池並在線程空閑時重用它們。 我試圖找到類似的東西,一個 goroutine 池,如果你願意的話,但找不到任何資源。 如果有人能告訴我如何完成手頭的任務或為我指出相同的資源,我將不勝感激。 謝謝!

我想,最簡單的方法是創建 250 個 goroutine 並傳遞給它們一個通道,您可以使用該通道將鏈接從主 goroutine 傳遞到子 goroutine,並監聽該通道。

當所有鏈接都傳遞給 goroutine 時,您關閉一個通道,所有 goroutine 就完成了它們的工作。

為了在孩子處理數據之前完成主 goroutine 的安全,您可以使用sync.WaitGroup

下面是一些代碼來說明我上面所說的(不是最終的工作版本,而是說明了這一點):

func worker(linkChan chan string, wg *sync.WaitGroup) {
   // Decreasing internal counter for wait-group as soon as goroutine finishes
   defer wg.Done()

   for url := range linkChan {
     // Analyze value and do the job here
   }
}

func main() {
    lCh := make(chan string)
    wg := new(sync.WaitGroup)

    // Adding routines to workgroup and running then
    for i := 0; i < 250; i++ {
        wg.Add(1)
        go worker(lCh, wg)
    }

    // Processing all links by spreading them to `free` goroutines
    for _, link := range yourLinksSlice {
        lCh <- link
    }

    // Closing channel (waiting in goroutines won't continue any more)
    close(lCh)

    // Waiting for all goroutines to finish (otherwise they die as main routine dies)
    wg.Wait()
}

你可以在這個git repo中使用Go的線程池實現庫

是關於如何使用通道作為線程池的好博客

來自博客的片段

    var (
 MaxWorker = os.Getenv("MAX_WORKERS")
 MaxQueue  = os.Getenv("MAX_QUEUE")
)

//Job represents the job to be run
type Job struct {
    Payload Payload
}

// A buffered channel that we can send work requests on.
var JobQueue chan Job

// Worker represents the worker that executes the job
type Worker struct {
    WorkerPool  chan chan Job
    JobChannel  chan Job
    quit        chan bool
}

func NewWorker(workerPool chan chan Job) Worker {
    return Worker{
        WorkerPool: workerPool,
        JobChannel: make(chan Job),
        quit:       make(chan bool)}
}

// Start method starts the run loop for the worker, listening for a quit channel in
// case we need to stop it
func (w Worker) Start() {
    go func() {
        for {
            // register the current worker into the worker queue.
            w.WorkerPool <- w.JobChannel

            select {
            case job := <-w.JobChannel:
                // we have received a work request.
                if err := job.Payload.UploadToS3(); err != nil {
                    log.Errorf("Error uploading to S3: %s", err.Error())
                }

            case <-w.quit:
                // we have received a signal to stop
                return
            }
        }
    }()
}

// Stop signals the worker to stop listening for work requests.
func (w Worker) Stop() {
    go func() {
        w.quit <- true
    }()
} 

此示例使用兩個通道,一個用於輸入,另一個用於輸出。 Worker 可以擴展到任何大小,每個 goroutine 在輸入隊列上工作並將所有輸出保存到輸出通道。 非常歡迎對更簡單方法的反饋。

package main

import (
    "fmt"
    "sync"
)

var wg sync.WaitGroup

func worker(input chan string, output chan string) {
    defer wg.Done()
    // Consumer: Process items from the input channel and send results to output channel
    for value := range input {
        output <- value + " processed"
    }
}

func main() {
    var jobs = []string{"one", "two", "three", "four", "two", "three", "four", "two", "three", "four", "two", "three", "four", "two", "three", "four", "two"}
    input := make(chan string, len(jobs))
    output := make(chan string, len(jobs))
    workers := 250

    // Increment waitgroup counter and create go routines
    for i := 0; i < workers; i++ {
        wg.Add(1)
        go worker(input, output)
    }

    // Producer: load up input channel with jobs
    for _, job := range jobs {
        input <- job
    }

    // Close input channel since no more jobs are being sent to input channel
    close(input)
    // Wait for all goroutines to finish processing
    wg.Wait()
    // Close output channel since all workers have finished processing
    close(output)

    // Read from output channel
    for result := range output {
        fmt.Println(result)
    }

}

你可以看看這個

我們在 go 中創建了一個線程池,並將其用於我們的生產系統。

我從這里參考

它使用起來非常簡單,並且還有一個 prometheus 客戶端,可以告訴您使用了多少工人。

要初始化,只需創建一個調度程序實例

dispatcher = workerpool.NewDispatcher(
    "DispatcherName",
    workerpool.SetMaxWorkers(10),
)

創建一個實現此接口的對象(可以說是job )。 所以它應該實現 Process 方法

// IJob : Interface for the Job to be processed
type IJob interface {
    Process() error
}

然后只需將作業發送給調度員

dispatcher.JobQueue <- job //object of job

就是這個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM