Browse Source

Added concurrency

main
Unbewohnte 4 years ago committed by GitHub
parent
commit
39f0753b87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 47
      backgroundchanger.go

47
backgroundchanger.go

@ -10,11 +10,16 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"time"
) )
var ( var (
// used as a flag if the program executed for "the first time" // used as a flag if the program executed for "the first time"
settingsFileExisted bool = false settingsFileExisted bool = false
WG sync.WaitGroup
maxWorkers uint = 50
) )
const ( const (
@ -247,6 +252,17 @@ func createBlackBG(width, height int) {
log.Println("Successfully created black background") log.Println("Successfully created black background")
} }
// a basic implementation of a concurrent worker
func worker(paths <-chan string, replacementImage string, successful, failed *uint64, WG *sync.WaitGroup) {
defer WG.Done()
for songPath := range paths {
s, f := replaceBackgrounds(songPath, replacementImage)
*successful += s
*failed += f
}
}
func init() { func init() {
setUpLogs() setUpLogs()
createSettingsFile() createSettingsFile()
@ -257,6 +273,7 @@ func main() {
if !settingsFileExisted { if !settingsFileExisted {
return return
} }
startingTime := time.Now().UTC()
settings := getSettings() settings := getSettings()
@ -279,21 +296,31 @@ func main() {
} }
// storing all paths to each beatmap // storing all paths to each beatmap
var songPaths []string songPaths := make(chan string, len(osuSongsDirContents))
for _, content := range osuSongsDirContents { for _, songDir := range osuSongsDirContents {
if content.IsDir() { if songDir.IsDir() {
songPaths = append(songPaths, filepath.Join(osuSongsDir, content.Name())) songPaths <- filepath.Join(osuSongsDir, songDir.Name())
} }
} }
log.Printf("Found %d song folders", len(songPaths)) log.Printf("Found %d song folders", len(songPaths))
// replacing backgrounds for each beatmap // check if there is less job than workers
if int(maxWorkers) > len(songPaths) {
maxWorkers = uint(len(songPaths))
}
// replacing backgrounds for each beatmap concurrently
var successful, failed uint64 = 0, 0 var successful, failed uint64 = 0, 0
for _, songPath := range songPaths { for i := 0; i < int(maxWorkers); i++ {
s, f := replaceBackgrounds(songPath, replacementImage) WG.Add(1)
successful += s go worker(songPaths, replacementImage, &successful, &failed, &WG)
failed += f
} }
log.Printf("\n\nDONE. %d successful; %d failed", successful, failed)
close(songPaths)
WG.Wait()
endTime := time.Now().UTC()
log.Printf("\n\nDONE in %v . %d successful; %d failed", endTime.Sub(startingTime), successful, failed)
} }

Loading…
Cancel
Save