第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 goroutine 池

如何使用 goroutine 池

Go
紅糖糍粑 2021-06-21 09:05:04
我想使用 Go 從雅虎財經下載股票價格電子表格。我將在自己的 goroutine 中為每只股票發(fā)出 http 請求。我有一個大約 2500 個符號的列表,但與其并行發(fā)出 2500 個請求,我更喜歡一次發(fā)出 250 個請求。在 Java 中,我會創(chuàng)建一個線程池并在線程空閑時重用它們。我試圖找到類似的東西,一個 goroutine 池,如果你愿意的話,但找不到任何資源。如果有人能告訴我如何完成手頭的任務或為我指出相同的資源,我將不勝感激。謝謝!
查看完整描述

3 回答

?
DIEA

TA貢獻1820條經驗 獲得超2個贊

我想,最簡單的方法是創(chuàng)建 250 個 goroutine 并將它們傳遞給一個通道,您可以使用該通道將鏈接從主 goroutine 傳遞到子 goroutine,并監(jiān)聽該通道。


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


為了在孩子處理數(shù)據(jù)之前完成主 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()

}


查看完整回答
反對 回復 2021-06-28
?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

你可以使用Go這個git repo 中的線程池實現(xiàn)庫


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


來自博客的片段


    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

    }()


查看完整回答
反對 回復 2021-06-28
  • 3 回答
  • 0 關注
  • 218 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號