Panji Tri Wahyudi
Panji Tri Wahyudi

Reputation: 512

Is there another way to make WaitGroup showed the process?

This is my Snippet Code to run the whole worker

for w := 1; w <= *totalworker; w++ {
        wg.Add(1)
        go worker(w, jobs, results, dir, &wg)
    }

This was my Worker

    defer wg.Done()
    for j := range jobs {
        filename := j[0][4] + ".csv"
        fmt.Printf("\nWorker %d starting a job\n", id)
        //results <- j //to show The result of jobs, unnecessary
        fmt.Printf("\nWorker %d Creating %s.csv\n", id, j[0][4])
        CreateFile(dir, &filename, j)
        fmt.Printf("\nWorker %d Finished creating %s.csv on %s\n", id, j[0][4], *dir)
        fmt.Printf("\nWorker %d finished a job\n", id)
    }
}

When i run without WaitGroup it will only create a few of the whole file i needed. but It show the process of it. It show worker1 do job, worker2 do job etc... so until the end of program it will show each of it.

Otherwise, with waitgroup it create the whole file i need. But, its completely do all in one without show the process, show when i run it with WaitGroup it just ended like ... wait where is the whole process xD, it just ended with showing Worker1 do job, worker2 do job etc... at End of program.

Is there any thing i can do with this Waitgroup so it show each of its print?

Upvotes: 0

Views: 79

Answers (1)

Sơn x&#234; k&#244;
Sơn x&#234; k&#244;

Reputation: 207

You need create some channels to listening what previous channel is completed like that, this my example I have 20 routines, they will process some logic at the same time, and return in original order:

package channel

import (
    "fmt"
    "sync"
    "time"
)


func Tests() {

    c := make(map[int]chan bool)

    var wg sync.WaitGroup

    // total go routine
    loop := 20

    // stop in step
    stop := 11

    for i := 1; i <= loop; i++ {
        // init channel
        c[i] = make(chan bool)
    }

    for i := 1; i <= loop; i++ {
        wg.Add(1)
        go func(c map[int]chan bool, i int) {
            defer wg.Done()

            // logic step
            fmt.Println("Process Logic step ", i)

            if i == 1 {
                fmt.Println("Sending message first ", i)
                c[i] <- true // send now notify to next step
            } else {
                select {
                    case channel := <-c[i-1]:
                    defer close(c[i-1])

                    if channel == true {
                        // send now
                        fmt.Println("Sending message ", i)
                        // not sent
                        if i < loop { // fix deadlock when channel doesnt write

                            if i == stop && stop > 0 {
                                c[i] <- false // stop in step i
                            } else {
                                c[i] <- true // send now notify to next step
                            }
                        }
                    } else {
                        // not send
                        if i < loop { // fix deadlock when channel doesnt write
                            c[i] <- false
                        }
                    }
                }
            }
        }(c, i)
    }
    wg.Wait()
    fmt.Println("go here ")
    //time.Sleep(3 * time.Second)go run
    fmt.Println("End")
}

Upvotes: 1

Related Questions