summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrail Finder <wohilas@gmail.com>2023-02-18 21:10:50 +0600
committerGrail Finder <wohilas@gmail.com>2023-02-18 21:10:50 +0600
commit1ec6ff8ec85ea4597b4897f1d2f1a89ec5e1abb1 (patch)
treeca809686b6c3928b5f3c6879931e70decbe428d1
parentdf498c756b1cb0e3f045bc88396223272b713a4c (diff)
Feat: close channel before iterating
-rw-r--r--main.go12
-rw-r--r--workers.go15
2 files changed, 20 insertions, 7 deletions
diff --git a/main.go b/main.go
index 5f738aa..aa90be3 100644
--- a/main.go
+++ b/main.go
@@ -188,6 +188,7 @@ func main() {
// === concurrent ===
queue := make(chan *Utterance, len(utterances))
geshaft := make(chan *Utterance, len(utterances))
+ done := make(chan bool, 1)
for _, ut := range utterances {
if _, err := os.Stat(ut.OutPath); os.IsNotExist(err) {
@@ -204,23 +205,28 @@ func main() {
workers := 100
for i := 0; i < workers; i++ {
- go worker(queue, i, geshaft)
+ go worker(queue, done, i, geshaft)
}
for {
if len(queue) == 0 {
+ fmt.Println("empty queue: sending true to done")
+ done <- true
// give time for workers to finish
- time.Sleep(3 * time.Second)
+ fmt.Println("sleeping for few seconds")
+ time.Sleep(5 * time.Second)
+ fmt.Println("closing queue")
close(queue)
+ fmt.Println("breaking from the loop")
break
}
time.Sleep(1 * time.Second)
}
+ close(geshaft)
for ut := range geshaft {
filteredUtterances = append(filteredUtterances, ut)
}
- close(geshaft)
newMeta := utterancesToFileTextMap(filteredUtterances)
writeCSV(mapToCSV(newMeta))
}
diff --git a/workers.go b/workers.go
index 6fa4835..03c9072 100644
--- a/workers.go
+++ b/workers.go
@@ -2,15 +2,22 @@ package main
import "fmt"
-func worker(queue chan *Utterance, worknumber int, geshaft chan *Utterance) {
+func worker(queue chan *Utterance, done chan bool, worknumber int, geshaft chan *Utterance) {
for {
if len(queue) == 0 {
fmt.Println("empty queue, number", worknumber)
return
}
- ut := <-queue
- if err := cutoutClipAndTranscode(ut); err == nil {
- geshaft <- ut
+ select {
+
+ case ut := <-queue:
+ if err := cutoutClipAndTranscode(ut); err == nil {
+ geshaft <- ut
+ }
+
+ case <-done:
+ fmt.Println("done signal, number", worknumber)
+ return
}
}
}