forked from wxbool/video-srt-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_tanslate.go
89 lines (77 loc) · 1.88 KB
/
task_tanslate.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package app
type TranslateMultitask struct {
MaxConcurrencyNumber int //最大运行并发数
Total int //任务总数
QueueFile []string //任务队列
CurrentIndex int //已处理的下标
FinishNumber int //已完成的任务数量
SrtTranslateApp *SrtTranslateApp
}
func NewTranslateMultitask(concurrencyNumber int) (*TranslateMultitask) {
app := new(TranslateMultitask)
if concurrencyNumber == 0 {
//默认并发数
app.MaxConcurrencyNumber = 2
} else {
app.MaxConcurrencyNumber = concurrencyNumber
}
return app
}
//设置 任务队列
func (task *TranslateMultitask) SetQueueFile(queue []string) {
task.QueueFile = queue
task.Total = len(queue)
}
//设置 任务队列
func (task *TranslateMultitask) SetSrtTranslateApp(app *SrtTranslateApp) {
task.SrtTranslateApp = app
}
//设置 并发数量
func (task *TranslateMultitask) SetMaxConcurrencyNumber(n int) {
task.MaxConcurrencyNumber = n
}
//并发运行
func (task *TranslateMultitask) Run() {
//初始参数
task.CurrentIndex = -1
task.FinishNumber = 0
number := 1
//并发调用
for number <= task.MaxConcurrencyNumber && task.CurrentIndex < (task.Total - 1){
if task.CurrentIndex == -1 {
task.CurrentIndex = 0;
path := task.QueueFile[task.CurrentIndex]
go func() {
task.SrtTranslateApp.Run(path)
}()
} else {
task.CurrentIndex++
path := task.QueueFile[task.CurrentIndex]
go func() {
task.SrtTranslateApp.Run(path)
}()
}
number++
}
}
func (task *TranslateMultitask) RunOver() bool {
if task.CurrentIndex >= (task.Total - 1) {
//任务队列处理完成
return true
}
//执行
task.CurrentIndex++
path := task.QueueFile[task.CurrentIndex]
go func() {
task.SrtTranslateApp.Run(path)
}()
return false
}
//标记已完成
func (task *TranslateMultitask) FinishTask() bool {
task.FinishNumber++
if (task.FinishNumber == task.Total) {
return true
}
return false
}