воркер пулы

This commit is contained in:
badkaktus 2019-10-08 12:23:00 +03:00
parent c2562c4af9
commit 77e54d9f74
3 changed files with 24 additions and 23 deletions

View File

@ -31,7 +31,7 @@ Select
Перебор значений из каналов (Range over Channels)
Таймеры (Timers)
Тикеры (повторения) (Tickers)
Worker Pools
Пулы воркеров (Worker Pools)
WaitGroups
Rate Limiting
Atomic Counters

View File

@ -1,5 +1,6 @@
// In this example we'll look at how to implement
// a _worker pool_ using goroutines and channels.
// В этом примере мы рассмотрим, как реализовать
// _пул воркеров_ с использованием каналов и
// горутин.
package main
@ -8,11 +9,11 @@ import (
"time"
)
// Here's the worker, of which we'll run several
// concurrent instances. These workers will receive
// work on the `jobs` channel and send the corresponding
// results on `results`. We'll sleep a second per job to
// simulate an expensive task.
// Это воркер, который мы будем запускать в нескольких
// параллельных инстансах. Эти воркеры будут получать
// задания через канал `jobs` и отсылать результаты
// в `results`. Мы будем ожидать одну секунду для
// каждого задания для имитации тяжелого запроса.
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
fmt.Println("worker", id, "started job", j)
@ -24,29 +25,29 @@ func worker(id int, jobs <-chan int, results chan<- int) {
func main() {
// In order to use our pool of workers we need to send
// them work and collect their results. We make 2
// channels for this.
// Чтобы использовать наш воркер пул, нам нужно
// отправить им задание и получить результаты выполнения.
// Для этого мы делаем 2 канала.
jobs := make(chan int, 100)
results := make(chan int, 100)
// This starts up 3 workers, initially blocked
// because there are no jobs yet.
// Стартуем 3 воркера, первоначально заблокированных,
// т.к. еще нет заданий.
for w := 1; w <= 3; w++ {
go worker(w, jobs, results)
}
// Here we send 5 `jobs` and then `close` that
// channel to indicate that's all the work we have.
// Посылаем 5 `заданий (jobs)` и затем `закрываем`
// канал, сообщая о том что все задания отправлены.
for j := 1; j <= 5; j++ {
jobs <- j
}
close(jobs)
// Finally we collect all the results of the work.
// This also ensures that the worker goroutines have
// finished. An alternative way to wait for multiple
// goroutines is to use a [WaitGroup](waitgroups).
// Наконец мы собираем все результаты. Это также
// гарантирует, что горутины закончились. Альтернативный
// способ ожидания нескольких процедур заключается
// в использовании [WaitGroup](waitgroups).
for a := 1; a <= 5; a++ {
<-results
}

View File

@ -1,7 +1,7 @@
# Our running program shows the 5 jobs being executed by
# various workers. The program only takes about 2 seconds
# despite doing about 5 seconds of total work because
# there are 3 workers operating concurrently.
# Наша программа показывает 5 заданий, выполняемых
# различными воркерами. Программа занимает всего около
# 2 секунд, несмотря на выполнение около 5 секунд общей
# работы, потому что одновременно работают 3 воркера.
$ time go run worker-pools.go
worker 1 started job 1
worker 2 started job 2