diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2019-06-16 13:19:08 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2019-06-16 13:19:08 -0400 |
commit | e2962707f79ab00ad055e09127bb8ef0f2c45b02 (patch) | |
tree | 08c5f77155d7ac2ff93cc373ad1edeee66663a30 /autoutils/batches.go | |
parent | bac7ad4b50fca46b24cfc843ac18a1313e1064ec (diff) |
Diffstat (limited to 'autoutils/batches.go')
-rw-r--r-- | autoutils/batches.go | 49 |
1 files changed, 25 insertions, 24 deletions
diff --git a/autoutils/batches.go b/autoutils/batches.go index fb39fc1..7e27473 100644 --- a/autoutils/batches.go +++ b/autoutils/batches.go @@ -20,7 +20,7 @@ along with AutoArt. If not, see <https://www.gnu.org/licenses/>. package autoutils import ( - "fmt" + "fmt" ) /* @@ -31,26 +31,27 @@ a message will be printed, starting with progress, and showing how many batches have been completed so far out of the total number of batches. */ const batchSize = 32 -func RunInBatches(number int64, progress string, f func (n int64, errs chan<- error)) error { - nBatches := number / batchSize - errs := make(chan error) - for batch := int64(0); batch <= nBatches; batch++ { - fmt.Println(progress, batch+1, "/", nBatches+1) - thisBatchSize := batchSize - if batch == nBatches { - // Deal with case of last batch - thisBatchSize = int(number - nBatches * batchSize) - } - for task := 0; task < thisBatchSize; task++ { - go f(int64(task) + batchSize * batch, errs) - } - - for completed := 0; completed < thisBatchSize; completed++ { - err := <-errs - if err != nil { - return err - } - } - } - return nil -}
\ No newline at end of file + +func RunInBatches(number int64, progress string, f func(n int64, errs chan<- error)) error { + nBatches := number / batchSize + errs := make(chan error) + for batch := int64(0); batch <= nBatches; batch++ { + fmt.Println(progress, batch+1, "/", nBatches+1) + thisBatchSize := batchSize + if batch == nBatches { + // Deal with case of last batch + thisBatchSize = int(number - nBatches*batchSize) + } + for task := 0; task < thisBatchSize; task++ { + go f(int64(task)+batchSize*batch, errs) + } + + for completed := 0; completed < thisBatchSize; completed++ { + err := <-errs + if err != nil { + return err + } + } + } + return nil +} |