feat: Improve the concurrency
This commit is contained in:
15
main.go
15
main.go
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -41,12 +42,21 @@ func run(filenames []string, op string, column int, out io.Writer) error {
|
||||
resCh := make(chan []float64)
|
||||
errCh := make(chan error)
|
||||
doneCh := make(chan struct{})
|
||||
filesCh := make(chan string)
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
go func() {
|
||||
defer close(filesCh)
|
||||
for _, fname := range filenames {
|
||||
filesCh <- fname
|
||||
}
|
||||
}()
|
||||
|
||||
for i := 0; i < runtime.NumCPU(); i++ {
|
||||
wg.Add(1)
|
||||
go func(fname string) {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for fname := range filesCh {
|
||||
f, err := os.Open(fname)
|
||||
if err != nil {
|
||||
errCh <- fmt.Errorf("cannot open file: %w", err)
|
||||
@@ -63,7 +73,8 @@ func run(filenames []string, op string, column int, out io.Writer) error {
|
||||
}
|
||||
|
||||
resCh <- data
|
||||
}(fname)
|
||||
}
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user