mirror of
https://github.com/axllent/goptimize.git
synced 2024-12-21 17:46:23 -05:00
Add threaded option
This commit is contained in:
parent
3f9f09880c
commit
521f3f24c1
@ -1,5 +1,10 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## [dev]
|
||||||
|
|
||||||
|
- Add threaded option (`-t`) to use all CPU cores
|
||||||
|
|
||||||
|
|
||||||
## [0.1.0]
|
## [0.1.0]
|
||||||
|
|
||||||
- Switch to go mods - go (>= 1.11 required)
|
- Switch to go mods - go (>= 1.11 required)
|
||||||
|
@ -35,6 +35,7 @@ Options:
|
|||||||
-m, --max string downscale to a maximum width & height in pixels (<width>x<height>)
|
-m, --max string downscale to a maximum width & height in pixels (<width>x<height>)
|
||||||
-o, --out string output directory (default overwrites original)
|
-o, --out string output directory (default overwrites original)
|
||||||
-p, --preserve preserve file modification times (default true)
|
-p, --preserve preserve file modification times (default true)
|
||||||
|
-t, --threaded run multi-threaded (use all CPU cores)
|
||||||
-u, --update update to latest release
|
-u, --update update to latest release
|
||||||
-v, --version show version number
|
-v, --version show version number
|
||||||
-h, --help show help
|
-h, --help show help
|
||||||
|
35
main.go
35
main.go
@ -5,7 +5,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/axllent/ghru"
|
"github.com/axllent/ghru"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
@ -22,6 +24,7 @@ var (
|
|||||||
optipng string
|
optipng string
|
||||||
pngquant string
|
pngquant string
|
||||||
gifsicle string
|
gifsicle string
|
||||||
|
threads = 1
|
||||||
version = "dev"
|
version = "dev"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -51,13 +54,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var maxSizes string
|
var maxSizes string
|
||||||
var update, showversion, showhelp bool
|
var multiThreaded, update, showversion, showhelp bool
|
||||||
|
|
||||||
flag.IntVarP(&quality, "quality", "q", 75, "quality, JPEG only")
|
flag.IntVarP(&quality, "quality", "q", 75, "quality, JPEG only")
|
||||||
flag.StringVarP(&maxSizes, "max", "m", "", "downscale to a maximum width & height in pixels (<width>x<height>)")
|
flag.StringVarP(&maxSizes, "max", "m", "", "downscale to a maximum width & height in pixels (<width>x<height>)")
|
||||||
flag.StringVarP(&outputDir, "out", "o", "", "output directory (default overwrites original)")
|
flag.StringVarP(&outputDir, "out", "o", "", "output directory (default overwrites original)")
|
||||||
flag.BoolVarP(&preserveModTimes, "preserve", "p", true, "preserve file modification times")
|
flag.BoolVarP(&preserveModTimes, "preserve", "p", true, "preserve file modification times")
|
||||||
flag.BoolVarP(&update, "update", "u", false, "update to latest release")
|
flag.BoolVarP(&update, "update", "u", false, "update to latest release")
|
||||||
|
flag.BoolVarP(&multiThreaded, "threaded", "t", false, "run multi-threaded (use all CPU cores)")
|
||||||
flag.BoolVarP(&showversion, "version", "v", false, "show version number")
|
flag.BoolVarP(&showversion, "version", "v", false, "show version number")
|
||||||
flag.BoolVarP(&showhelp, "help", "h", false, "show help")
|
flag.BoolVarP(&showhelp, "help", "h", false, "show help")
|
||||||
|
|
||||||
@ -137,9 +141,34 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, img := range args {
|
if multiThreaded {
|
||||||
Goptimize(img)
|
threads = runtime.NumCPU()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
processChan := make(chan string)
|
||||||
|
|
||||||
|
wg := &sync.WaitGroup{} // Signal to main goroutine that worker goroutines are working/done working
|
||||||
|
wg.Add(threads)
|
||||||
|
|
||||||
|
for i := 0; i < threads; i++ {
|
||||||
|
go func() {
|
||||||
|
for nextFile := range processChan {
|
||||||
|
Goptimize(nextFile)
|
||||||
|
}
|
||||||
|
// Channel was closed, so we finished this goroutine.
|
||||||
|
wg.Done() // Let main goroutine know we are done.
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, img := range args {
|
||||||
|
processChan <- img
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the channel. This tells the worker goroutines that they can be done.
|
||||||
|
close(processChan)
|
||||||
|
|
||||||
|
// Wait for all worker goroutines to finish processing the IPs
|
||||||
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
// displayDelectedOptimizer prints whether the optimizer was found
|
// displayDelectedOptimizer prints whether the optimizer was found
|
||||||
|
Loading…
Reference in New Issue
Block a user