forked from davyxu/tabtoy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallelworker.go
More file actions
60 lines (42 loc) · 1.09 KB
/
parallelworker.go
File metadata and controls
60 lines (42 loc) · 1.09 KB
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
package main
import (
"path"
)
// 封装signal返回, 因为go关键字会忽略函数返回值, 所以用channel来传递结果
func task(input, output string, callback func(string, string) bool, signal chan bool) bool {
result := callback(input, output)
if signal != nil {
signal <- result
return result
}
return result
}
func parallelWorker(fileList []string, para bool, outDir string, callback func(string, string) bool) bool {
// 处理多个导出文件情况
var signal chan bool
if para {
signal = make(chan bool)
}
for _, v := range fileList {
inputFile := v
// 使用指定的导出文件夹,并更换电子表格输入文件的后缀名为pbt作为输出文件
outputFile := path.Join(outDir, changeFileExt(inputFile, getOutputExt()))
if signal != nil {
go task(inputFile, outputFile, callback, signal)
} else {
if !task(inputFile, outputFile, callback, signal) {
return false
}
}
}
// 并发导出同步
if signal != nil {
for i := 0; i < len(fileList); i++ {
result := <-signal
if !result {
return false
}
}
}
return true
}