forked from wxbool/video-srt-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
92 lines (79 loc) · 1.88 KB
/
task.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
90
91
92
package app
type VideoMultitask struct {
MaxConcurrencyNumber int //最大运行并发数
Total int //任务总数
QueueFile []string //任务队列
CurrentIndex int //已处理的下标
FinishNumber int //已完成的任务数量
VideoSrt *VideoSrt
}
func NewVideoMultitask(concurrencyNumber int) (*VideoMultitask) {
app := new(VideoMultitask)
if concurrencyNumber == 0 {
//默认并发数
app.MaxConcurrencyNumber = 2
} else {
app.MaxConcurrencyNumber = concurrencyNumber
}
return app
}
//设置 任务队列
func (task *VideoMultitask) SetQueueFile(queue []string) {
task.QueueFile = queue
task.Total = len(queue)
}
//设置 任务队列
func (task *VideoMultitask) SetVideoSrt(v *VideoSrt) {
task.VideoSrt = v
}
//设置 并发数量
func (task *VideoMultitask) SetMaxConcurrencyNumber(n int) {
task.MaxConcurrencyNumber = n
}
//并发运行
func (task *VideoMultitask) 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.VideoSrt.Run(path)
}()
} else {
task.CurrentIndex++
path := task.QueueFile[task.CurrentIndex]
go func() {
task.VideoSrt.Run(path)
}()
}
number++
}
}
func (task *VideoMultitask) RunOver() bool {
//fmt.Println("RunOver:" , task.CurrentIndex)
if task.CurrentIndex >= (task.Total - 1) {
//任务队列处理完成
return true
}
//执行
task.CurrentIndex++
path := task.QueueFile[task.CurrentIndex]
go func() {
task.VideoSrt.Run(path)
}()
return false
}
//标记已完成
func (task *VideoMultitask) FinishTask() bool {
task.FinishNumber++
//fmt.Println("FinishTask:" , task.FinishNumber)
if (task.FinishNumber == task.Total) {
return true
}
return false
}