Skip to content

Commit

Permalink
Use all CPU cores for conversion (closes #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Dec 15, 2017
1 parent 94cb85f commit c0e081a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
63 changes: 45 additions & 18 deletions kepub/kepub.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sync"
"time"

"github.com/beevik/etree"
Expand Down Expand Up @@ -56,24 +58,49 @@ func Kepubify(src, dest string, printlog bool) error {
bar.Start()
}

for _, cf := range contentfiles {
buf, err := ioutil.ReadFile(cf)
if err != nil {
return fmt.Errorf("Could not open content file \"%s\" for reading: %s", cf, err)
}
str := string(buf)
err = process(&str)
if err != nil {
return fmt.Errorf("Error processing content file \"%s\": %s", cf, err)
}
err = ioutil.WriteFile(cf, []byte(str), 0644)
if err != nil {
return fmt.Errorf("Error writing content file \"%s\": %s", cf, err)
}
time.Sleep(time.Millisecond * 5)
if printlog {
bar.Increment()
}
runtime.GOMAXPROCS(runtime.NumCPU() + 1)
wg := sync.WaitGroup{}
cerr := make(chan error, 1)
for _, f := range contentfiles {
wg.Add(1)
go func(cf string) {
defer wg.Done()
buf, err := ioutil.ReadFile(cf)
if err != nil {
select {
case cerr <- fmt.Errorf("Could not open content file \"%s\" for reading: %s", cf, err): // Put err in the channel unless it is full
default:
}
return
}
str := string(buf)
err = process(&str)
if err != nil {
select {
case cerr <- fmt.Errorf("Error processing content file \"%s\": %s", cf, err): // Put err in the channel unless it is full
default:
}
return
}
err = ioutil.WriteFile(cf, []byte(str), 0644)
if err != nil {
select {
case cerr <- fmt.Errorf("Error writing content file \"%s\": %s", cf, err): // Put err in the channel unless it is full
default:
}
return
}
time.Sleep(time.Millisecond * 5)
if printlog {
bar.Increment()
}
}(f)
}
wg.Wait()
if len(cerr) > 0 {
bar.Finish()
fmt.Println()
return <-cerr
}

if printlog {
Expand Down
8 changes: 7 additions & 1 deletion kepubify.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ func main() {
app.Version = version

app.ArgsUsage = "EPUB_INPUT_PATH [KEPUB_OUTPUT_PATH]"
app.Action = convert
app.Action = func(c *cli.Context) error {
err := convert(c)
if err != nil {
fmt.Println(err)
}
return err
}

app.Run(os.Args)
}

0 comments on commit c0e081a

Please sign in to comment.