Last active
November 11, 2018 13:41
-
-
Save mamaz/d4b893ea5a6f5ff0d7d31910f96625ab to your computer and use it in GitHub Desktop.
Resize Image in Parallel
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 resizeparallel | |
import ( | |
"Exercise/ResizeImage/lib/resizer" | |
"Exercise/ResizeImage/lib/types" | |
"flag" | |
"fmt" | |
"os" | |
"runtime" | |
"time" | |
) | |
// ResizeParallel - Resize sequentially a file input, to several sizes | |
func ResizeParallel(input string, sizes []types.Size, maxprocs int) { | |
if maxprocs > 0 { | |
runtime.GOMAXPROCS(maxprocs) | |
} | |
start := time.Now() | |
if input == "" { | |
fmt.Printf("No input file \n") | |
flag.Usage() | |
os.Exit(0) | |
} | |
channel := make(chan types.ReturnValue) | |
for _, size := range sizes { | |
go resizer.ResizeImageNonBlocking( | |
input, | |
size.Width, | |
size.Height, | |
channel, | |
) | |
} | |
for i := 0; i < len(sizes); i++ { | |
returnValue := <-channel | |
if returnValue.Error != nil { | |
fmt.Printf("Error happens %v", returnValue.Error) | |
os.Exit(0) | |
} | |
fmt.Printf("\nImage resized %v\n", returnValue.FileName) | |
} | |
fmt.Println("\nResizeParallel time: ", time.Since(start)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment