From 87842afd1318549501bd84641a3faae9473c4232 Mon Sep 17 00:00:00 2001 From: sunshineplan Date: Tue, 9 Jul 2024 13:28:18 +0800 Subject: [PATCH] converter: Update workers --- converter/go.mod | 2 ++ converter/go.sum | 4 ++++ converter/main.go | 13 +++++++------ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/converter/go.mod b/converter/go.mod index 046c4c0..38d2df1 100644 --- a/converter/go.mod +++ b/converter/go.mod @@ -6,6 +6,7 @@ require ( github.com/sunshineplan/imgconv v0.0.0-00010101000000-000000000000 github.com/sunshineplan/tiff v0.0.0-20220128141034-29b9d69bd906 github.com/sunshineplan/utils v0.1.71 + github.com/sunshineplan/workers v1.0.1 ) require ( @@ -17,6 +18,7 @@ require ( github.com/rivo/uniseg v0.4.4 // indirect github.com/sunshineplan/pdf v1.0.7 // indirect golang.org/x/image v0.18.0 // indirect + golang.org/x/sync v0.7.0 // indirect golang.org/x/text v0.16.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/converter/go.sum b/converter/go.sum index 812db38..046173e 100644 --- a/converter/go.sum +++ b/converter/go.sum @@ -17,8 +17,12 @@ github.com/sunshineplan/tiff v0.0.0-20220128141034-29b9d69bd906 h1:+yYRCj+PGQNnn github.com/sunshineplan/tiff v0.0.0-20220128141034-29b9d69bd906/go.mod h1:O+Ar7ouRbdfxLgoZLFz447/dvdM1NVKk1VpOQaijvAU= github.com/sunshineplan/utils v0.1.71 h1:FTe6qAdevePih+hLil1KvKhMAU2Bp+qHTtlO0wxoAYI= github.com/sunshineplan/utils v0.1.71/go.mod h1:QMe2vCEFq9VliPac8WfXndi5jP0ZrPHus8wvJlgBy5M= +github.com/sunshineplan/workers v1.0.1 h1:vhDo4ZaerVu0QM1udRmWpvHY2U61/vTLRVwmKcYOxNw= +github.com/sunshineplan/workers v1.0.1/go.mod h1:EjLgk5vd/2ig6vdWDAZy/fpkzdSm8IVDsARPQVSyQy0= golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ= golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/converter/main.go b/converter/main.go index e942e0a..24699b8 100644 --- a/converter/main.go +++ b/converter/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "errors" "flag" "fmt" @@ -16,7 +17,7 @@ import ( "github.com/sunshineplan/utils/flags" "github.com/sunshineplan/utils/log" "github.com/sunshineplan/utils/progressbar" - "github.com/sunshineplan/utils/workers" + "github.com/sunshineplan/workers" ) var ( @@ -37,7 +38,7 @@ var ( width = flag.Int("width", 0, "") height = flag.Int("height", 0, "") percent = flag.Float64("percent", 0, "") - worker = flag.Int("worker", 5, "") + worker = flag.Int64("worker", 5, "") debug = flag.Bool("debug", false, "") format imgconv.Format @@ -133,12 +134,12 @@ func main() { log.Println("Total images:", total) pb := progressbar.New(total) pb.Start() - workers.RunSlice(*worker, images, func(_ int, image string) { + workers.NewWorkers(*worker).Run(context.Background(), workers.SliceJob(images, func(_ int, image string) { defer pb.Add(1) if _, err := open(image); err != nil { log.Error("Bad image", "image", image, "error", err) } - }) + })) pb.Done() case srcInfo.Mode().IsRegular(): if _, err := open(*src); err != nil { @@ -210,7 +211,7 @@ func main() { log.Println("Total images:", total) pb := progressbar.New(total) pb.Start() - workers.RunSlice(*worker, images, func(_ int, image string) { + workers.NewWorkers(*worker).Run(context.Background(), workers.SliceJob(images, func(_ int, image string) { defer pb.Add(1) rel, err := filepath.Rel(*src, image) if err != nil { @@ -225,7 +226,7 @@ func main() { return } log.Debug("Converted " + image) - }) + })) pb.Done() case srcInfo.Mode().IsRegular(): output := *dst