-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel.go
72 lines (56 loc) · 1.14 KB
/
parallel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"io/ioutil"
"os/exec"
"sync"
"runtime"
)
const POOL_SIZE_MULTIPLIER = 1
func main() {
batch := make(map[int][]int)
batch[0] = []int{1, 2, 3, 4, 5}
batch[1] = []int{6, 7, 8, 9, 10}
files, _ := ioutil.ReadDir("./images")
counter := 0
pool_size := POOL_SIZE_MULTIPLIER * runtime.NumCPU()
for i := 0; i < len(files); i += pool_size {
end := i + pool_size
if end > len(files) {
end = len(files)
}
batch[counter] = makeRange(i, end-1)
counter += 1
}
ch := make(chan []int, 1)
go func(ch chan []int, batch map[int][]int) {
for key := range batch {
ch <- batch[key]
}
close(ch)
}(ch, batch)
for list := range ch {
wg := sync.WaitGroup{}
for _, each := range list {
wg.Add(1)
go func(each int) {
file_name := fmt.Sprintf("./images/frame_%d.png", each)
cmd := exec.Command("python", "outline.py", file_name)
fmt.Println("Outlining", file_name)
err := cmd.Run()
if err != nil {
fmt.Println(err)
}
wg.Done()
}(each)
}
wg.Wait()
}
}
func makeRange(min, max int) []int {
a := make([]int, max-min+1)
for i := range a {
a[i] = min + i
}
return a
}