multithread

This commit is contained in:
ChaotiCryptidz 2022-08-04 16:22:51 +01:00
parent c263ccbda0
commit 204c73d46c
No known key found for this signature in database

View file

@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"sync"
"github.com/akamensky/argparse"
"github.com/rs/zerolog/log"
@ -16,6 +17,7 @@ type CopyCommandArgs struct {
Source *string
Dest *string
TranscodePreset *string
Threads *int
SkipExisting *bool
SingleDirectory *bool
}
@ -107,6 +109,10 @@ func RegisterCopyCommand(parser *argparse.Parser) (*argparse.Command, *CopyComma
Default: false,
})
arguments.Threads = cmd.Int("", "threads", &argparse.Options{
Default: 0,
})
return cmd, arguments
}
@ -209,10 +215,33 @@ func (c *CopyCommand) transcode_files() {
if err != nil {
log.Fatal().Err(err).Msg("Could not get transcode preset")
}
if *c.Args.Threads == 0 {
for _, file := range c.Files {
c._transcode_file(file, trans_preset.Config)
}
} else {
var wg sync.WaitGroup
threads := *c.Args.Threads
jobs := make(chan *types.File, threads)
for _, file := range c.Files {
c._transcode_file(file, trans_preset.Config)
for i := 1; i <= threads; i++ {
go func() {
for file := range jobs {
defer wg.Done()
c._transcode_file(file, trans_preset.Config)
}
}()
}
for _, file := range c.Files {
wg.Add(1)
jobs <- file
}
close(jobs)
wg.Wait()
}
}
func (c *CopyCommand) _copy_file(file *types.File) {