简体   繁体   English

是否有另一种方法可以让 WaitGroup 显示该过程?

[英]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.当我在没有 WaitGroup 的情况下运行时,它只会创建我需要的整个文件中的一部分。 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.它显示worker1做工作,worker2做工作等等......所以直到程序结束它会显示每一个。

Otherwise, with waitgroup it create the whole file i need.否则,使用 waitgroup 它创建我需要的整个文件。 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.但是,它完全合二为一,没有显示进程,显示当我用 WaitGroup 运行它时,它就像... 等待整个过程在哪里 xD,它刚刚以显示 Worker1 做作业、worker2 做作业等结束。 . 在程序结束时。

Is there any thing i can do with this Waitgroup so it show each of its print?我可以用这个 Waitgroup 做些什么,让它显示它的每一个打印件?

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:你需要创建一些通道来监听之前的通道完成了什么,这个我的例子我有 20 个例程,它们会同时处理一些逻辑,并按原始顺序返回:

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")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM