From 77e54d9f741c9f583cf84ebec63ee3b10af78b1d Mon Sep 17 00:00:00 2001 From: badkaktus Date: Tue, 8 Oct 2019 12:23:00 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B2=D0=BE=D1=80=D0=BA=D0=B5=D1=80=20=D0=BF?= =?UTF-8?q?=D1=83=D0=BB=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples.txt | 2 +- examples/worker-pools/worker-pools.go | 37 ++++++++++++++------------- examples/worker-pools/worker-pools.sh | 8 +++--- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/examples.txt b/examples.txt index 0e9c9cf..39b3ebd 100644 --- a/examples.txt +++ b/examples.txt @@ -31,7 +31,7 @@ Select Перебор значений из каналов (Range over Channels) Таймеры (Timers) Тикеры (повторения) (Tickers) -Worker Pools +Пулы воркеров (Worker Pools) WaitGroups Rate Limiting Atomic Counters diff --git a/examples/worker-pools/worker-pools.go b/examples/worker-pools/worker-pools.go index 1584d59..eded434 100644 --- a/examples/worker-pools/worker-pools.go +++ b/examples/worker-pools/worker-pools.go @@ -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 } diff --git a/examples/worker-pools/worker-pools.sh b/examples/worker-pools/worker-pools.sh index 10139fb..f60b6ec 100644 --- a/examples/worker-pools/worker-pools.sh +++ b/examples/worker-pools/worker-pools.sh @@ -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