Created
June 12, 2017 21:44
-
-
Save greut/c4cc1310131af9b4b939a136fbdbd695 to your computer and use it in GitHub Desktop.
Testing parallel processing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"gopkg.in/h2non/bimg.v1" | |
"os" | |
"runtime" | |
"strconv" | |
"sync" | |
) | |
func main() { | |
fmt.Printf("libvips: %#v\n", bimg.VipsVersion) | |
fmt.Printf("bimg: %#v\n", bimg.Version) | |
filename := os.Args[1] | |
quantity, err := strconv.Atoi(os.Args[2]) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "Specify the quantity as int.\n") | |
return | |
} | |
var waitGroup sync.WaitGroup | |
waitGroup.Add(quantity) | |
fmt.Printf("MAXPROCS: %#d\n", runtime.GOMAXPROCS(quantity)) | |
defer waitGroup.Wait() | |
for i := 0; i < quantity; i++ { | |
go func(id int) { | |
defer waitGroup.Done() | |
resize(id, filename) | |
}(i) | |
} | |
} | |
func resize(id int, filename string) { | |
fmt.Printf("> %#d\n", id) | |
buffer, err := bimg.Read(filename) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
} | |
newImage, err := bimg.NewImage(buffer).Process(bimg.Options{ | |
Type: bimg.WEBP, | |
Width: 1000, | |
Height: 1000, | |
Crop: true, | |
}) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
} | |
bimg.Write(fmt.Sprintf("%#d-%#s.webp", id, filename), newImage) | |
fmt.Printf("< %#d\n", id) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment